From 93da10abbb105261aea4f4b565ebf4fb65499b7d Mon Sep 17 00:00:00 2001 From: refraction Date: Fri, 6 Feb 2009 19:15:15 +0000 Subject: [PATCH] git-svn-id: http://pcsx2.googlecode.com/svn/trunk@411 96395faa-99c1-11dd-bbfe-3dabce05a288 --- plugins/GSdx/GPU.cpp | 306 ++ plugins/GSdx/GPU.h | 276 ++ plugins/GSdx/GPUDrawScanline.cpp | 910 +++++ plugins/GSdx/GPUDrawScanline.h | 146 + plugins/GSdx/GPUDrawingEnvironment.h | 81 + plugins/GSdx/GPULocalMemory.cpp | 666 ++++ plugins/GSdx/GPULocalMemory.h | 85 + plugins/GSdx/GPURenderer.cpp | 25 + plugins/GSdx/GPURenderer.h | 399 ++ plugins/GSdx/GPURendererSW.cpp | 23 + plugins/GSdx/GPURendererSW.h | 218 ++ plugins/GSdx/GPUSettingsDlg.cpp | 301 ++ plugins/GSdx/GPUSettingsDlg.h | 71 + plugins/GSdx/GPUState.cpp | 747 ++++ plugins/GSdx/GPUState.h | 143 + plugins/GSdx/GPUVertex.h | 51 + plugins/GSdx/GS.cpp | 589 +++ plugins/GSdx/GS.h | 1101 ++++++ plugins/GSdx/GSAlignedClass.cpp | 23 + plugins/GSdx/GSAlignedClass.h | 48 + plugins/GSdx/GSBlock.cpp | 37 + plugins/GSdx/GSBlock.h | 2289 +++++++++++ plugins/GSdx/GSCapture.cpp | 539 +++ plugins/GSdx/GSCapture.h | 43 + plugins/GSdx/GSCaptureDlg.cpp | 243 ++ plugins/GSdx/GSCaptureDlg.h | 73 + plugins/GSdx/GSClut.cpp | 869 +++++ plugins/GSdx/GSClut.h | 110 + plugins/GSdx/GSCrc.cpp | 143 + plugins/GSdx/GSCrc.h | 98 + plugins/GSdx/GSDevice.cpp | 23 + plugins/GSdx/GSDevice.h | 244 ++ plugins/GSdx/GSDevice10.cpp | 833 ++++ plugins/GSdx/GSDevice10.h | 147 + plugins/GSdx/GSDevice7.cpp | 241 ++ plugins/GSdx/GSDevice7.h | 58 + plugins/GSdx/GSDevice9.cpp | 1062 +++++ plugins/GSdx/GSDevice9.h | 192 + plugins/GSdx/GSDeviceNull.cpp | 50 + plugins/GSdx/GSDeviceNull.h | 53 + plugins/GSdx/GSDirtyRect.cpp | 70 + plugins/GSdx/GSDirtyRect.h | 42 + plugins/GSdx/GSDrawScanline.cpp | 2845 ++++++++++++++ plugins/GSdx/GSDrawScanline.h | 217 ++ plugins/GSdx/GSDrawingContext.h | 115 + plugins/GSdx/GSDrawingEnvironment.h | 77 + plugins/GSdx/GSDump.cpp | 88 + plugins/GSdx/GSDump.h | 59 + plugins/GSdx/GSFunctionMap.cpp | 24 + plugins/GSdx/GSFunctionMap.h | 161 + plugins/GSdx/GSLocalMemory.cpp | 2399 ++++++++++++ plugins/GSdx/GSLocalMemory.h | 1009 +++++ plugins/GSdx/GSPerfMon.cpp | 103 + plugins/GSdx/GSPerfMon.h | 63 + plugins/GSdx/GSRasterizer.cpp | 726 ++++ plugins/GSdx/GSRasterizer.h | 153 + plugins/GSdx/GSRenderer.cpp | 23 + plugins/GSdx/GSRenderer.h | 594 +++ plugins/GSdx/GSRendererHW.cpp | 23 + plugins/GSdx/GSRendererHW.h | 633 +++ plugins/GSdx/GSRendererHW10.cpp | 586 +++ plugins/GSdx/GSRendererHW10.h | 56 + plugins/GSdx/GSRendererHW9.cpp | 594 +++ plugins/GSdx/GSRendererHW9.h | 65 + plugins/GSdx/GSRendererNull.cpp | 23 + plugins/GSdx/GSRendererNull.h | 49 + plugins/GSdx/GSRendererSW.cpp | 25 + plugins/GSdx/GSRendererSW.h | 858 ++++ plugins/GSdx/GSSetting.cpp | 24 + plugins/GSdx/GSSetting.h | 45 + plugins/GSdx/GSSettingsDlg.cpp | 324 ++ plugins/GSdx/GSSettingsDlg.h | 79 + plugins/GSdx/GSState.cpp | 2179 +++++++++++ plugins/GSdx/GSState.h | 268 ++ plugins/GSdx/GSTables.cpp | 263 ++ plugins/GSdx/GSTables.h | 39 + plugins/GSdx/GSTexture.cpp | 23 + plugins/GSdx/GSTexture.h | 48 + plugins/GSdx/GSTexture10.cpp | 210 + plugins/GSdx/GSTexture10.h | 57 + plugins/GSdx/GSTexture7.cpp | 184 + plugins/GSdx/GSTexture7.h | 55 + plugins/GSdx/GSTexture9.cpp | 224 ++ plugins/GSdx/GSTexture9.h | 54 + plugins/GSdx/GSTextureCache.cpp | 23 + plugins/GSdx/GSTextureCache.h | 816 ++++ plugins/GSdx/GSTextureCache10.cpp | 452 +++ plugins/GSdx/GSTextureCache10.h | 66 + plugins/GSdx/GSTextureCache9.cpp | 453 +++ plugins/GSdx/GSTextureCache9.h | 66 + plugins/GSdx/GSTextureCacheSW.cpp | 376 ++ plugins/GSdx/GSTextureCacheSW.h | 83 + plugins/GSdx/GSTextureFX10.cpp | 583 +++ plugins/GSdx/GSTextureFX10.h | 248 ++ plugins/GSdx/GSTextureFX9.cpp | 464 +++ plugins/GSdx/GSTextureFX9.h | 173 + plugins/GSdx/GSTextureNull.cpp | 36 + plugins/GSdx/GSTextureNull.h | 44 + plugins/GSdx/GSUtil.cpp | 272 ++ plugins/GSdx/GSUtil.h | 68 + plugins/GSdx/GSVector.cpp | 46 + plugins/GSdx/GSVector.h | 2644 +++++++++++++ plugins/GSdx/GSVertex.h | 87 + plugins/GSdx/GSVertexHW.h | 87 + plugins/GSdx/GSVertexList.cpp | 23 + plugins/GSdx/GSVertexList.h | 87 + plugins/GSdx/GSVertexSW.h | 254 ++ plugins/GSdx/GSWnd.cpp | 76 + plugins/GSdx/GSWnd.h | 37 + plugins/GSdx/GSdx.cpp | 97 + plugins/GSdx/GSdx.def | 63 + plugins/GSdx/GSdx.h | 37 + plugins/GSdx/GSdx.rc | 269 ++ plugins/GSdx/GSdx_vs2008.vcproj | 5054 ++++++++++++++++++++++++ plugins/GSdx/baseclasses/activex.rcv | 142 + plugins/GSdx/baseclasses/activex.ver | 56 + plugins/GSdx/baseclasses/amextra.cpp | 111 + plugins/GSdx/baseclasses/amextra.h | 56 + plugins/GSdx/baseclasses/amfilter.cpp | 5203 +++++++++++++++++++++++++ plugins/GSdx/baseclasses/amfilter.h | 1587 ++++++++ plugins/GSdx/baseclasses/amvideo.cpp | 275 ++ plugins/GSdx/baseclasses/cache.h | 74 + plugins/GSdx/baseclasses/combase.cpp | 256 ++ plugins/GSdx/baseclasses/combase.h | 319 ++ plugins/GSdx/baseclasses/ctlutil.cpp | 2531 ++++++++++++ plugins/GSdx/baseclasses/ctlutil.h | 919 +++++ plugins/GSdx/baseclasses/ddmm.cpp | 129 + plugins/GSdx/baseclasses/ddmm.h | 28 + plugins/GSdx/baseclasses/dsschedule.h | 128 + plugins/GSdx/baseclasses/fourcc.h | 101 + plugins/GSdx/baseclasses/measure.h | 222 ++ plugins/GSdx/baseclasses/msgthrd.h | 120 + plugins/GSdx/baseclasses/mtype.cpp | 477 +++ plugins/GSdx/baseclasses/mtype.h | 89 + plugins/GSdx/baseclasses/outputq.cpp | 794 ++++ plugins/GSdx/baseclasses/outputq.h | 137 + plugins/GSdx/baseclasses/pstream.cpp | 196 + plugins/GSdx/baseclasses/pstream.h | 114 + plugins/GSdx/baseclasses/pullpin.cpp | 527 +++ plugins/GSdx/baseclasses/pullpin.h | 152 + plugins/GSdx/baseclasses/refclock.cpp | 340 ++ plugins/GSdx/baseclasses/refclock.h | 171 + plugins/GSdx/baseclasses/reftime.h | 116 + plugins/GSdx/baseclasses/renbase.cpp | 2844 ++++++++++++++ plugins/GSdx/baseclasses/renbase.h | 478 +++ plugins/GSdx/baseclasses/schedule.cpp | 284 ++ plugins/GSdx/baseclasses/schedule.h | 128 + plugins/GSdx/baseclasses/seekpt.cpp | 83 + plugins/GSdx/baseclasses/seekpt.h | 30 + plugins/GSdx/baseclasses/source.cpp | 522 +++ plugins/GSdx/baseclasses/source.h | 172 + plugins/GSdx/baseclasses/streams.h | 254 ++ plugins/GSdx/baseclasses/strmctl.cpp | 401 ++ plugins/GSdx/baseclasses/strmctl.h | 157 + plugins/GSdx/baseclasses/sysclock.cpp | 74 + plugins/GSdx/baseclasses/sysclock.h | 39 + plugins/GSdx/baseclasses/transfrm.cpp | 1016 +++++ plugins/GSdx/baseclasses/transfrm.h | 304 ++ plugins/GSdx/baseclasses/transip.cpp | 966 +++++ plugins/GSdx/baseclasses/transip.h | 250 ++ plugins/GSdx/baseclasses/vtrans.cpp | 468 +++ plugins/GSdx/baseclasses/vtrans.h | 143 + plugins/GSdx/baseclasses/wxdebug.cpp | 1418 +++++++ plugins/GSdx/baseclasses/wxdebug.h | 393 ++ plugins/GSdx/baseclasses/wxlist.cpp | 885 +++++ plugins/GSdx/baseclasses/wxlist.h | 543 +++ plugins/GSdx/baseclasses/wxutil.cpp | 1243 ++++++ plugins/GSdx/baseclasses/wxutil.h | 541 +++ plugins/GSdx/res/GSdx.rc2 | 22 + plugins/GSdx/res/convert10.fx | 67 + plugins/GSdx/res/convert9.fx | 55 + plugins/GSdx/res/interlace10.fx | 43 + plugins/GSdx/res/interlace9.fx | 35 + plugins/GSdx/res/logo10.bmp | Bin 0 -> 53094 bytes plugins/GSdx/res/logo9.bmp | Bin 0 -> 56002 bytes plugins/GSdx/res/merge10.fx | 27 + plugins/GSdx/res/merge9.fx | 25 + plugins/GSdx/res/tfx10.fx | 501 +++ plugins/GSdx/res/tfx9.fx | 313 ++ plugins/GSdx/resource.h | 55 + plugins/GSdx/sse.h | 132 + plugins/GSdx/stdafx.cpp | 8 + plugins/GSdx/stdafx.h | 107 + plugins/GSdx/svnrev_template.h | 2 + plugins/GSdx/vsprops/common.vsprops | 32 + plugins/GSdx/vsprops/debug.vsprops | 19 + plugins/GSdx/vsprops/release.vsprops | 25 + plugins/GSdx/vsprops/sse2.vsprops | 12 + plugins/GSdx/vsprops/sse4.vsprops | 12 + plugins/GSdx/vsprops/ssse3.vsprops | 12 + 190 files changed, 72906 insertions(+) create mode 100644 plugins/GSdx/GPU.cpp create mode 100644 plugins/GSdx/GPU.h create mode 100644 plugins/GSdx/GPUDrawScanline.cpp create mode 100644 plugins/GSdx/GPUDrawScanline.h create mode 100644 plugins/GSdx/GPUDrawingEnvironment.h create mode 100644 plugins/GSdx/GPULocalMemory.cpp create mode 100644 plugins/GSdx/GPULocalMemory.h create mode 100644 plugins/GSdx/GPURenderer.cpp create mode 100644 plugins/GSdx/GPURenderer.h create mode 100644 plugins/GSdx/GPURendererSW.cpp create mode 100644 plugins/GSdx/GPURendererSW.h create mode 100644 plugins/GSdx/GPUSettingsDlg.cpp create mode 100644 plugins/GSdx/GPUSettingsDlg.h create mode 100644 plugins/GSdx/GPUState.cpp create mode 100644 plugins/GSdx/GPUState.h create mode 100644 plugins/GSdx/GPUVertex.h create mode 100644 plugins/GSdx/GS.cpp create mode 100644 plugins/GSdx/GS.h create mode 100644 plugins/GSdx/GSAlignedClass.cpp create mode 100644 plugins/GSdx/GSAlignedClass.h create mode 100644 plugins/GSdx/GSBlock.cpp create mode 100644 plugins/GSdx/GSBlock.h create mode 100644 plugins/GSdx/GSCapture.cpp create mode 100644 plugins/GSdx/GSCapture.h create mode 100644 plugins/GSdx/GSCaptureDlg.cpp create mode 100644 plugins/GSdx/GSCaptureDlg.h create mode 100644 plugins/GSdx/GSClut.cpp create mode 100644 plugins/GSdx/GSClut.h create mode 100644 plugins/GSdx/GSCrc.cpp create mode 100644 plugins/GSdx/GSCrc.h create mode 100644 plugins/GSdx/GSDevice.cpp create mode 100644 plugins/GSdx/GSDevice.h create mode 100644 plugins/GSdx/GSDevice10.cpp create mode 100644 plugins/GSdx/GSDevice10.h create mode 100644 plugins/GSdx/GSDevice7.cpp create mode 100644 plugins/GSdx/GSDevice7.h create mode 100644 plugins/GSdx/GSDevice9.cpp create mode 100644 plugins/GSdx/GSDevice9.h create mode 100644 plugins/GSdx/GSDeviceNull.cpp create mode 100644 plugins/GSdx/GSDeviceNull.h create mode 100644 plugins/GSdx/GSDirtyRect.cpp create mode 100644 plugins/GSdx/GSDirtyRect.h create mode 100644 plugins/GSdx/GSDrawScanline.cpp create mode 100644 plugins/GSdx/GSDrawScanline.h create mode 100644 plugins/GSdx/GSDrawingContext.h create mode 100644 plugins/GSdx/GSDrawingEnvironment.h create mode 100644 plugins/GSdx/GSDump.cpp create mode 100644 plugins/GSdx/GSDump.h create mode 100644 plugins/GSdx/GSFunctionMap.cpp create mode 100644 plugins/GSdx/GSFunctionMap.h create mode 100644 plugins/GSdx/GSLocalMemory.cpp create mode 100644 plugins/GSdx/GSLocalMemory.h create mode 100644 plugins/GSdx/GSPerfMon.cpp create mode 100644 plugins/GSdx/GSPerfMon.h create mode 100644 plugins/GSdx/GSRasterizer.cpp create mode 100644 plugins/GSdx/GSRasterizer.h create mode 100644 plugins/GSdx/GSRenderer.cpp create mode 100644 plugins/GSdx/GSRenderer.h create mode 100644 plugins/GSdx/GSRendererHW.cpp create mode 100644 plugins/GSdx/GSRendererHW.h create mode 100644 plugins/GSdx/GSRendererHW10.cpp create mode 100644 plugins/GSdx/GSRendererHW10.h create mode 100644 plugins/GSdx/GSRendererHW9.cpp create mode 100644 plugins/GSdx/GSRendererHW9.h create mode 100644 plugins/GSdx/GSRendererNull.cpp create mode 100644 plugins/GSdx/GSRendererNull.h create mode 100644 plugins/GSdx/GSRendererSW.cpp create mode 100644 plugins/GSdx/GSRendererSW.h create mode 100644 plugins/GSdx/GSSetting.cpp create mode 100644 plugins/GSdx/GSSetting.h create mode 100644 plugins/GSdx/GSSettingsDlg.cpp create mode 100644 plugins/GSdx/GSSettingsDlg.h create mode 100644 plugins/GSdx/GSState.cpp create mode 100644 plugins/GSdx/GSState.h create mode 100644 plugins/GSdx/GSTables.cpp create mode 100644 plugins/GSdx/GSTables.h create mode 100644 plugins/GSdx/GSTexture.cpp create mode 100644 plugins/GSdx/GSTexture.h create mode 100644 plugins/GSdx/GSTexture10.cpp create mode 100644 plugins/GSdx/GSTexture10.h create mode 100644 plugins/GSdx/GSTexture7.cpp create mode 100644 plugins/GSdx/GSTexture7.h create mode 100644 plugins/GSdx/GSTexture9.cpp create mode 100644 plugins/GSdx/GSTexture9.h create mode 100644 plugins/GSdx/GSTextureCache.cpp create mode 100644 plugins/GSdx/GSTextureCache.h create mode 100644 plugins/GSdx/GSTextureCache10.cpp create mode 100644 plugins/GSdx/GSTextureCache10.h create mode 100644 plugins/GSdx/GSTextureCache9.cpp create mode 100644 plugins/GSdx/GSTextureCache9.h create mode 100644 plugins/GSdx/GSTextureCacheSW.cpp create mode 100644 plugins/GSdx/GSTextureCacheSW.h create mode 100644 plugins/GSdx/GSTextureFX10.cpp create mode 100644 plugins/GSdx/GSTextureFX10.h create mode 100644 plugins/GSdx/GSTextureFX9.cpp create mode 100644 plugins/GSdx/GSTextureFX9.h create mode 100644 plugins/GSdx/GSTextureNull.cpp create mode 100644 plugins/GSdx/GSTextureNull.h create mode 100644 plugins/GSdx/GSUtil.cpp create mode 100644 plugins/GSdx/GSUtil.h create mode 100644 plugins/GSdx/GSVector.cpp create mode 100644 plugins/GSdx/GSVector.h create mode 100644 plugins/GSdx/GSVertex.h create mode 100644 plugins/GSdx/GSVertexHW.h create mode 100644 plugins/GSdx/GSVertexList.cpp create mode 100644 plugins/GSdx/GSVertexList.h create mode 100644 plugins/GSdx/GSVertexSW.h create mode 100644 plugins/GSdx/GSWnd.cpp create mode 100644 plugins/GSdx/GSWnd.h create mode 100644 plugins/GSdx/GSdx.cpp create mode 100644 plugins/GSdx/GSdx.def create mode 100644 plugins/GSdx/GSdx.h create mode 100644 plugins/GSdx/GSdx.rc create mode 100644 plugins/GSdx/GSdx_vs2008.vcproj create mode 100644 plugins/GSdx/baseclasses/activex.rcv create mode 100644 plugins/GSdx/baseclasses/activex.ver create mode 100644 plugins/GSdx/baseclasses/amextra.cpp create mode 100644 plugins/GSdx/baseclasses/amextra.h create mode 100644 plugins/GSdx/baseclasses/amfilter.cpp create mode 100644 plugins/GSdx/baseclasses/amfilter.h create mode 100644 plugins/GSdx/baseclasses/amvideo.cpp create mode 100644 plugins/GSdx/baseclasses/cache.h create mode 100644 plugins/GSdx/baseclasses/combase.cpp create mode 100644 plugins/GSdx/baseclasses/combase.h create mode 100644 plugins/GSdx/baseclasses/ctlutil.cpp create mode 100644 plugins/GSdx/baseclasses/ctlutil.h create mode 100644 plugins/GSdx/baseclasses/ddmm.cpp create mode 100644 plugins/GSdx/baseclasses/ddmm.h create mode 100644 plugins/GSdx/baseclasses/dsschedule.h create mode 100644 plugins/GSdx/baseclasses/fourcc.h create mode 100644 plugins/GSdx/baseclasses/measure.h create mode 100644 plugins/GSdx/baseclasses/msgthrd.h create mode 100644 plugins/GSdx/baseclasses/mtype.cpp create mode 100644 plugins/GSdx/baseclasses/mtype.h create mode 100644 plugins/GSdx/baseclasses/outputq.cpp create mode 100644 plugins/GSdx/baseclasses/outputq.h create mode 100644 plugins/GSdx/baseclasses/pstream.cpp create mode 100644 plugins/GSdx/baseclasses/pstream.h create mode 100644 plugins/GSdx/baseclasses/pullpin.cpp create mode 100644 plugins/GSdx/baseclasses/pullpin.h create mode 100644 plugins/GSdx/baseclasses/refclock.cpp create mode 100644 plugins/GSdx/baseclasses/refclock.h create mode 100644 plugins/GSdx/baseclasses/reftime.h create mode 100644 plugins/GSdx/baseclasses/renbase.cpp create mode 100644 plugins/GSdx/baseclasses/renbase.h create mode 100644 plugins/GSdx/baseclasses/schedule.cpp create mode 100644 plugins/GSdx/baseclasses/schedule.h create mode 100644 plugins/GSdx/baseclasses/seekpt.cpp create mode 100644 plugins/GSdx/baseclasses/seekpt.h create mode 100644 plugins/GSdx/baseclasses/source.cpp create mode 100644 plugins/GSdx/baseclasses/source.h create mode 100644 plugins/GSdx/baseclasses/streams.h create mode 100644 plugins/GSdx/baseclasses/strmctl.cpp create mode 100644 plugins/GSdx/baseclasses/strmctl.h create mode 100644 plugins/GSdx/baseclasses/sysclock.cpp create mode 100644 plugins/GSdx/baseclasses/sysclock.h create mode 100644 plugins/GSdx/baseclasses/transfrm.cpp create mode 100644 plugins/GSdx/baseclasses/transfrm.h create mode 100644 plugins/GSdx/baseclasses/transip.cpp create mode 100644 plugins/GSdx/baseclasses/transip.h create mode 100644 plugins/GSdx/baseclasses/vtrans.cpp create mode 100644 plugins/GSdx/baseclasses/vtrans.h create mode 100644 plugins/GSdx/baseclasses/wxdebug.cpp create mode 100644 plugins/GSdx/baseclasses/wxdebug.h create mode 100644 plugins/GSdx/baseclasses/wxlist.cpp create mode 100644 plugins/GSdx/baseclasses/wxlist.h create mode 100644 plugins/GSdx/baseclasses/wxutil.cpp create mode 100644 plugins/GSdx/baseclasses/wxutil.h create mode 100644 plugins/GSdx/res/GSdx.rc2 create mode 100644 plugins/GSdx/res/convert10.fx create mode 100644 plugins/GSdx/res/convert9.fx create mode 100644 plugins/GSdx/res/interlace10.fx create mode 100644 plugins/GSdx/res/interlace9.fx create mode 100644 plugins/GSdx/res/logo10.bmp create mode 100644 plugins/GSdx/res/logo9.bmp create mode 100644 plugins/GSdx/res/merge10.fx create mode 100644 plugins/GSdx/res/merge9.fx create mode 100644 plugins/GSdx/res/tfx10.fx create mode 100644 plugins/GSdx/res/tfx9.fx create mode 100644 plugins/GSdx/resource.h create mode 100644 plugins/GSdx/sse.h create mode 100644 plugins/GSdx/stdafx.cpp create mode 100644 plugins/GSdx/stdafx.h create mode 100644 plugins/GSdx/svnrev_template.h create mode 100644 plugins/GSdx/vsprops/common.vsprops create mode 100644 plugins/GSdx/vsprops/debug.vsprops create mode 100644 plugins/GSdx/vsprops/release.vsprops create mode 100644 plugins/GSdx/vsprops/sse2.vsprops create mode 100644 plugins/GSdx/vsprops/sse4.vsprops create mode 100644 plugins/GSdx/vsprops/ssse3.vsprops diff --git a/plugins/GSdx/GPU.cpp b/plugins/GSdx/GPU.cpp new file mode 100644 index 0000000000..8b13096eb6 --- /dev/null +++ b/plugins/GSdx/GPU.cpp @@ -0,0 +1,306 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSUtil.h" +#include "GPURendererSW.h" +#include "GSDevice7.h" +#include "GSDevice9.h" +#include "GSDevice10.h" +#include "GPUSettingsDlg.h" + +#define PSE_LT_GPU 2 + +static HRESULT s_hr = E_FAIL; +static GPURendererBase* s_gpu = NULL; + +EXPORT_C_(UINT32) PSEgetLibType() +{ + return PSE_LT_GPU; +} + +EXPORT_C_(char*) PSEgetLibName() +{ + return GSUtil::GetLibName(); +} + +EXPORT_C_(UINT32) PSEgetLibVersion() +{ + static const UINT32 version = 1; + static const UINT32 revision = 1; + + return version << 16 | revision << 8 | PLUGIN_VERSION; +} + +EXPORT_C_(INT32) GPUinit() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + // TODO + + return 0; +} + +EXPORT_C_(INT32) GPUshutdown() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + // TODO + + return 0; +} + +EXPORT_C_(INT32) GPUclose() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + delete s_gpu; + + s_gpu = NULL; + + if(SUCCEEDED(s_hr)) + { + ::CoUninitialize(); + + s_hr = E_FAIL; + } + + return 0; +} + +EXPORT_C_(INT32) GPUopen(HWND hWnd) +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + if(!GSUtil::CheckDirectX() || !GSUtil::CheckSSE()) + { + return -1; + } + + GPUclose(); + + GPURendererSettings rs; + + rs.m_filter = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("filter"), 0); + rs.m_dither = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("dithering"), 1); + rs.m_aspectratio = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("AspectRatio"), 1); + rs.m_vsync = !!AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("vsync"), FALSE); + rs.m_scale.cx = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("scale_x"), 0); + rs.m_scale.cy = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("scale_y"), 0); + + int threads = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("swthreads"), 1); + + int renderer = AfxGetApp()->GetProfileInt(_T("GPUSettings"), _T("Renderer"), 1); + + switch(renderer) + { + default: + case 0: s_gpu = new GPURendererSW(rs, threads); break; + case 1: s_gpu = new GPURendererSW(rs, threads); break; + case 2: s_gpu = new GPURendererSW(rs, threads); break; + // TODO: case 3: s_gpu = new GPURendererNull(rs, threads); break; + } + + s_hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); + + if(!s_gpu->Create(hWnd)) + { + GPUclose(); + + return -1; + } + + return 0; +} + +EXPORT_C_(INT32) GPUconfigure() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + GPUSettingsDlg dlg; + + if(IDOK == dlg.DoModal()) + { + GPUshutdown(); + GPUinit(); + } + + return 0; +} + +EXPORT_C_(INT32) GPUtest() +{ + // TODO + + return 0; +} + +EXPORT_C GPUabout() +{ + // TODO +} + +EXPORT_C GPUwriteDataMem(const BYTE* mem, UINT32 size) +{ + s_gpu->WriteData(mem, size); +} + +EXPORT_C GPUwriteData(UINT32 data) +{ + s_gpu->WriteData((BYTE*)&data, 1); +} + +EXPORT_C GPUreadDataMem(BYTE* mem, UINT32 size) +{ + s_gpu->ReadData(mem, size); +} + +EXPORT_C_(UINT32) GPUreadData() +{ + UINT32 data = 0; + + s_gpu->ReadData((BYTE*)&data, 1); + + return data; +} + +EXPORT_C GPUwriteStatus(UINT32 status) +{ + s_gpu->WriteStatus(status); +} + +EXPORT_C_(UINT32) GPUreadStatus() +{ + return s_gpu->ReadStatus(); +} + +EXPORT_C_(UINT32) GPUdmaChain(const BYTE* mem, UINT32 addr) +{ + // TODO + + UINT32 last[3]; + + memset(last, 0xff, sizeof(last)); + + do + { + if(addr == last[1] || addr == last[2]) break; + (addr < last[0] ? last[1] : last[2]) = addr; + last[0] = addr; + + BYTE size = mem[addr + 3]; + + if(size > 0) + { + s_gpu->WriteData(&mem[addr + 4], size); + } + + addr = *(UINT32*)&mem[addr] & 0xffffff; + } + while(addr != 0xffffff); + + return 0; +} + +EXPORT_C_(UINT32) GPUgetMode() +{ + // TODO + + return 0; +} + +EXPORT_C GPUsetMode(UINT32) +{ + // TODO +} + +EXPORT_C GPUupdateLace() +{ + s_gpu->VSync(); +} + +EXPORT_C GPUmakeSnapshot() +{ + LPCTSTR path = _T("C:\\"); // TODO + + s_gpu->MakeSnapshot(path); +} + +EXPORT_C GPUdisplayText(char* text) +{ + // TODO +} + +EXPORT_C GPUdisplayFlags(UINT32 flags) +{ + // TODO +} + +EXPORT_C_(INT32) GPUfreeze(UINT32 type, GPUFreezeData* data) +{ + if(!data || data->version != 1) + { + return 0; + } + + if(type == 0) + { + s_gpu->Defrost(data); + + return 1; + } + else if(type == 1) + { + s_gpu->Freeze(data); + + return 1; + } + else if(type == 2) + { + int slot = *(int*)data + 1; + + if(slot < 1 || slot > 9) + { + return 0; + } + + // TODO + + return 1; + } + + return 0; +} + +EXPORT_C GPUgetScreenPic(BYTE* mem) +{ + // TODO +} + +EXPORT_C GPUshowScreenPic(BYTE* mem) +{ + // TODO +} + +EXPORT_C GPUcursor(int player, int x, int y) +{ + // TODO +} \ No newline at end of file diff --git a/plugins/GSdx/GPU.h b/plugins/GSdx/GPU.h new file mode 100644 index 0000000000..4a2ada7b66 --- /dev/null +++ b/plugins/GSdx/GPU.h @@ -0,0 +1,276 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#pragma pack(push, 1) + +#include "GS.h" + +enum +{ + GPU_POLYGON = 1, + GPU_LINE = 2, + GPU_SPRITE = 3, +}; + +REG32_(GPUReg, STATUS) + UINT32 TX:4; + UINT32 TY:1; + UINT32 ABR:2; + UINT32 TP:2; + UINT32 DTD:1; + UINT32 DFE:1; + UINT32 MD:1; + UINT32 ME:1; + UINT32 _PAD0:3; + UINT32 WIDTH1:1; + UINT32 WIDTH0:2; + UINT32 HEIGHT:1; + UINT32 ISPAL:1; + UINT32 ISRGB24:1; + UINT32 ISINTER:1; + UINT32 DEN:1; + UINT32 _PAD1:2; + UINT32 IDLE:1; + UINT32 IMG:1; + UINT32 COM:1; + UINT32 DMA:2; + UINT32 LCF:1; + /* + UINT32 TX:4; + UINT32 TY:1; + UINT32 ABR:2; + UINT32 TP:2; + UINT32 DTD:1; + UINT32 DFE:1; + UINT32 PBW:1; + UINT32 PBC:1; + UINT32 _PAD0:3; + UINT32 HRES2:1; + UINT32 HRES1:2; + UINT32 VRES:1; + UINT32 ISPAL:1; + UINT32 ISRGB24:1; + UINT32 ISINTER:1; + UINT32 ISSTOP:1; + UINT32 _PAD1:1; + UINT32 DMARDY:1; + UINT32 IDIDLE:1; + UINT32 DATARDY:1; + UINT32 ISEMPTY:1; + UINT32 TMODE:2; + UINT32 ODE:1; + */ +REG_END + +REG32_(GPUReg, PACKET) + UINT32 _PAD:24; + UINT32 OPTION:5; + UINT32 TYPE:3; +REG_END + +REG32_(GPUReg, PRIM) + UINT32 VTX:24; + UINT32 TGE:1; + UINT32 ABE:1; + UINT32 TME:1; + UINT32 _PAD2:1; + UINT32 IIP:1; + UINT32 TYPE:3; +REG_END + +REG32_(GPUReg, POLYGON) + UINT32 _PAD:24; + UINT32 TGE:1; + UINT32 ABE:1; + UINT32 TME:1; + UINT32 VTX:1; + UINT32 IIP:1; + UINT32 TYPE:3; +REG_END + +REG32_(GPUReg, LINE) + UINT32 _PAD:24; + UINT32 ZERO1:1; + UINT32 ABE:1; + UINT32 ZERO2:1; + UINT32 PLL:1; + UINT32 IIP:1; + UINT32 TYPE:3; +REG_END + +REG32_(GPUReg, SPRITE) + UINT32 _PAD:24; + UINT32 ZERO:1; + UINT32 ABE:1; + UINT32 TME:1; + UINT32 SIZE:2; + UINT32 TYPE:3; +REG_END + +REG32_(GPUReg, RESET) + UINT32 _PAD:32; +REG_END + +REG32_(GPUReg, DEN) + UINT32 DEN:1; + UINT32 _PAD:31; +REG_END + +REG32_(GPUReg, DMA) + UINT32 DMA:2; + UINT32 _PAD:30; +REG_END + +REG32_(GPUReg, DAREA) + UINT32 X:10; + UINT32 Y:9; + UINT32 _PAD:13; +REG_END + +REG32_(GPUReg, DHRANGE) + UINT32 X1:12; + UINT32 X2:12; + UINT32 _PAD:8; +REG_END + +REG32_(GPUReg, DVRANGE) + UINT32 Y1:10; + UINT32 Y2:11; + UINT32 _PAD:11; +REG_END + +REG32_(GPUReg, DMODE) + UINT32 WIDTH0:2; + UINT32 HEIGHT:1; + UINT32 ISPAL:1; + UINT32 ISRGB24:1; + UINT32 ISINTER:1; + UINT32 WIDTH1:1; + UINT32 REVERSE:1; + UINT32 _PAD:24; +REG_END + +REG32_(GPUReg, GPUINFO) + UINT32 PARAM:24; + UINT32 _PAD:8; +REG_END + +REG32_(GPUReg, MODE) + UINT32 TX:4; + UINT32 TY:1; + UINT32 ABR:2; + UINT32 TP:2; + UINT32 DTD:1; + UINT32 DFE:1; + UINT32 _PAD:21; +REG_END + +REG32_(GPUReg, MASK) + UINT32 MD:1; + UINT32 ME:1; + UINT32 _PAD:30; +REG_END + +REG32_(GPUReg, DRAREA) + UINT32 X:10; + UINT32 Y:10; + UINT32 _PAD:12; +REG_END + +REG32_(GPUReg, DROFF) + INT32 X:11; + INT32 Y:11; + INT32 _PAD:10; +REG_END + +REG32_(GPUReg, RGB) + UINT32 R:8; + UINT32 G:8; + UINT32 B:8; + UINT32 _PAD:8; +REG_END + +REG32_(GPUReg, XY) + INT32 X:11; + INT32 _PAD1:5; + INT32 Y:11; + INT32 _PAD2:5; +REG_END + +REG32_(GPUReg, UV) + UINT32 U:8; + UINT32 V:8; + UINT32 _PAD:16; +REG_END + +REG32_(GPUReg, TWIN) + UINT32 TWW:5; + UINT32 TWH:5; + UINT32 TWX:5; + UINT32 TWY:5; + UINT32 _PAD:12; +REG_END + +REG32_(GPUReg, CLUT) + UINT32 _PAD1:16; + UINT32 X:6; + UINT32 Y:9; + UINT32 _PAD2:1; +REG_END + +REG32_SET(GPUReg) + GPURegSTATUS STATUS; + GPURegPACKET PACKET; + GPURegPRIM PRIM; + GPURegPOLYGON POLYGON; + GPURegLINE LINE; + GPURegSPRITE SPRITE; + GPURegRESET RESET; + GPURegDEN DEN; + GPURegDMA DMA; + GPURegDAREA DAREA; + GPURegDHRANGE DHRANGE; + GPURegDVRANGE DVRANGE; + GPURegDMODE DMODE; + GPURegGPUINFO GPUINFO; + GPURegMODE MODE; + GPURegMASK MASK; + GPURegDRAREA DRAREA; + GPURegDROFF DROFF; + GPURegRGB RGB; + GPURegXY XY; + GPURegUV UV; + GPURegTWIN TWIN; + GPURegCLUT CLUT; +REG_SET_END + +struct GPUFreezeData +{ + UINT32 version; // == 1 + UINT32 status; + UINT32 control[256]; + UINT16 vram[1024 * 1024]; +}; + +#pragma pack(pop) + diff --git a/plugins/GSdx/GPUDrawScanline.cpp b/plugins/GSdx/GPUDrawScanline.cpp new file mode 100644 index 0000000000..253bcd7653 --- /dev/null +++ b/plugins/GSdx/GPUDrawScanline.cpp @@ -0,0 +1,910 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GPUDrawScanline.h" + +GPUDrawScanline::GPUDrawScanline(GPUState* state, int id) + : m_state(state) + , m_id(id) +{ +} + +GPUDrawScanline::~GPUDrawScanline() +{ +} + +void GPUDrawScanline::BeginDraw(const GSRasterizerData* data, Functions* f) +{ + GPUDrawingEnvironment& env = m_state->m_env; + + const GPUScanlineParam* p = (const GPUScanlineParam*)data->param; + + m_env.sel = p->sel; + + m_env.mem = &m_state->m_mem; + + if(m_env.sel.tme) + { + m_env.tex = p->tex; + m_env.clut = p->clut; + + if(m_env.sel.twin) + { + DWORD u, v; + + u = ~(env.TWIN.TWW << 3) & 0xff; + v = ~(env.TWIN.TWH << 3) & 0xff; + + m_env.u[0] = GSVector4i((u << 16) | u); + m_env.v[0] = GSVector4i((v << 16) | v); + + u = env.TWIN.TWX << 3; + v = env.TWIN.TWY << 3; + + m_env.u[1] = GSVector4i((u << 16) | u) & ~m_env.u[0]; + m_env.v[1] = GSVector4i((v << 16) | v) & ~m_env.v[0]; + } + } + + m_env.a = GSVector4i(env.PRIM.ABE ? 0xffffffff : 0); + m_env.md = GSVector4i(env.STATUS.MD ? 0x80008000 : 0); + + f->sl = m_ds.Lookup(m_env.sel); + + f->sr = NULL; // TODO + + DWORD sel = 0; + + sel |= (data->primclass == GS_SPRITE_CLASS ? 1 : 0) << 0; + sel |= m_env.sel.tme << 1; + sel |= m_env.sel.iip << 2; + + f->sp = m_sp.Lookup(sel); +} + +template +void GPUDrawScanline::SetupPrim(const GSVertexSW* vertices, const GSVertexSW& dscan) +{ + if(m_env.sel.tme && !m_env.sel.twin) + { + if(sprite) + { + GSVector4i t; + + t = (GSVector4i(vertices[1].t) >> 8) - GSVector4i::x00000001(); + t = t.ps32(t); + t = t.upl16(t); + + m_env.u[2] = t.xxxx(); + m_env.v[2] = t.yyyy(); + } + else + { + m_env.u[2] = GSVector4i::x00ff(); + m_env.v[2] = GSVector4i::x00ff(); + } + } + + GSVector4 ps0123 = GSVector4::ps0123(); + GSVector4 ps4567 = GSVector4::ps4567(); + + GSVector4 dt = dscan.t; + GSVector4 dc = dscan.c; + + GSVector4i dtc8 = GSVector4i(dt * 8.0f).ps32(GSVector4i(dc * 8.0f)); + + if(tme) + { + m_env.dst8 = dtc8.upl16(dtc8); + + m_env.ds = GSVector4i(dt.xxxx() * ps0123).ps32(GSVector4i(dt.xxxx() * ps4567)); + m_env.dt = GSVector4i(dt.yyyy() * ps0123).ps32(GSVector4i(dt.yyyy() * ps4567)); + } + + if(iip) + { + m_env.dc8 = dtc8.uph16(dtc8); + + m_env.dr = GSVector4i(dc.xxxx() * ps0123).ps32(GSVector4i(dc.xxxx() * ps4567)); + m_env.dg = GSVector4i(dc.yyyy() * ps0123).ps32(GSVector4i(dc.yyyy() * ps4567)); + m_env.db = GSVector4i(dc.zzzz() * ps0123).ps32(GSVector4i(dc.zzzz() * ps4567)); + } +} +void GPUDrawScanline::SampleTexture(DWORD ltf, DWORD tlu, DWORD twin, GSVector4i& test, const GSVector4i& s, const GSVector4i& t, GSVector4i* c) +{ + const void* RESTRICT tex = m_env.tex; + const WORD* RESTRICT clut = m_env.clut; + + if(ltf) + { + GSVector4i u = s.sub16(GSVector4i(0x00200020)); // - 0.125f + GSVector4i v = t.sub16(GSVector4i(0x00200020)); // - 0.125f + + GSVector4i u0 = u.srl16(8); + GSVector4i v0 = v.srl16(8); + + GSVector4i u1 = u0.add16(GSVector4i::x0001()); + GSVector4i v1 = v0.add16(GSVector4i::x0001()); + + GSVector4i uf = (u & GSVector4i::x00ff()) << 7; + GSVector4i vf = (v & GSVector4i::x00ff()) << 7; + + if(twin) + { + u0 = (u0 & m_env.u[0]).add16(m_env.u[1]); + v0 = (v0 & m_env.v[0]).add16(m_env.v[1]); + u1 = (u1 & m_env.u[0]).add16(m_env.u[1]); + v1 = (v1 & m_env.v[0]).add16(m_env.v[1]); + } + else + { + u0 = u0.min_i16(m_env.u[2]); + v0 = v0.min_i16(m_env.v[2]); + u1 = u1.min_i16(m_env.u[2]); + v1 = v1.min_i16(m_env.v[2]); + } + + GSVector4i addr00 = v0.sll16(8) | u0; + GSVector4i addr01 = v0.sll16(8) | u1; + GSVector4i addr10 = v1.sll16(8) | u0; + GSVector4i addr11 = v1.sll16(8) | u1; + + GSVector4i c00, c01, c10, c11; + + if(tlu) + { + c00 = addr00.gather16_16((const BYTE*)tex, clut); + c01 = addr01.gather16_16((const BYTE*)tex, clut); + c10 = addr10.gather16_16((const BYTE*)tex, clut); + c11 = addr11.gather16_16((const BYTE*)tex, clut); + } + else + { + c00 = addr00.gather16_16((const WORD*)tex); + c01 = addr01.gather16_16((const WORD*)tex); + c10 = addr00.gather16_16((const WORD*)tex); + c11 = addr01.gather16_16((const WORD*)tex); + } + + GSVector4i r00 = (c00 & 0x001f001f) << 3; + GSVector4i r01 = (c01 & 0x001f001f) << 3; + GSVector4i r10 = (c10 & 0x001f001f) << 3; + GSVector4i r11 = (c11 & 0x001f001f) << 3; + + r00 = r00.lerp16<0>(r01, uf); + r10 = r10.lerp16<0>(r11, uf); + c[0] = r00.lerp16<0>(r10, vf); + + GSVector4i g00 = (c00 & 0x03e003e0) >> 2; + GSVector4i g01 = (c01 & 0x03e003e0) >> 2; + GSVector4i g10 = (c10 & 0x03e003e0) >> 2; + GSVector4i g11 = (c11 & 0x03e003e0) >> 2; + + g00 = g00.lerp16<0>(g01, uf); + g10 = g10.lerp16<0>(g11, uf); + c[1] = g00.lerp16<0>(g10, vf); + + GSVector4i b00 = (c00 & 0x7c007c00) >> 7; + GSVector4i b01 = (c01 & 0x7c007c00) >> 7; + GSVector4i b10 = (c10 & 0x7c007c00) >> 7; + GSVector4i b11 = (c11 & 0x7c007c00) >> 7; + + b00 = b00.lerp16<0>(b01, uf); + b10 = b10.lerp16<0>(b11, uf); + c[2] = b00.lerp16<0>(b10, vf); + + GSVector4i a00 = (c00 & 0x80008000) >> 8; + GSVector4i a01 = (c01 & 0x80008000) >> 8; + GSVector4i a10 = (c10 & 0x80008000) >> 8; + GSVector4i a11 = (c11 & 0x80008000) >> 8; + + a00 = a00.lerp16<0>(a01, uf); + a10 = a10.lerp16<0>(a11, uf); + c[3] = a00.lerp16<0>(a10, vf).gt16(GSVector4i::zero()); + + // mask out blank pixels (not perfect) + + test |= + c[0].eq16(GSVector4i::zero()) & + c[1].eq16(GSVector4i::zero()) & + c[2].eq16(GSVector4i::zero()) & + c[3].eq16(GSVector4i::zero()); + } + else + { + GSVector4i u = s.srl16(8); + GSVector4i v = t.srl16(8); + + if(twin) + { + u = (u & m_env.u[0]).add16(m_env.u[1]); + v = (v & m_env.v[0]).add16(m_env.v[1]); + } + else + { + u = u.min_i16(m_env.u[2]); + v = v.min_i16(m_env.v[2]); + } + + GSVector4i addr = v.sll16(8) | u; + + GSVector4i c00; + + if(tlu) + { + c00 = addr.gather16_16((const BYTE*)tex, clut); + } + else + { + c00 = addr.gather16_16((const WORD*)tex); + } + + test |= c00.eq16(GSVector4i::zero()); // mask out blank pixels + + c[0] = (c00 & 0x001f001f) << 3; + c[1] = (c00 & 0x03e003e0) >> 2; + c[2] = (c00 & 0x7c007c00) >> 7; + c[3] = c00.sra16(15); + } +} + +void GPUDrawScanline::ColorTFX(DWORD tfx, const GSVector4i& r, const GSVector4i& g, const GSVector4i& b, GSVector4i* c) +{ + switch(tfx) + { + case 0: // none (tfx = 0) + case 1: // none (tfx = tge) + c[0] = r.srl16(7); + c[1] = g.srl16(7); + c[2] = b.srl16(7); + break; + case 2: // modulate (tfx = tme | tge) + c[0] = c[0].modulate16<1>(r).clamp8(); + c[1] = c[1].modulate16<1>(g).clamp8(); + c[2] = c[2].modulate16<1>(b).clamp8(); + break; + case 3: // decal (tfx = tme) + break; + default: + __assume(0); + } +} + +void GPUDrawScanline::AlphaBlend(UINT32 abr, UINT32 tme, const GSVector4i& d, GSVector4i* c) +{ + GSVector4i r = (d & 0x001f001f) << 3; + GSVector4i g = (d & 0x03e003e0) >> 2; + GSVector4i b = (d & 0x7c007c00) >> 7; + + switch(abr) + { + case 0: + r = r.avg8(c[0]); + g = g.avg8(c[0]); + b = b.avg8(c[0]); + break; + case 1: + r = r.addus8(c[0]); + g = g.addus8(c[1]); + b = b.addus8(c[2]); + break; + case 2: + r = r.subus8(c[0]); + g = g.subus8(c[1]); + b = b.subus8(c[2]); + break; + case 3: + r = r.addus8(c[0].srl16(2)); + g = g.addus8(c[1].srl16(2)); + b = b.addus8(c[2].srl16(2)); + break; + default: + __assume(0); + } + + if(tme) // per pixel + { + c[0] = c[0].blend8(r, c[3]); + c[1] = c[1].blend8(g, c[3]); + c[2] = c[2].blend8(b, c[3]); + } + else + { + c[0] = r; + c[1] = g; + c[2] = b; + c[3] = GSVector4i::zero(); + } +} + +void GPUDrawScanline::WriteFrame(WORD* RESTRICT fb, const GSVector4i& test, const GSVector4i* c, int pixels) +{ + GSVector4i r = (c[0] & 0x00f800f8) >> 3; + GSVector4i g = (c[1] & 0x00f800f8) << 2; + GSVector4i b = (c[2] & 0x00f800f8) << 7; + GSVector4i a = (c[3] & 0x00800080) << 8; + + GSVector4i s = r | g | b | a | m_env.md; + + int i = 0; + + do + { + if(test.u16[i] == 0) + { + fb[i] = s.u16[i]; + } + } + while(++i < pixels); +} + +// + +__declspec(align(16)) static WORD s_dither[4][16] = +{ + {7, 0, 6, 1, 7, 0, 6, 1, 7, 0, 6, 1, 7, 0, 6, 1}, + {2, 5, 3, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 5, 3, 4}, + {1, 6, 0, 7, 1, 6, 0, 7, 1, 6, 0, 7, 1, 6, 0, 7}, + {4, 3, 5, 2, 4, 3, 5, 2, 4, 3, 5, 2, 4, 3, 5, 2}, +}; + +void GPUDrawScanline::DrawScanline(int top, int left, int right, const GSVertexSW& v) +{ + GSVector4i s, t; + GSVector4i r, g, b; + + if(m_env.sel.tme) + { + GSVector4i vt = GSVector4i(v.t).xxzzl(); + + s = vt.xxxx().add16(m_env.ds); + t = vt.yyyy().add16(m_env.dt); + } + + GSVector4i vc = GSVector4i(v.c).xxzzlh(); + + r = vc.xxxx(); + g = vc.yyyy(); + b = vc.zzzz(); + + if(m_env.sel.iip) + { + r = r.add16(m_env.dr); + g = g.add16(m_env.dg); + b = b.add16(m_env.db); + } + + GSVector4i dither; + + if(m_env.sel.dtd) + { + dither = GSVector4i::load(&s_dither[top & 3][left & 3]); + } + + int steps = right - left; + + WORD* fb = m_env.mem->GetPixelAddress(left, top); + + while(1) + { + do + { + int pixels = GSVector4i::min_i16(steps, 8); + + GSVector4i test = GSVector4i::zero(); + + GSVector4i d = GSVector4i::zero(); + + if(m_env.sel.rfb) // me | abe + { + d = GSVector4i::load(fb); + + if(m_env.sel.me) + { + test = d.sra16(15); + + if(test.alltrue()) + { + continue; + } + } + } + + GSVector4i c[4]; + + if(m_env.sel.tme) + { + SampleTexture(m_env.sel.ltf, m_env.sel.tlu, m_env.sel.twin, test, s, t, c); + } + + ColorTFX(m_env.sel.tfx, r, g, b, c); + + if(m_env.sel.abe) + { + AlphaBlend(m_env.sel.abr, m_env.sel.tme, d, c); + } + + if(m_env.sel.dtd) + { + c[0] = c[0].addus8(dither); + c[1] = c[1].addus8(dither); + c[2] = c[2].addus8(dither); + } + + WriteFrame(fb, test, c, pixels); + } + while(0); + + if(steps <= 8) break; + + steps -= 8; + + fb += 8; + + if(m_env.sel.tme) + { + GSVector4i dst8 = m_env.dst8; + + s = s.add16(dst8.xxxx()); + t = t.add16(dst8.yyyy()); + } + + if(m_env.sel.iip) + { + GSVector4i dc8 = m_env.dc8; + + r = r.add16(dc8.xxxx()); + g = g.add16(dc8.yyyy()); + b = b.add16(dc8.zzzz()); + } + } +} + +template +void GPUDrawScanline::DrawScanlineEx(int top, int left, int right, const GSVertexSW& v) +{ + DWORD iip = (sel >> 0) & 1; + DWORD me = (sel >> 1) & 1; + DWORD abe = (sel >> 2) & 1; + DWORD abr = (sel >> 3) & 3; + // DWORD tge = (sel >> 5) & 1; + DWORD tme = (sel >> 6) & 1; + DWORD twin = (sel >> 7) & 1; + DWORD rfb = (sel >> 1) & 3; + DWORD tfx = (sel >> 5) & 3; + + GSVector4i s, t; + GSVector4i r, g, b; + + if(tme) + { + GSVector4i vt = GSVector4i(v.t).xxzzl(); + + s = vt.xxxx().add16(m_env.ds); + t = vt.yyyy().add16(m_env.dt); + } + + GSVector4i vc = GSVector4i(v.c).xxzzlh(); + + r = vc.xxxx(); + g = vc.yyyy(); + b = vc.zzzz(); + + if(iip) + { + r = r.add16(m_env.dr); + g = g.add16(m_env.dg); + b = b.add16(m_env.db); + } + + GSVector4i dither; + + if(m_env.sel.dtd) + { + dither = GSVector4i::load(&s_dither[top & 3][left & 3]); + } + + int steps = right - left; + + WORD* fb = m_env.mem->GetPixelAddress(left, top); + + while(1) + { + do + { + int pixels = GSVector4i::min_i16(steps, 8); + + GSVector4i test = GSVector4i::zero(); + + GSVector4i d = GSVector4i::zero(); + + if(rfb) // me | abe + { + d = GSVector4i::load(fb); + + if(me) + { + test = d.sra16(15); + + if(test.alltrue()) + { + continue; + } + } + } + + GSVector4i c[4]; + + if(tme) + { + SampleTexture(m_env.sel.ltf, m_env.sel.tlu, twin, test, s, t, c); + } + + ColorTFX(tfx, r, g, b, c); + + if(abe) + { + AlphaBlend(abr, tme, d, c); + } + + if(m_env.sel.dtd) + { + c[0] = c[0].addus8(dither); + c[1] = c[1].addus8(dither); + c[2] = c[2].addus8(dither); + } + + WriteFrame(fb, test, c, pixels); + } + while(0); + + if(steps <= 8) break; + + steps -= 8; + + fb += 8; + + if(tme) + { + GSVector4i dst8 = m_env.dst8; + + s = s.add16(dst8.xxxx()); + t = t.add16(dst8.yyyy()); + } + + if(iip) + { + GSVector4i dc8 = m_env.dc8; + + r = r.add16(dc8.xxxx()); + g = g.add16(dc8.yyyy()); + b = b.add16(dc8.zzzz()); + } + } +} + +GPUDrawScanline::GPUDrawScanlineMap::GPUDrawScanlineMap() +{ + for(int i = 0; i < countof(m_default); i++) + { + m_default[i] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanline; + } + + #ifdef FAST_DRAWSCANLINE + + m_default[0x00] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x00>; + m_default[0x01] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x01>; + m_default[0x02] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x02>; + m_default[0x03] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x03>; + m_default[0x04] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x04>; + m_default[0x05] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x05>; + m_default[0x06] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x06>; + m_default[0x07] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x07>; + m_default[0x08] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x08>; + m_default[0x09] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x09>; + m_default[0x0a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0a>; + m_default[0x0b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0b>; + m_default[0x0c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0c>; + m_default[0x0d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0d>; + m_default[0x0e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0e>; + m_default[0x0f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x0f>; + m_default[0x10] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x10>; + m_default[0x11] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x11>; + m_default[0x12] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x12>; + m_default[0x13] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x13>; + m_default[0x14] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x14>; + m_default[0x15] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x15>; + m_default[0x16] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x16>; + m_default[0x17] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x17>; + m_default[0x18] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x18>; + m_default[0x19] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x19>; + m_default[0x1a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1a>; + m_default[0x1b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1b>; + m_default[0x1c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1c>; + m_default[0x1d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1d>; + m_default[0x1e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1e>; + m_default[0x1f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x1f>; + m_default[0x20] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x20>; + m_default[0x21] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x21>; + m_default[0x22] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x22>; + m_default[0x23] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x23>; + m_default[0x24] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x24>; + m_default[0x25] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x25>; + m_default[0x26] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x26>; + m_default[0x27] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x27>; + m_default[0x28] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x28>; + m_default[0x29] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x29>; + m_default[0x2a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2a>; + m_default[0x2b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2b>; + m_default[0x2c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2c>; + m_default[0x2d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2d>; + m_default[0x2e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2e>; + m_default[0x2f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x2f>; + m_default[0x30] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x30>; + m_default[0x31] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x31>; + m_default[0x32] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x32>; + m_default[0x33] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x33>; + m_default[0x34] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x34>; + m_default[0x35] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x35>; + m_default[0x36] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x36>; + m_default[0x37] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x37>; + m_default[0x38] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x38>; + m_default[0x39] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x39>; + m_default[0x3a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3a>; + m_default[0x3b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3b>; + m_default[0x3c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3c>; + m_default[0x3d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3d>; + m_default[0x3e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3e>; + m_default[0x3f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x3f>; + m_default[0x40] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x40>; + m_default[0x41] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x41>; + m_default[0x42] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x42>; + m_default[0x43] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x43>; + m_default[0x44] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x44>; + m_default[0x45] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x45>; + m_default[0x46] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x46>; + m_default[0x47] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x47>; + m_default[0x48] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x48>; + m_default[0x49] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x49>; + m_default[0x4a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4a>; + m_default[0x4b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4b>; + m_default[0x4c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4c>; + m_default[0x4d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4d>; + m_default[0x4e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4e>; + m_default[0x4f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x4f>; + m_default[0x50] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x50>; + m_default[0x51] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x51>; + m_default[0x52] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x52>; + m_default[0x53] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x53>; + m_default[0x54] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x54>; + m_default[0x55] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x55>; + m_default[0x56] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x56>; + m_default[0x57] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x57>; + m_default[0x58] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x58>; + m_default[0x59] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x59>; + m_default[0x5a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5a>; + m_default[0x5b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5b>; + m_default[0x5c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5c>; + m_default[0x5d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5d>; + m_default[0x5e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5e>; + m_default[0x5f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x5f>; + m_default[0x60] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x60>; + m_default[0x61] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x61>; + m_default[0x62] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x62>; + m_default[0x63] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x63>; + m_default[0x64] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x64>; + m_default[0x65] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x65>; + m_default[0x66] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x66>; + m_default[0x67] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x67>; + m_default[0x68] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x68>; + m_default[0x69] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x69>; + m_default[0x6a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6a>; + m_default[0x6b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6b>; + m_default[0x6c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6c>; + m_default[0x6d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6d>; + m_default[0x6e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6e>; + m_default[0x6f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x6f>; + m_default[0x70] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x70>; + m_default[0x71] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x71>; + m_default[0x72] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x72>; + m_default[0x73] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x73>; + m_default[0x74] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x74>; + m_default[0x75] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x75>; + m_default[0x76] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x76>; + m_default[0x77] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x77>; + m_default[0x78] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x78>; + m_default[0x79] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x79>; + m_default[0x7a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7a>; + m_default[0x7b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7b>; + m_default[0x7c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7c>; + m_default[0x7d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7d>; + m_default[0x7e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7e>; + m_default[0x7f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x7f>; + m_default[0x80] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x80>; + m_default[0x81] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x81>; + m_default[0x82] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x82>; + m_default[0x83] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x83>; + m_default[0x84] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x84>; + m_default[0x85] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x85>; + m_default[0x86] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x86>; + m_default[0x87] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x87>; + m_default[0x88] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x88>; + m_default[0x89] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x89>; + m_default[0x8a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8a>; + m_default[0x8b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8b>; + m_default[0x8c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8c>; + m_default[0x8d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8d>; + m_default[0x8e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8e>; + m_default[0x8f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x8f>; + m_default[0x90] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x90>; + m_default[0x91] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x91>; + m_default[0x92] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x92>; + m_default[0x93] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x93>; + m_default[0x94] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x94>; + m_default[0x95] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x95>; + m_default[0x96] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x96>; + m_default[0x97] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x97>; + m_default[0x98] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x98>; + m_default[0x99] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x99>; + m_default[0x9a] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9a>; + m_default[0x9b] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9b>; + m_default[0x9c] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9c>; + m_default[0x9d] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9d>; + m_default[0x9e] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9e>; + m_default[0x9f] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0x9f>; + m_default[0xa0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa0>; + m_default[0xa1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa1>; + m_default[0xa2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa2>; + m_default[0xa3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa3>; + m_default[0xa4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa4>; + m_default[0xa5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa5>; + m_default[0xa6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa6>; + m_default[0xa7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa7>; + m_default[0xa8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa8>; + m_default[0xa9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xa9>; + m_default[0xaa] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xaa>; + m_default[0xab] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xab>; + m_default[0xac] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xac>; + m_default[0xad] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xad>; + m_default[0xae] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xae>; + m_default[0xaf] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xaf>; + m_default[0xb0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb0>; + m_default[0xb1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb1>; + m_default[0xb2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb2>; + m_default[0xb3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb3>; + m_default[0xb4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb4>; + m_default[0xb5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb5>; + m_default[0xb6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb6>; + m_default[0xb7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb7>; + m_default[0xb8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb8>; + m_default[0xb9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xb9>; + m_default[0xba] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xba>; + m_default[0xbb] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xbb>; + m_default[0xbc] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xbc>; + m_default[0xbd] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xbd>; + m_default[0xbe] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xbe>; + m_default[0xbf] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xbf>; + m_default[0xc0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc0>; + m_default[0xc1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc1>; + m_default[0xc2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc2>; + m_default[0xc3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc3>; + m_default[0xc4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc4>; + m_default[0xc5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc5>; + m_default[0xc6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc6>; + m_default[0xc7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc7>; + m_default[0xc8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc8>; + m_default[0xc9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xc9>; + m_default[0xca] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xca>; + m_default[0xcb] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xcb>; + m_default[0xcc] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xcc>; + m_default[0xcd] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xcd>; + m_default[0xce] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xce>; + m_default[0xcf] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xcf>; + m_default[0xd0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd0>; + m_default[0xd1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd1>; + m_default[0xd2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd2>; + m_default[0xd3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd3>; + m_default[0xd4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd4>; + m_default[0xd5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd5>; + m_default[0xd6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd6>; + m_default[0xd7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd7>; + m_default[0xd8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd8>; + m_default[0xd9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xd9>; + m_default[0xda] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xda>; + m_default[0xdb] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xdb>; + m_default[0xdc] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xdc>; + m_default[0xdd] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xdd>; + m_default[0xde] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xde>; + m_default[0xdf] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xdf>; + m_default[0xe0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe0>; + m_default[0xe1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe1>; + m_default[0xe2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe2>; + m_default[0xe3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe3>; + m_default[0xe4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe4>; + m_default[0xe5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe5>; + m_default[0xe6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe6>; + m_default[0xe7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe7>; + m_default[0xe8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe8>; + m_default[0xe9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xe9>; + m_default[0xea] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xea>; + m_default[0xeb] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xeb>; + m_default[0xec] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xec>; + m_default[0xed] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xed>; + m_default[0xee] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xee>; + m_default[0xef] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xef>; + m_default[0xf0] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf0>; + m_default[0xf1] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf1>; + m_default[0xf2] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf2>; + m_default[0xf3] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf3>; + m_default[0xf4] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf4>; + m_default[0xf5] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf5>; + m_default[0xf6] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf6>; + m_default[0xf7] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf7>; + m_default[0xf8] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf8>; + m_default[0xf9] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xf9>; + m_default[0xfa] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xfa>; + m_default[0xfb] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xfb>; + m_default[0xfc] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xfc>; + m_default[0xfd] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xfd>; + m_default[0xfe] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xfe>; + m_default[0xff] = (DrawScanlinePtr)&GPUDrawScanline::DrawScanlineEx<0xff>; + + #endif +} + +IDrawScanline::DrawScanlinePtr GPUDrawScanline::GPUDrawScanlineMap::GetDefaultFunction(DWORD dw) +{ + GPUScanlineSelector sel; + + sel.dw = dw; + + return m_default[sel]; +} + +// + +GPUDrawScanline::GPUSetupPrimMap::GPUSetupPrimMap() +{ + #define InitSP_IIP(sprite, tme, iip) \ + m_default[sprite][tme][iip] = (SetupPrimPtr)&GPUDrawScanline::SetupPrim; \ + + #define InitSP_TME(sprite, tme) \ + InitSP_IIP(sprite, tme, 0) \ + InitSP_IIP(sprite, tme, 1) \ + + #define InitSP_SPRITE(sprite) \ + InitSP_TME(sprite, 0) \ + InitSP_TME(sprite, 1) \ + + InitSP_SPRITE(0); + InitSP_SPRITE(1); +} + +IDrawScanline::SetupPrimPtr GPUDrawScanline::GPUSetupPrimMap::GetDefaultFunction(DWORD dw) +{ + DWORD sprite = (dw >> 0) & 1; + DWORD tme = (dw >> 1) & 1; + DWORD iip = (dw >> 2) & 1; + + return m_default[sprite][tme][iip]; +} + diff --git a/plugins/GSdx/GPUDrawScanline.h b/plugins/GSdx/GPUDrawScanline.h new file mode 100644 index 0000000000..87bb3228e0 --- /dev/null +++ b/plugins/GSdx/GPUDrawScanline.h @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPUState.h" +#include "GSRasterizer.h" +#include "GSAlignedClass.h" + +union GPUScanlineSelector +{ + struct + { + DWORD iip:1; // 0 + DWORD me:1; // 1 + DWORD abe:1; // 2 + DWORD abr:2; // 3 + DWORD tge:1; // 5 + DWORD tme:1; // 6 + DWORD twin:1; // 7 + DWORD tlu:1; // 8 + DWORD dtd:1; // 9 + DWORD ltf:1; // 10 + // DWORD dte:1: // 11 + }; + + struct + { + DWORD _pad1:1; // 0 + DWORD rfb:2; // 1 + DWORD _pad2:2; // 3 + DWORD tfx:2; // 5 + }; + + DWORD dw; + + operator DWORD() {return dw & 0xff;} +}; + +__declspec(align(16)) struct GPUScanlineEnvironment +{ + GPUScanlineSelector sel; + + GPULocalMemory* mem; + const void* tex; + const WORD* clut; + + GSVector4i u[3]; + GSVector4i v[3]; + + GSVector4i a; + GSVector4i md; // similar to gs fba + + GSVector4i ds, dt, dst8; + GSVector4i dr, dg, db, dc8; +}; + +__declspec(align(16)) struct GPUScanlineParam +{ + GPUScanlineSelector sel; + + const void* tex; + const WORD* clut; +}; + +class GPUDrawScanline : public GSAlignedClass<16>, public IDrawScanline +{ + GPUScanlineEnvironment m_env; + + // + + class GPUDrawScanlineMap : public GSFunctionMap + { + DrawScanlinePtr m_default[256]; + + public: + GPUDrawScanlineMap(); + + DrawScanlinePtr GetDefaultFunction(DWORD dw); + }; + + GPUDrawScanlineMap m_ds; + + // + + class GPUSetupPrimMap : public GSFunctionMap + { + SetupPrimPtr m_default[2][2][2]; + + public: + GPUSetupPrimMap(); + + SetupPrimPtr GetDefaultFunction(DWORD dw); + }; + + GPUSetupPrimMap m_sp; + + // + + template + void SetupPrim(const GSVertexSW* vertices, const GSVertexSW& dscan); + + // + + __forceinline void SampleTexture(DWORD ltf, DWORD tlu, DWORD twin, GSVector4i& test, const GSVector4i& s, const GSVector4i& t, GSVector4i* c); + __forceinline void ColorTFX(DWORD tfx, const GSVector4i& r, const GSVector4i& g, const GSVector4i& b, GSVector4i* c); + __forceinline void AlphaBlend(UINT32 abr, UINT32 tme, const GSVector4i& d, GSVector4i* c); + __forceinline void WriteFrame(WORD* RESTRICT fb, const GSVector4i& test, const GSVector4i* c, int pixels); + + void DrawScanline(int top, int left, int right, const GSVertexSW& v); + + template + void DrawScanlineEx(int top, int left, int right, const GSVertexSW& v); + +protected: + GPUState* m_state; + int m_id; + +public: + GPUDrawScanline(GPUState* state, int id); + virtual ~GPUDrawScanline(); + + // IDrawScanline + + void BeginDraw(const GSRasterizerData* data, Functions* f); + void EndDraw(const GSRasterizerStats& stats) {} + void PrintStats() {} +}; diff --git a/plugins/GSdx/GPUDrawingEnvironment.h b/plugins/GSdx/GPUDrawingEnvironment.h new file mode 100644 index 0000000000..2a5cd1b6e5 --- /dev/null +++ b/plugins/GSdx/GPUDrawingEnvironment.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPU.h" + +#pragma pack(push, 1) + +__declspec(align(16)) class GPUDrawingEnvironment +{ +public: + GPURegSTATUS STATUS; + GPURegPRIM PRIM; + GPURegDAREA DAREA; + GPURegDHRANGE DHRANGE; + GPURegDVRANGE DVRANGE; + GPURegDRAREA DRAREATL; + GPURegDRAREA DRAREABR; + GPURegDROFF DROFF; + GPURegTWIN TWIN; + GPURegCLUT CLUT; + + GPUDrawingEnvironment() + { + Reset(); + } + + void Reset() + { + memset(this, 0, sizeof(*this)); + + STATUS.IDLE = 1; + STATUS.COM = 1; + STATUS.WIDTH0 = 1; + DVRANGE.Y1 = 16; + DVRANGE.Y2 = 256; + } + + CRect GetDisplayRect() + { + static int s_width[] = {256, 320, 512, 640, 368, 384, 512, 640}; + static int s_height[] = {240, 480}; + + CRect r; + + r.left = DAREA.X & ~7; // FIXME + r.top = DAREA.Y; + r.right = r.left + s_width[(STATUS.WIDTH1 << 2) | STATUS.WIDTH0]; + r.bottom = r.top + (DVRANGE.Y2 - DVRANGE.Y1) * s_height[STATUS.HEIGHT] / 240; + + r &= CRect(0, 0, 1024, 512); + + return r; + } + + int GetFPS() + { + return STATUS.ISPAL ? 50 : 60; + } +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GPULocalMemory.cpp b/plugins/GSdx/GPULocalMemory.cpp new file mode 100644 index 0000000000..b2d718e766 --- /dev/null +++ b/plugins/GSdx/GPULocalMemory.cpp @@ -0,0 +1,666 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GPULocalMemory.h" + +const GSVector4i GPULocalMemory::m_xxxa(0x00008000); +const GSVector4i GPULocalMemory::m_xxbx(0x00007c00); +const GSVector4i GPULocalMemory::m_xgxx(0x000003e0); +const GSVector4i GPULocalMemory::m_rxxx(0x0000001f); + +GPULocalMemory::GPULocalMemory(const CSize& scale) +{ + m_scale.cx = min(max(scale.cx, 0), 2); + m_scale.cy = min(max(scale.cy, 0), 2); + + // + + int size = (1 << (12 + 11)) * sizeof(WORD); + + m_vm = (WORD*)VirtualAlloc(NULL, size * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + + memset(m_vm, 0, size); + + // + + m_clut.buff = m_vm + size; + m_clut.dirty = true; + + // + + size = 256 * 256 * (1 + 1 + 4) * 32; + + m_texture.buff[0] = (BYTE*)VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + m_texture.buff[1] = m_texture.buff[0] + 256 * 256 * 32; + m_texture.buff[2] = m_texture.buff[1] + 256 * 256 * 32; + + memset(m_texture.buff[0], 0, size); + + memset(m_texture.valid, 0, sizeof(m_texture.valid)); + + for(int y = 0, offset = 0; y < 2; y++) + { + for(int x = 0; x < 16; x++, offset += 256 * 256) + { + m_texture.page[0][y][x] = &((BYTE*)m_texture.buff[0])[offset]; + m_texture.page[1][y][x] = &((BYTE*)m_texture.buff[1])[offset]; + } + } + + for(int y = 0, offset = 0; y < 2; y++) + { + for(int x = 0; x < 16; x++, offset += 256 * 256) + { + m_texture.page[2][y][x] = &((DWORD*)m_texture.buff[2])[offset]; + } + } +} + +GPULocalMemory::~GPULocalMemory() +{ + VirtualFree(m_vm, 0, MEM_RELEASE); + + VirtualFree(m_texture.buff[0], 0, MEM_RELEASE); +} + +const WORD* GPULocalMemory::GetCLUT(int tp, int cx, int cy) +{ + if(m_clut.dirty || m_clut.tp != tp || m_clut.cx != cx || m_clut.cy != cy) + { + WORD* src = GetPixelAddressScaled(cx << 4, cy); + WORD* dst = m_clut.buff; + + // TODO: at normal horizontal resolution just return src + + if(m_scale.cx == 0) + { + memcpy(dst, src, (tp == 0 ? 16 : 256) * 2); + } + else if(m_scale.cx == 1) + { + if(tp == 0) + { + for(int i = 0; i < 16; i++) + { + dst[i] = src[i * 2]; + } + } + else if(tp == 1) + { + for(int i = 0; i < 256; i++) + { + dst[i] = src[i * 2]; + } + } + } + else if(m_scale.cx == 2) + { + if(tp == 0) + { + for(int i = 0; i < 16; i++) + { + dst[i] = src[i * 4]; + } + } + else if(tp == 1) + { + for(int i = 0; i < 256; i++) + { + dst[i] = src[i * 4]; + } + } + } + else + { + ASSERT(0); + } + + m_clut.tp = tp; + m_clut.cx = cx; + m_clut.cy = cy; + m_clut.dirty = false; + } + + return m_clut.buff; +} + +const void* GPULocalMemory::GetTexture(int tp, int tx, int ty) +{ + if(tp == 3) + { + ASSERT(0); + + return NULL; + } + + void* buff = m_texture.page[tp][ty][tx]; + + UINT32 flag = 1 << tx; + + if((m_texture.valid[tp][ty] & flag) == 0) + { + int bpp = 0; + + switch(tp) + { + case 0: + ReadPage4(tx, ty, (BYTE*)buff); + bpp = 4; + break; + case 1: + ReadPage8(tx, ty, (BYTE*)buff); + bpp = 8; + break; + case 2: + case 3: + ReadPage16(tx, ty, (WORD*)buff); + bpp = 16; + default: + // FIXME: __assume(0); // vc9 generates bogus code in release mode + break; + } + + // TODO: m_state->m_perfmon.Put(GSPerfMon::Unswizzle, 256 * 256 * bpp >> 3); + + m_texture.valid[tp][ty] |= flag; + } + + return buff; +} + +void GPULocalMemory::Invalidate(const CRect& r) +{ + if(!m_clut.dirty) + { + if(r.top <= m_clut.cy && m_clut.cy < r.bottom) + { + int left = m_clut.cx << 4; + int right = left + (m_clut.tp == 0 ? 16 : 256); + + if(r.left < right && r.right > left) + { + m_clut.dirty = true; + } + } + } + + for(int y = 0, ye = min(r.bottom, 512), j = 0; y < ye; y += 256, j++) + { + if(r.top >= y + 256) continue; + + for(int x = 0, xe = min(r.right, 1024), i = 0; x < xe; x += 64, i++) + { + DWORD flag = 1 << i; + + if(r.left >= x + 256) continue; + + m_texture.valid[2][j] &= ~flag; + + if(r.left >= x + 128) continue; + + m_texture.valid[1][j] &= ~flag; + + if(r.left >= x + 64) continue; + + m_texture.valid[0][j] &= ~flag; + } + } +} + +void GPULocalMemory::FillRect(const CRect& r, WORD c) +{ + Invalidate(r); + + WORD* RESTRICT dst = GetPixelAddressScaled(r.left, r.top); + + int w = r.Width() << m_scale.cx; + int h = r.Height() << m_scale.cy; + + int pitch = GetWidth(); + + for(int j = 0; j < h; j++, dst += pitch) + { + for(int i = 0; i < w; i++) + { + dst[i] = c; + } + } +} + +void GPULocalMemory::WriteRect(const CRect& r, const WORD* RESTRICT src) +{ + Invalidate(r); + + WORD* RESTRICT dst = GetPixelAddressScaled(r.left, r.top); + + int w = r.Width(); + int h = r.Height(); + + int pitch = GetWidth(); + + if(m_scale.cx == 0) + { + for(int j = 0; j < h; j++, src += w) + { + for(int k = 1 << m_scale.cy; k >= 1; k--, dst += pitch) + { + memcpy(dst, src, w * 2); + } + } + } + else if(m_scale.cx == 1) + { + for(int j = 0; j < h; j++, src += w) + { + for(int k = 1 << m_scale.cy; k >= 1; k--, dst += pitch) + { + for(int i = 0; i < w; i++) + { + dst[i * 2 + 0] = src[i]; + dst[i * 2 + 1] = src[i]; + } + } + } + } + else if(m_scale.cx == 2) + { + for(int j = 0; j < h; j++, src += w) + { + for(int k = 1 << m_scale.cy; k >= 1; k--, dst += pitch) + { + for(int i = 0; i < w; i++) + { + dst[i * 4 + 0] = src[i]; + dst[i * 4 + 1] = src[i]; + dst[i * 4 + 2] = src[i]; + dst[i * 4 + 3] = src[i]; + } + } + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::ReadRect(const CRect& r, WORD* RESTRICT dst) +{ + WORD* RESTRICT src = GetPixelAddressScaled(r.left, r.top); + + int w = r.Width(); + int h = r.Height(); + + int pitch = GetWidth() << m_scale.cy; + + if(m_scale.cx == 0) + { + for(int j = 0; j < h; j++, src += pitch, dst += w) + { + memcpy(dst, src, w * 2); + } + } + else if(m_scale.cx == 1) + { + for(int j = 0; j < h; j++, src += pitch, dst += w) + { + for(int i = 0; i < w; i++) + { + dst[i] = src[i * 2]; + } + } + } + else if(m_scale.cx == 2) + { + for(int j = 0; j < h; j++, src += pitch, dst += w) + { + for(int i = 0; i < w; i++) + { + dst[i] = src[i * 4]; + } + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::MoveRect(const CPoint& src, const CPoint& dst, int w, int h) +{ + Invalidate(CRect(dst, CSize(w, h))); + + WORD* s = GetPixelAddressScaled(src.x, src.y); + WORD* d = GetPixelAddressScaled(dst.x, dst.y); + + w <<= m_scale.cx; + h <<= m_scale.cy; + + int pitch = GetWidth(); + + for(int i = 0; i < h; i++, s += pitch, d += pitch) + { + memcpy(d, s, w * sizeof(WORD)); + } +} + +void GPULocalMemory::ReadPage4(int tx, int ty, BYTE* RESTRICT dst) +{ + WORD* src = GetPixelAddressScaled(tx << 6, ty << 8); + + int pitch = GetWidth() << m_scale.cy; + + if(m_scale.cx == 0) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 64; i++) + { + dst[i * 4 + 0] = (src[i] >> 0) & 0xf; + dst[i * 4 + 1] = (src[i] >> 4) & 0xf; + dst[i * 4 + 2] = (src[i] >> 8) & 0xf; + dst[i * 4 + 3] = (src[i] >> 12) & 0xf; + } + } + } + else if(m_scale.cx == 1) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 64; i++) + { + dst[i * 4 + 0] = (src[i * 2] >> 0) & 0xf; + dst[i * 4 + 1] = (src[i * 2] >> 4) & 0xf; + dst[i * 4 + 2] = (src[i * 2] >> 8) & 0xf; + dst[i * 4 + 3] = (src[i * 2] >> 12) & 0xf; + } + } + } + else if(m_scale.cx == 2) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 64; i++) + { + dst[i * 4 + 0] = (src[i * 4] >> 0) & 0xf; + dst[i * 4 + 1] = (src[i * 4] >> 4) & 0xf; + dst[i * 4 + 2] = (src[i * 4] >> 8) & 0xf; + dst[i * 4 + 3] = (src[i * 4] >> 12) & 0xf; + } + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::ReadPage8(int tx, int ty, BYTE* RESTRICT dst) +{ + WORD* src = GetPixelAddressScaled(tx << 6, ty << 8); + + int pitch = GetWidth() << m_scale.cy; + + if(m_scale.cx == 0) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + memcpy(dst, src, 256); + } + } + else if(m_scale.cx == 1) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 128; i++) + { + ((WORD*)dst)[i] = src[i * 2]; + } + } + } + else if(m_scale.cx == 2) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 128; i++) + { + ((WORD*)dst)[i] = src[i * 4]; + } + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::ReadPage16(int tx, int ty, WORD* RESTRICT dst) +{ + WORD* src = GetPixelAddressScaled(tx << 6, ty << 8); + + int pitch = GetWidth() << m_scale.cy; + + if(m_scale.cx == 0) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + memcpy(dst, src, 512); + } + } + else if(m_scale.cx == 1) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 256; i++) + { + dst[i] = src[i * 2]; + } + } + } + else if(m_scale.cx == 2) + { + for(int j = 0; j < 256; j++, src += pitch, dst += 256) + { + for(int i = 0; i < 256; i++) + { + dst[i] = src[i * 4]; + } + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::ReadFrame32(const CRect& r, DWORD* RESTRICT dst, bool rgb24) +{ + WORD* src = GetPixelAddress(r.left, r.top); + + int pitch = GetWidth(); + + if(rgb24) + { + for(int i = r.top; i < r.bottom; i++, src += pitch, dst += pitch) + { + Expand24(src, dst, r.Width()); + } + } + else + { + for(int i = r.top; i < r.bottom; i++, src += pitch, dst += pitch) + { + Expand16(src, dst, r.Width()); + } + } +} + +void GPULocalMemory::Expand16(const WORD* RESTRICT src, DWORD* RESTRICT dst, int pixels) +{ + GSVector4i rm = m_rxxx; + GSVector4i gm = m_xgxx; + GSVector4i bm = m_xxbx; + GSVector4i am = m_xxxa; + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + for(int i = 0, j = pixels >> 3; i < j; i++) + { + GSVector4i c = s[i]; + + GSVector4i l = c.upl16(); + GSVector4i h = c.uph16(); + + d[i * 2 + 0] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | ((l & am) << 16); + d[i * 2 + 1] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | ((h & am) << 16); + } +} + +void GPULocalMemory::Expand24(const WORD* RESTRICT src, DWORD* RESTRICT dst, int pixels) +{ + BYTE* s = (BYTE*)src; + + if(m_scale.cx == 0) + { + for(int i = 0; i < pixels; i += 2, s += 6) + { + dst[i + 0] = (s[2] << 16) | (s[1] << 8) | s[0]; + dst[i + 1] = (s[5] << 16) | (s[4] << 8) | s[3]; + } + } + else if(m_scale.cx == 1) + { + for(int i = 0; i < pixels; i += 4, s += 12) + { + dst[i + 0] = dst[i + 1] = (s[4] << 16) | (s[1] << 8) | s[0]; + dst[i + 2] = dst[i + 3] = (s[9] << 16) | (s[8] << 8) | s[5]; + } + } + else if(m_scale.cx == 2) + { + for(int i = 0; i < pixels; i += 8, s += 24) + { + dst[i + 0] = dst[i + 1] = dst[i + 2] = dst[i + 3] = (s[8] << 16) | (s[1] << 8) | s[0]; + dst[i + 4] = dst[i + 5] = dst[i + 6] = dst[i + 7] = (s[17] << 16) | (s[16] << 8) | s[9]; + } + } + else + { + ASSERT(0); + } +} + +void GPULocalMemory::SaveBMP(LPCTSTR path, CRect r, int tp, int cx, int cy) +{ + r.left <<= m_scale.cx; + r.top <<= m_scale.cy; + r.right <<= m_scale.cx; + r.bottom <<= m_scale.cy; + + r.left &= ~1; + r.right &= ~1; + + if(FILE* fp = _tfopen(path, _T("wb"))) + { + BITMAPINFOHEADER bih; + memset(&bih, 0, sizeof(bih)); + bih.biSize = sizeof(bih); + bih.biWidth = r.Width(); + bih.biHeight = r.Height(); + bih.biPlanes = 1; + bih.biBitCount = 32; + bih.biCompression = BI_RGB; + bih.biSizeImage = bih.biWidth * bih.biHeight * 4; + + BITMAPFILEHEADER bfh; + memset(&bfh, 0, sizeof(bfh)); + bfh.bfType = 'MB'; + bfh.bfOffBits = sizeof(bfh) + sizeof(bih); + bfh.bfSize = bfh.bfOffBits + bih.biSizeImage; + bfh.bfReserved1 = bfh.bfReserved2 = 0; + + fwrite(&bfh, 1, sizeof(bfh), fp); + fwrite(&bih, 1, sizeof(bih), fp); + + int pitch = GetWidth(); + + WORD* buff = (WORD*)_aligned_malloc(pitch * sizeof(WORD), 16); + DWORD* buff32 = (DWORD*)_aligned_malloc(pitch * sizeof(DWORD), 16); + WORD* src = GetPixelAddress(r.left, r.bottom - 1); + const WORD* clut = GetCLUT(tp, cx, cy); + + for(int j = r.bottom - 1; j >= r.top; j--, src -= pitch) + { + switch(tp) + { + case 0: // 4 bpp + + for(int i = 0, k = r.Width() / 2; i < k; i++) + { + buff[i * 2 + 0] = clut[((BYTE*)src)[i] & 0xf]; + buff[i * 2 + 1] = clut[((BYTE*)src)[i] >> 4]; + } + + break; + + case 1: // 8 bpp + + for(int i = 0, k = r.Width(); i < k; i++) + { + buff[i] = clut[((BYTE*)src)[i]]; + } + + break; + + case 2: // 16 bpp; + + for(int i = 0, k = r.Width(); i < k; i++) + { + buff[i] = src[i]; + } + + break; + + case 3: // 24 bpp + + // TODO + + break; + } + + Expand16(buff, buff32, r.Width()); + + for(int i = 0, k = r.Width(); i < k; i++) + { + buff32[i] = (buff32[i] & 0xff00ff00) | ((buff32[i] & 0x00ff0000) >> 16) | ((buff32[i] & 0x000000ff) << 16); + } + + fwrite(buff32, 1, r.Width() * 4, fp); + } + + _aligned_free(buff); + _aligned_free(buff32); + + fclose(fp); + } +} diff --git a/plugins/GSdx/GPULocalMemory.h b/plugins/GSdx/GPULocalMemory.h new file mode 100644 index 0000000000..85e90155a1 --- /dev/null +++ b/plugins/GSdx/GPULocalMemory.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPU.h" +#include "GSVector.h" + +class GPULocalMemory +{ + static const GSVector4i m_xxxa; + static const GSVector4i m_xxbx; + static const GSVector4i m_xgxx; + static const GSVector4i m_rxxx; + + WORD* m_vm; + + struct + { + WORD* buff; + int tp, cx, cy; + bool dirty; + } m_clut; + + struct + { + BYTE* buff[3]; + void* page[3][2][16]; + WORD valid[3][2]; + } m_texture; + + CSize m_scale; + +public: + GPULocalMemory(const CSize& scale); + virtual ~GPULocalMemory(); + + CSize GetScale() {return m_scale;} + int GetWidth() {return 1 << (10 + m_scale.cx);} + int GetHeight() {return 1 << (9 + m_scale.cy);} + + WORD* GetPixelAddress(int x, int y) const {return &m_vm[(y << (10 + m_scale.cx)) + x];} + WORD* GetPixelAddressScaled(int x, int y) const {return &m_vm[((y << m_scale.cy) << (10 + m_scale.cx)) + (x << m_scale.cx)];} + + const WORD* GetCLUT(int tp, int cx, int cy); + const void* GetTexture(int tp, int tx, int ty); + + void Invalidate(const CRect& r); + + void FillRect(const CRect& r, WORD c); + void WriteRect(const CRect& r, const WORD* RESTRICT src); + void ReadRect(const CRect& r, WORD* RESTRICT dst); + void MoveRect(const CPoint& src, const CPoint& dst, int w, int h); + + void ReadPage4(int tx, int ty, BYTE* RESTRICT dst); + void ReadPage8(int tx, int ty, BYTE* RESTRICT dst); + void ReadPage16(int tx, int ty, WORD* RESTRICT dst); + + void ReadFrame32(const CRect& r, DWORD* RESTRICT dst, bool rgb24); + + void Expand16(const WORD* RESTRICT src, DWORD* RESTRICT dst, int pixels); + void Expand24(const WORD* RESTRICT src, DWORD* RESTRICT dst, int pixels); + + void SaveBMP(LPCTSTR path, CRect r, int tp, int cx, int cy); +}; + +#pragma warning(default: 4244) \ No newline at end of file diff --git a/plugins/GSdx/GPURenderer.cpp b/plugins/GSdx/GPURenderer.cpp new file mode 100644 index 0000000000..d53b3cc612 --- /dev/null +++ b/plugins/GSdx/GPURenderer.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GPURenderer.h" + +CAtlMap GPURendererBase::m_wnd2gpu; \ No newline at end of file diff --git a/plugins/GSdx/GPURenderer.h b/plugins/GSdx/GPURenderer.h new file mode 100644 index 0000000000..b45abd91bf --- /dev/null +++ b/plugins/GSdx/GPURenderer.h @@ -0,0 +1,399 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPUState.h" +#include "GSVertexList.h" + +struct GPURendererSettings +{ + int m_filter; + int m_dither; + int m_aspectratio; + bool m_vsync; + CSize m_scale; +}; + +class GPURendererBase : public GPUState, protected GPURendererSettings +{ +protected: + HWND m_hWnd; + WNDPROC m_wndproc; + static CAtlMap m_wnd2gpu; + + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) + { + if(CAtlMap::CPair* pair = m_wnd2gpu.Lookup(hWnd)) + { + return pair->m_value->OnMessage(message, wParam, lParam); + } + + ASSERT(0); + + return 0; + } + + LRESULT OnMessage(UINT message, WPARAM wParam, LPARAM lParam) + { + if(message == WM_KEYUP) + { + if(wParam == VK_DELETE) + { + m_filter = (m_filter + 1) % 3; + return 0; + } + + if(wParam == VK_END) + { + m_dither = m_dither ? 0 : 1; + return 0; + } + + if(wParam == VK_NEXT) + { + m_aspectratio = (m_aspectratio + 1) % 3; + return 0; + } + } + + return m_wndproc(m_hWnd, message, wParam, lParam); + } + +public: + GPURendererBase(const GPURendererSettings& rs) + : GPUState(rs.m_scale) + , m_hWnd(NULL) + , m_wndproc(NULL) + { + m_filter = rs.m_filter; + m_dither = rs.m_dither; + m_aspectratio = rs.m_aspectratio; + m_vsync = rs.m_vsync; + m_scale = m_mem.GetScale(); + } + + virtual ~GPURendererBase() + { + if(m_wndproc) + { + SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, (LONG_PTR)m_wndproc); + + m_wnd2gpu.RemoveKey(m_hWnd); + } + } + + virtual bool Create(HWND hWnd) + { + m_hWnd = hWnd; + + m_wndproc = (WNDPROC)GetWindowLongPtr(hWnd, GWLP_WNDPROC); + SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WndProc); + m_wnd2gpu.SetAt(hWnd, this); + + DWORD style = GetWindowLong(hWnd, GWL_STYLE); + style |= WS_OVERLAPPEDWINDOW; + SetWindowLong(hWnd, GWL_STYLE, style); + UpdateWindow(hWnd); + + ShowWindow(hWnd, SW_SHOWNORMAL); + + return true; + } + + virtual void VSync() = 0; + virtual bool MakeSnapshot(LPCTSTR path) = 0; +}; + +template +class GPURenderer : public GPURendererBase +{ +protected: + typedef typename Device::Texture Texture; + + Vertex* m_vertices; + int m_count; + int m_maxcount; + GSVertexList m_vl; + + void Reset() + { + m_count = 0; + m_vl.RemoveAll(); + + __super::Reset(); + } + + void VertexKick() + { + if(m_vl.GetCount() < m_env.PRIM.VTX) + { + return; + } + + if(m_count > m_maxcount) + { + m_maxcount = max(10000, m_maxcount * 3/2); + m_vertices = (Vertex*)_aligned_realloc(m_vertices, sizeof(Vertex) * m_maxcount, 16); + m_maxcount -= 100; + } + + Vertex* v = &m_vertices[m_count]; + + int count = 0; + + switch(m_env.PRIM.TYPE) + { + case GPU_POLYGON: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.GetAt(2, v[2]); + m_vl.RemoveAll(); + count = 3; + break; + case GPU_LINE: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.RemoveAll(); + count = 2; + break; + case GPU_SPRITE: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.RemoveAll(); + count = 2; + break; + default: + ASSERT(0); + m_vl.RemoveAll(); + count = 0; + break; + } + + (this->*m_fpDrawingKickHandlers[m_env.PRIM.TYPE])(v, count); + + m_count += count; + } + + typedef void (GPURenderer::*DrawingKickHandler)(Vertex* v, int& count); + + DrawingKickHandler m_fpDrawingKickHandlers[4]; + + void DrawingKickNull(Vertex* v, int& count) + { + ASSERT(0); + } + + void ResetPrim() + { + m_vl.RemoveAll(); + } + + void FlushPrim() + { + if(m_count > 0) + { + /* + Dump(_T("db")); + + if(m_env.PRIM.TME) + { + CRect r; + + r.left = m_env.STATUS.TX << 6; + r.top = m_env.STATUS.TY << 8; + r.right = r.left + 256; + r.bottom = r.top + 256; + + CString str; + str.Format(_T("da_%d_%d_%d_%d_%d"), m_env.STATUS.TP, r); + Dump(str, m_env.STATUS.TP, r, false); + } + */ + + Draw(); + + m_count = 0; + + //Dump(_T("dc"), false); + } + } + + virtual void ResetDevice() {} + virtual void Draw() = 0; + virtual bool GetOutput(Texture& t) = 0; + + bool Merge() + { + Texture st[2]; + + if(!GetOutput(st[0])) + { + return false; + } + + CSize s; + + s.cx = st[0].GetWidth(); + s.cy = st[0].GetHeight(); + + GSVector4 sr[2]; + + sr[0].x = 0; + sr[0].y = 0; + sr[0].z = 1.0f; + sr[0].w = 1.0f; + + GSVector4 dr[2]; + + dr[0].x = 0; + dr[0].y = 0; + dr[0].z = (float)s.cx; + dr[0].w = (float)s.cy; + + GSVector4 c(0, 0, 0, 1); + + m_dev.Merge(st, sr, dr, s, 1, 1, c); + + return true; + } + +public: + Device m_dev; + +public: + GPURenderer(const GPURendererSettings& rs) + : GPURendererBase(rs) + , m_count(0) + , m_maxcount(10000) + { + m_vertices = (Vertex*)_aligned_malloc(sizeof(Vertex) * m_maxcount, 16); + m_maxcount -= 100; + + for(int i = 0; i < countof(m_fpDrawingKickHandlers); i++) + { + m_fpDrawingKickHandlers[i] = &GPURenderer::DrawingKickNull; + } + } + + virtual ~GPURenderer() + { + if(m_vertices) _aligned_free(m_vertices); + } + + virtual bool Create(HWND hWnd) + { + if(!__super::Create(hWnd)) + { + return false; + } + + if(!m_dev.Create(hWnd, m_vsync)) + { + return false; + } + + Reset(); + + return true; + } + + virtual void VSync() + { + GSPerfMonAutoTimer pmat(m_perfmon); + + // m_env.STATUS.LCF = ~m_env.STATUS.LCF; // ? + + if(!IsWindow(m_hWnd)) + { + return; + } + + Flush(); + + m_perfmon.Put(GSPerfMon::Frame); + + if(!Merge()) + { + return; + } + + // osd + + static UINT64 s_frame = 0; + static CString s_stats; + + if(m_perfmon.GetFrame() - s_frame >= 30) + { + m_perfmon.Update(); + + s_frame = m_perfmon.GetFrame(); + + double fps = 1000.0f / m_perfmon.Get(GSPerfMon::Frame); + + CRect r = m_env.GetDisplayRect(); + + int w = r.Width() << m_scale.cx; + int h = r.Height() << m_scale.cy; + + s_stats.Format( + _T("%I64d | %d x %d | %.2f fps (%d%%) | %d/%d | %d%% CPU | %.2f | %.2f"), + m_perfmon.GetFrame(), w, h, fps, (int)(100.0 * fps / m_env.GetFPS()), + (int)m_perfmon.Get(GSPerfMon::Prim), + (int)m_perfmon.Get(GSPerfMon::Draw), + m_perfmon.CPU(), + m_perfmon.Get(GSPerfMon::Swizzle) / 1024, + m_perfmon.Get(GSPerfMon::Unswizzle) / 1024 + ); + + double fillrate = m_perfmon.Get(GSPerfMon::Fillrate); + + if(fillrate > 0) + { + s_stats.Format(_T("%s | %.2f mpps"), CString(s_stats), fps * fillrate / (1024 * 1024)); + } + + SetWindowText(m_hWnd, s_stats); + } + + if(m_dev.IsLost()) + { + ResetDevice(); + } + + CRect r; + + GetClientRect(m_hWnd, &r); + + GSUtil::FitRect(r, m_aspectratio); + + m_dev.Present(r); + } + + virtual bool MakeSnapshot(LPCTSTR path) + { + CString fn; + + fn.Format(_T("%s_%s"), path, CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S"))); + + return m_dev.SaveCurrent(fn + _T(".bmp")); + } +}; diff --git a/plugins/GSdx/GPURendererSW.cpp b/plugins/GSdx/GPURendererSW.cpp new file mode 100644 index 0000000000..3a8505e2da --- /dev/null +++ b/plugins/GSdx/GPURendererSW.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GPURendererSW.h" diff --git a/plugins/GSdx/GPURendererSW.h b/plugins/GSdx/GPURendererSW.h new file mode 100644 index 0000000000..be69912b22 --- /dev/null +++ b/plugins/GSdx/GPURendererSW.h @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPURenderer.h" +#include "GPUDrawScanline.h" + +template +class GPURendererSW : public GPURenderer +{ +protected: + GSRasterizerList m_rl; + Texture m_texture; + + void ResetDevice() + { + m_texture = Texture(); + } + + bool GetOutput(Texture& t) + { + CRect r = m_env.GetDisplayRect(); + + r.left <<= m_scale.cx; + r.top <<= m_scale.cy; + r.right <<= m_scale.cx; + r.bottom <<= m_scale.cy; + + // TODO + static DWORD* buff = (DWORD*)_aligned_malloc(m_mem.GetWidth() * m_mem.GetHeight() * sizeof(DWORD), 16); + + m_mem.ReadFrame32(r, buff, !!m_env.STATUS.ISRGB24); + + r.OffsetRect(-r.TopLeft()); + + if(m_texture.GetWidth() != r.Width() || m_texture.GetHeight() != r.Height()) + { + m_texture = Texture(); + } + + if(!m_texture && !m_dev.CreateTexture(m_texture, r.Width(), r.Height())) + { + return false; + } + + m_texture.Update(r, buff, m_mem.GetWidth() * sizeof(DWORD)); + + t = m_texture; + + return true; + } + + void VertexKick() + { + GSVertexSW& v = m_vl.AddTail(); + + // TODO: x/y + off.x/y should wrap around at +/-1024 + + int x = (int)(m_v.XY.X + m_env.DROFF.X) << m_scale.cx; + int y = (int)(m_v.XY.Y + m_env.DROFF.Y) << m_scale.cy; + + int s = m_v.UV.X; + int t = m_v.UV.Y; + + GSVector4 pt(x, y, s, t); + + v.p = pt.xyxy(GSVector4::zero()); + v.t = (pt.zwzw(GSVector4::zero()) + GSVector4(0.125f)) * 256.0f; + v.c = GSVector4((DWORD)m_v.RGB.ai32) * 128.0f; + + __super::VertexKick(); + } + + void DrawingKickTriangle(GSVertexSW* v, int& count) + { + // TODO + } + + void DrawingKickLine(GSVertexSW* v, int& count) + { + // TODO + } + + void DrawingKickSprite(GSVertexSW* v, int& count) + { + // TODO + } + + GSVector4i GetScissor() + { + GSVector4i v; + + v.x = (int)m_env.DRAREATL.X << m_scale.cx; + v.y = (int)m_env.DRAREATL.Y << m_scale.cy; + v.z = min((int)(m_env.DRAREABR.X + 1) << m_scale.cx, m_mem.GetWidth()); + v.w = min((int)(m_env.DRAREABR.Y + 1) << m_scale.cy, m_mem.GetHeight()); + + return v; + } + + void Draw() + { + const GPUDrawingEnvironment& env = m_env; + + // + + GPUScanlineParam p; + + p.sel.dw = 0; + p.sel.iip = env.PRIM.IIP; + p.sel.me = env.STATUS.ME; + p.sel.abe = env.PRIM.ABE; + p.sel.abr = env.STATUS.ABR; + p.sel.tge = env.PRIM.TGE; + p.sel.tme = env.PRIM.TME; + p.sel.tlu = env.STATUS.TP < 2; + p.sel.twin = (env.TWIN.ai32 & 0xfffff) != 0; + p.sel.dtd = m_dither ? env.STATUS.DTD : 0; + p.sel.ltf = m_filter == 1 && env.PRIM.TYPE == GPU_POLYGON || m_filter == 2 ? 1 : 0; + + if(env.PRIM.TME) + { + const void* t = m_mem.GetTexture(env.STATUS.TP, env.STATUS.TX, env.STATUS.TY); + + if(!t) {ASSERT(0); return;} + + p.tex = t; + p.clut = m_mem.GetCLUT(env.STATUS.TP, env.CLUT.X, env.CLUT.Y); + } + + // + + GSRasterizerData data; + + data.scissor = GetScissor(); + data.vertices = m_vertices; + data.count = m_count; + data.param = &p; + + switch(env.PRIM.TYPE) + { + case GPU_POLYGON: data.primclass = GS_TRIANGLE_CLASS; break; + case GPU_LINE: data.primclass = GS_LINE_CLASS; break; + case GPU_SPRITE: data.primclass = GS_SPRITE_CLASS; break; + default: __assume(0); + } + + m_rl.Draw(&data); + + GSRasterizerStats stats; + + m_rl.GetStats(stats); + + m_perfmon.Put(GSPerfMon::Draw, 1); + m_perfmon.Put(GSPerfMon::Prim, stats.prims); + m_perfmon.Put(GSPerfMon::Fillrate, stats.pixels); + + // TODO + + { + GSVector4 tl(+1e10f); + GSVector4 br(-1e10f); + + for(int i = 0, j = m_count; i < j; i++) + { + GSVector4 p = m_vertices[i].p; + + tl = tl.minv(p); + br = br.maxv(p); + } + + GSVector4i scissor = data.scissor; + + CRect r; + + r.left = max(scissor.x, min(scissor.z, (int)tl.x)) >> m_scale.cx; + r.top = max(scissor.y, min(scissor.w, (int)tl.y)) >> m_scale.cy; + r.right = max(scissor.x, min(scissor.z, (int)br.x)) >> m_scale.cx; + r.bottom = max(scissor.y, min(scissor.w, (int)br.y)) >> m_scale.cy; + + Invalidate(r); + } + } + +public: + GPURendererSW(const GPURendererSettings& rs, int threads) + : GPURenderer(rs) + { + m_rl.Create(this, threads); + + m_fpDrawingKickHandlers[GPU_POLYGON] = (DrawingKickHandler)&GPURendererSW::DrawingKickTriangle; + m_fpDrawingKickHandlers[GPU_LINE] = (DrawingKickHandler)&GPURendererSW::DrawingKickLine; + m_fpDrawingKickHandlers[GPU_SPRITE] = (DrawingKickHandler)&GPURendererSW::DrawingKickSprite; + } + + virtual ~GPURendererSW() + { + } +}; diff --git a/plugins/GSdx/GPUSettingsDlg.cpp b/plugins/GSdx/GPUSettingsDlg.cpp new file mode 100644 index 0000000000..cccf108944 --- /dev/null +++ b/plugins/GSdx/GPUSettingsDlg.cpp @@ -0,0 +1,301 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSdx.h" +#include "GSUtil.h" +#include "GPUSettingsDlg.h" +#include +#include + +GSSetting GPUSettingsDlg::g_renderers[] = +{ + {0, _T("Direct3D7 (Software)"), NULL}, + {1, _T("Direct3D9 (Software)"), NULL}, + {2, _T("Direct3D10 (Software)"), NULL}, +// {3, _T("Null (Null)"), NULL}, +}; + +GSSetting GPUSettingsDlg::g_psversion[] = +{ + {D3DPS_VERSION(3, 0), _T("Pixel Shader 3.0"), NULL}, + {D3DPS_VERSION(2, 0), _T("Pixel Shader 2.0"), NULL}, + //{D3DPS_VERSION(1, 4), _T("Pixel Shader 1.4"), NULL}, + //{D3DPS_VERSION(1, 1), _T("Pixel Shader 1.1"), NULL}, + //{D3DPS_VERSION(0, 0), _T("Fixed Pipeline (bogus)"), NULL}, +}; + +GSSetting GPUSettingsDlg::g_filter[] = +{ + {0, _T("Nearest"), NULL}, + {1, _T("Bilinear (polygons only)"), NULL}, + {2, _T("Bilinear"), NULL}, +}; + +GSSetting GPUSettingsDlg::g_dithering[] = +{ + {0, _T("Disabled"), NULL}, + {1, _T("Auto"), NULL}, +}; + +GSSetting GPUSettingsDlg::g_aspectratio[] = +{ + {0, _T("Stretch"), NULL}, + {1, _T("4:3"), NULL}, + {2, _T("16:9"), NULL}, +}; + +GSSetting GPUSettingsDlg::g_internalresolution[] = +{ + {0 | (0 << 2), _T("H x 1 - V x 1"), NULL}, + {1 | (0 << 2), _T("H x 2 - V x 1"), NULL}, + {0 | (1 << 2), _T("H x 1 - V x 2"), NULL}, + {1 | (1 << 2), _T("H x 2 - V x 2"), NULL}, + {2 | (1 << 2), _T("H x 4 - V x 2"), NULL}, + {1 | (2 << 2), _T("H x 2 - V x 4"), NULL}, + {2 | (2 << 2), _T("H x 4 - V x 4"), NULL}, +}; + +IMPLEMENT_DYNAMIC(GPUSettingsDlg, CDialog) + +GPUSettingsDlg::GPUSettingsDlg(CWnd* pParent /*=NULL*/) + : CDialog(GPUSettingsDlg::IDD, pParent) +{ + +} + +GPUSettingsDlg::~GPUSettingsDlg() +{ +} + +LRESULT GPUSettingsDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) +{ + LRESULT ret = __super::DefWindowProc(message, wParam, lParam); + + if(message == WM_INITDIALOG) + { + SendMessage(WM_KICKIDLE); + } + + return ret; +} + +void GPUSettingsDlg::DoDataExchange(CDataExchange* pDX) +{ + __super::DoDataExchange(pDX); + DDX_Control(pDX, IDC_COMBO3, m_resolution); + DDX_Control(pDX, IDC_COMBO1, m_renderer); + DDX_Control(pDX, IDC_COMBO4, m_psversion); + DDX_Control(pDX, IDC_COMBO2, m_filter); + DDX_Control(pDX, IDC_COMBO5, m_dithering); + DDX_Control(pDX, IDC_COMBO6, m_aspectratio); + DDX_Control(pDX, IDC_COMBO7, m_internalresolution); + DDX_Control(pDX, IDC_SPIN3, m_swthreads); + DDX_Control(pDX, IDC_EDIT3, m_swthreadsedit); +} + +BEGIN_MESSAGE_MAP(GPUSettingsDlg, CDialog) + ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle) + ON_UPDATE_COMMAND_UI(IDC_COMBO4, OnUpdateD3D9Options) + ON_UPDATE_COMMAND_UI(IDC_COMBO7, OnUpdateSWOptions) + ON_UPDATE_COMMAND_UI(IDC_SPIN3, OnUpdateSWOptions) + ON_UPDATE_COMMAND_UI(IDC_EDIT3, OnUpdateSWOptions) + ON_CBN_SELCHANGE(IDC_COMBO1, &GPUSettingsDlg::OnCbnSelchangeCombo1) +END_MESSAGE_MAP() + +void GPUSettingsDlg::OnKickIdle() +{ + UpdateDialogControls(this, false); +} + +BOOL GPUSettingsDlg::OnInitDialog() +{ + __super::OnInitDialog(); + + CWinApp* pApp = AfxGetApp(); + + D3DCAPS9 caps; + memset(&caps, 0, sizeof(caps)); + caps.PixelShaderVersion = D3DPS_VERSION(0, 0); + + m_modes.RemoveAll(); + + // windowed + + { + D3DDISPLAYMODE mode; + memset(&mode, 0, sizeof(mode)); + m_modes.AddTail(mode); + + int iItem = m_resolution.AddString(_T("Windowed")); + m_resolution.SetItemDataPtr(iItem, m_modes.GetTailPosition()); + m_resolution.SetCurSel(iItem); + } + + // fullscreen + + if(CComPtr d3d = Direct3DCreate9(D3D_SDK_VERSION)) + { + UINT ModeWidth = pApp->GetProfileInt(_T("Settings"), _T("ModeWidth"), 0); + UINT ModeHeight = pApp->GetProfileInt(_T("Settings"), _T("ModeHeight"), 0); + UINT ModeRefreshRate = pApp->GetProfileInt(_T("Settings"), _T("ModeRefreshRate"), 0); + + UINT nModes = d3d->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8); + + for(UINT i = 0; i < nModes; i++) + { + D3DDISPLAYMODE mode; + + if(S_OK == d3d->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &mode)) + { + CString str; + str.Format(_T("%dx%d %dHz"), mode.Width, mode.Height, mode.RefreshRate); + int iItem = m_resolution.AddString(str); + + m_modes.AddTail(mode); + m_resolution.SetItemDataPtr(iItem, m_modes.GetTailPosition()); + + if(ModeWidth == mode.Width && ModeHeight == mode.Height && ModeRefreshRate == mode.RefreshRate) + { + m_resolution.SetCurSel(iItem); + } + } + } + + d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); + } + + bool isdx10avail = GSUtil::IsDirect3D10Available(); + + CAtlArray renderers; + + for(size_t i = 0; i < countof(g_renderers); i++) + { + if(i == 2 && !isdx10avail) continue; + + renderers.Add(g_renderers[i]); + } + + GSSetting::InitComboBox(renderers.GetData(), renderers.GetCount(), m_renderer, pApp->GetProfileInt(_T("GPUSettings"), _T("Renderer"), 1)); + GSSetting::InitComboBox(g_psversion, countof(g_psversion), m_psversion, pApp->GetProfileInt(_T("Settings"), _T("PixelShaderVersion2"), D3DPS_VERSION(2, 0)), caps.PixelShaderVersion); + GSSetting::InitComboBox(g_filter, countof(g_filter), m_filter, pApp->GetProfileInt(_T("GPUSettings"), _T("filter"), 0)); + GSSetting::InitComboBox(g_dithering, countof(g_dithering), m_dithering, pApp->GetProfileInt(_T("GPUSettings"), _T("dithering"), 1)); + GSSetting::InitComboBox(g_aspectratio, countof(g_aspectratio), m_aspectratio, pApp->GetProfileInt(_T("GPUSettings"), _T("AspectRatio"), 1)); + GSSetting::InitComboBox(g_internalresolution, countof(g_internalresolution), m_internalresolution, pApp->GetProfileInt(_T("GPUSettings"), _T("scale_x"), 0) | (pApp->GetProfileInt(_T("GPUSettings"), _T("scale_y"), 0) << 2)); + + + OnCbnSelchangeCombo1(); + + // + + m_swthreads.SetRange(1, 16); + m_swthreads.SetPos(pApp->GetProfileInt(_T("GPUSettings"), _T("swthreads"), 1)); + + // + + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GPUSettingsDlg::OnOK() +{ + CWinApp* pApp = AfxGetApp(); + + UpdateData(); + + if(m_resolution.GetCurSel() >= 0) + { + D3DDISPLAYMODE& mode = m_modes.GetAt((POSITION)m_resolution.GetItemData(m_resolution.GetCurSel())); + pApp->WriteProfileInt(_T("Settings"), _T("ModeWidth"), mode.Width); + pApp->WriteProfileInt(_T("Settings"), _T("ModeHeight"), mode.Height); + pApp->WriteProfileInt(_T("Settings"), _T("ModeRefreshRate"), mode.RefreshRate); + } + + if(m_renderer.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("GPUSettings"), _T("Renderer"), (DWORD)m_renderer.GetItemData(m_renderer.GetCurSel())); + } + + if(m_psversion.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("Settings"), _T("PixelShaderVersion2"), (DWORD)m_psversion.GetItemData(m_psversion.GetCurSel())); + } + + if(m_filter.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("GPUSettings"), _T("filter"), (DWORD)m_filter.GetItemData(m_filter.GetCurSel())); + } + + if(m_dithering.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("GPUSettings"), _T("dithering"), (DWORD)m_dithering.GetItemData(m_dithering.GetCurSel())); + } + + if(m_aspectratio.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("GPUSettings"), _T("AspectRatio"), (DWORD)m_aspectratio.GetItemData(m_aspectratio.GetCurSel())); + } + + if(m_internalresolution.GetCurSel() >= 0) + { + DWORD value = (DWORD)m_internalresolution.GetItemData(m_internalresolution.GetCurSel()); + + pApp->WriteProfileInt(_T("GPUSettings"), _T("scale_x"), value & 3); + pApp->WriteProfileInt(_T("GPUSettings"), _T("scale_y"), (value >> 2) & 3); + } + + pApp->WriteProfileInt(_T("GPUSettings"), _T("swthreads"), m_swthreads.GetPos()); + + __super::OnOK(); +} + +void GPUSettingsDlg::OnUpdateResolution(CCmdUI* pCmdUI) +{ + UpdateData(); + + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(i == 1); +} + +void GPUSettingsDlg::OnUpdateD3D9Options(CCmdUI* pCmdUI) +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(i == 1); +} + +void GPUSettingsDlg::OnUpdateSWOptions(CCmdUI* pCmdUI) +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(i >= 0 && i <= 2); +} + +void GPUSettingsDlg::OnCbnSelchangeCombo1() +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + GetDlgItem(IDC_LOGO9)->ShowWindow(i == 1 ? SW_SHOW : SW_HIDE); + GetDlgItem(IDC_LOGO10)->ShowWindow(i == 2 ? SW_SHOW : SW_HIDE); +} diff --git a/plugins/GSdx/GPUSettingsDlg.h b/plugins/GSdx/GPUSettingsDlg.h new file mode 100644 index 0000000000..b91e9f496b --- /dev/null +++ b/plugins/GSdx/GPUSettingsDlg.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSSetting.h" +#include "resource.h" + +class GPUSettingsDlg : public CDialog +{ + DECLARE_DYNAMIC(GPUSettingsDlg) + +private: + CAtlList m_modes; + +public: + GPUSettingsDlg(CWnd* pParent = NULL); // standard constructor + virtual ~GPUSettingsDlg(); + + static GSSetting g_renderers[]; + static GSSetting g_psversion[]; + static GSSetting g_filter[]; + static GSSetting g_dithering[]; + static GSSetting g_aspectratio[]; + static GSSetting g_internalresolution[]; + +// Dialog Data + enum { IDD = IDD_GPUCONFIG }; + CComboBox m_resolution; + CComboBox m_renderer; + CComboBox m_psversion; + CComboBox m_filter; + CComboBox m_dithering; + CComboBox m_aspectratio; + CComboBox m_internalresolution; + CSpinButtonCtrl m_swthreads; + CEdit m_swthreadsedit; + +protected: + virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual BOOL OnInitDialog(); + virtual void OnOK(); + + DECLARE_MESSAGE_MAP() + +public: + afx_msg void OnKickIdle(); + afx_msg void OnUpdateResolution(CCmdUI* pCmdUI); + afx_msg void OnUpdateD3D9Options(CCmdUI* pCmdUI); + afx_msg void OnUpdateSWOptions(CCmdUI* pCmdUI); + afx_msg void OnCbnSelchangeCombo1(); +}; diff --git a/plugins/GSdx/GPUState.cpp b/plugins/GSdx/GPUState.cpp new file mode 100644 index 0000000000..da59192a73 --- /dev/null +++ b/plugins/GSdx/GPUState.cpp @@ -0,0 +1,747 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GPUState.h" + +GPUState::GPUState(const CSize& scale) + : m_mem(scale) + , s_n(0) +{ + memset(m_status, 0, sizeof(m_status)); + + for(int i = 0; i < countof(m_fpGPUStatusCommandHandlers); i++) + { + m_fpGPUStatusCommandHandlers[i] = &GPUState::SCH_Null; + } + + m_fpGPUStatusCommandHandlers[0x00] = &GPUState::SCH_ResetGPU; + m_fpGPUStatusCommandHandlers[0x01] = &GPUState::SCH_ResetCommandBuffer; + m_fpGPUStatusCommandHandlers[0x02] = &GPUState::SCH_ResetIRQ; + m_fpGPUStatusCommandHandlers[0x03] = &GPUState::SCH_DisplayEnable; + m_fpGPUStatusCommandHandlers[0x04] = &GPUState::SCH_DMASetup; + m_fpGPUStatusCommandHandlers[0x05] = &GPUState::SCH_StartOfDisplayArea; + m_fpGPUStatusCommandHandlers[0x06] = &GPUState::SCH_HorizontalDisplayRange; + m_fpGPUStatusCommandHandlers[0x07] = &GPUState::SCH_VerticalDisplayRange; + m_fpGPUStatusCommandHandlers[0x08] = &GPUState::SCH_DisplayMode; + m_fpGPUStatusCommandHandlers[0x10] = &GPUState::SCH_GPUInfo; + + m_fpGPUPacketHandler[0] = &GPUState::PH_Command; + m_fpGPUPacketHandler[1] = &GPUState::PH_Polygon; + m_fpGPUPacketHandler[2] = &GPUState::PH_Line; + m_fpGPUPacketHandler[3] = &GPUState::PH_Sprite; + m_fpGPUPacketHandler[4] = &GPUState::PH_Move; + m_fpGPUPacketHandler[5] = &GPUState::PH_Write; + m_fpGPUPacketHandler[6] = &GPUState::PH_Read; + m_fpGPUPacketHandler[7] = &GPUState::PH_Environment; + + Reset(); +} + +GPUState::~GPUState() +{ +} + +void GPUState::Reset() +{ + m_env.Reset(); + + m_mem.Invalidate(CRect(0, 0, 1024, 512)); + + memset(&m_v, 0, sizeof(m_v)); +} + +void GPUState::Flush() +{ + FlushPrim(); +} + +void GPUState::SetPrim(GPUReg* r) +{ + if(m_env.PRIM.TYPE != r->PRIM.TYPE) + { + ResetPrim(); + } + + GPURegPRIM PRIM = r->PRIM; + + PRIM.VTX = 0; + + switch(r->PRIM.TYPE) + { + case GPU_POLYGON: + PRIM.ai32 = (r->PRIM.ai32 & 0xF7000000) | 3; // TYPE IIP TME ABE TGE + break; + case GPU_LINE: + PRIM.ai32 = (r->PRIM.ai32 & 0xF2000000) | 2; // TYPE IIP ABE + PRIM.TGE = 1; // ? + break; + case GPU_SPRITE: + PRIM.ai32 = (r->PRIM.ai32 & 0xE7000000) | 2; // TYPE TME ABE TGE + break; + } + + if(m_env.PRIM.ai32 != PRIM.ai32) + { + Flush(); + + m_env.PRIM = PRIM; + } +} + +void GPUState::SetCLUT(GPUReg* r) +{ + UINT32 mask = 0xFFFF0000; // X Y + + UINT32 value = (m_env.CLUT.ai32 & ~mask) | (r->ai32 & mask); + + if(m_env.CLUT.ai32 != value) + { + Flush(); + + m_env.CLUT.ai32 = value; + } +} + +void GPUState::SetTPAGE(GPUReg* r) +{ + UINT32 mask = 0x000001FF; // TP ABR TY TX + + UINT32 value = (m_env.STATUS.ai32 & ~mask) | ((r->ai32 >> 16) & mask); + + if(m_env.STATUS.ai32 != value) + { + Flush(); + + m_env.STATUS.ai32 = value; + } +} + +void GPUState::Invalidate(const CRect& r) +{ + m_mem.Invalidate(r); +} + +void GPUState::WriteData(const BYTE* mem, UINT32 size) +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + size <<= 2; + + m_write.Append(mem, size); + + int i = 0; + + while(i < m_write.bytes) + { + GPUReg* r = (GPUReg*)&m_write.buff[i]; + + int ret = (this->*m_fpGPUPacketHandler[r->PACKET.TYPE])(r, (m_write.bytes - i) >> 2); + + if(ret == 0) return; // need more data + + i += ret << 2; + } + + m_write.Remove(i); +} + +void GPUState::ReadData(BYTE* mem, UINT32 size) +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + int remaining = m_read.bytes - m_read.cur; + + int bytes = (int)size << 2; + + if(bytes > remaining) + { + // ASSERT(0); + + TRACE(_T("WARNING: ReadData\n")); + + // memset(&mem[remaining], 0, bytes - remaining); + + bytes = remaining; + } + + memcpy(mem, &m_read.buff[m_read.cur], bytes); + + m_read.cur += bytes; + + if(m_read.cur >= m_read.bytes) + { + m_env.STATUS.IMG = 0; + } +} + +void GPUState::WriteStatus(UINT32 status) +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + UINT32 b = status >> 24; + + m_status[b] = status; + + (this->*m_fpGPUStatusCommandHandlers[b])((GPUReg*)&status); +} + +UINT32 GPUState::ReadStatus() +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + m_env.STATUS.LCF = ~m_env.STATUS.LCF; // ? + + return m_env.STATUS.ai32; +} + +void GPUState::Freeze(GPUFreezeData* data) +{ + data->status = m_env.STATUS.ai32; + memcpy(data->control, m_status, 256 * 4); + m_mem.ReadRect(CRect(0, 0, 1024, 512), data->vram); +} + +void GPUState::Defrost(const GPUFreezeData* data) +{ + m_env.STATUS.ai32 = data->status; + memcpy(m_status, data->control, 256 * 4); + m_mem.WriteRect(CRect(0, 0, 1024, 512), data->vram); + + for(int i = 0; i <= 8; i++) + { + WriteStatus(m_status[i]); + } +} + +void GPUState::SCH_Null(GPUReg* r) +{ + ASSERT(0); +} + +void GPUState::SCH_ResetGPU(GPUReg* r) +{ + Reset(); +} + +void GPUState::SCH_ResetCommandBuffer(GPUReg* r) +{ + // ? +} + +void GPUState::SCH_ResetIRQ(GPUReg* r) +{ + // ? +} + +void GPUState::SCH_DisplayEnable(GPUReg* r) +{ + m_env.STATUS.DEN = r->DEN.DEN; +} + +void GPUState::SCH_DMASetup(GPUReg* r) +{ + m_env.STATUS.DMA = r->DMA.DMA; +} + +void GPUState::SCH_StartOfDisplayArea(GPUReg* r) +{ + m_env.DAREA = r->DAREA; +} + +void GPUState::SCH_HorizontalDisplayRange(GPUReg* r) +{ + m_env.DHRANGE = r->DHRANGE; +} + +void GPUState::SCH_VerticalDisplayRange(GPUReg* r) +{ + m_env.DVRANGE = r->DVRANGE; +} + +void GPUState::SCH_DisplayMode(GPUReg* r) +{ + m_env.STATUS.WIDTH0 = r->DMODE.WIDTH0; + m_env.STATUS.HEIGHT = r->DMODE.HEIGHT; + m_env.STATUS.ISPAL = r->DMODE.ISPAL; + m_env.STATUS.ISRGB24 = r->DMODE.ISRGB24; + m_env.STATUS.ISINTER = r->DMODE.ISINTER; + m_env.STATUS.WIDTH1 = r->DMODE.WIDTH1; +} + +void GPUState::SCH_GPUInfo(GPUReg* r) +{ + UINT32 value = 0; + + switch(r->GPUINFO.PARAM) + { + case 0x2: + value = m_env.TWIN.ai32; + break; + case 0x0: + case 0x1: + case 0x3: + value = m_env.DRAREATL.ai32; + break; + case 0x4: + value = m_env.DRAREABR.ai32; + break; + case 0x5: + case 0x6: + value = m_env.DROFF.ai32; + break; + case 0x7: + value = 2; + break; + case 0x8: + case 0xf: + value = 0xBFC03720; // ? + break; + default: + ASSERT(0); + break; + } + + m_read.RemoveAll(); + m_read.Append((BYTE*)&value, 4); + m_read.cur = 0; +} + +int GPUState::PH_Command(GPUReg* r, int size) +{ + switch(r->PACKET.OPTION) + { + case 0: // ??? + + return 1; + + case 1: // clear cache + + return 1; + + case 2: // fillrect + + if(size < 3) return 0; + + Flush(); + + CRect r2; + + r2.left = r[1].XY.X; + r2.top = r[1].XY.Y; + r2.right = r2.left + r[2].XY.X; + r2.bottom = r2.top + r[2].XY.Y; + + WORD c = (WORD)(((r[0].RGB.R >> 3) << 10) | ((r[0].RGB.R >> 3) << 5) | (r[0].RGB.R >> 3)); + + m_mem.FillRect(r2, c); + + Invalidate(r2); + + Dump(_T("f")); + + return 3; + } + + ASSERT(0); + + return 1; +} + +int GPUState::PH_Polygon(GPUReg* r, int size) +{ + int required = 1; + + int vertices = r[0].POLYGON.VTX ? 4 : 3; + + required += vertices; + + if(r[0].POLYGON.TME) required += vertices; + + if(r[0].POLYGON.IIP) required += vertices - 1; + + if(size < required) return 0; + + // + + SetPrim(r); + + if(r[0].POLYGON.TME) + { + SetCLUT(&r[2]); + + SetTPAGE(&r[r[0].POLYGON.IIP ? 5 : 4]); + } + + // + + GPUVertex v[4]; + + for(int i = 0, j = 0; j < vertices; j++) + { + v[j].RGB = r[r[0].POLYGON.IIP ? i : 0].RGB; + + if(j == 0 || r[0].POLYGON.IIP) i++; + + v[j].XY = r[i++].XY; + + if(r[0].POLYGON.TME) + { + v[j].UV.X = r[i].UV.U; + v[j].UV.Y = r[i].UV.V; + + i++; + } + } + + for(int i = 0; i <= vertices - 3; i++) + { + for(int j = 0; j < 3; j++) + { + m_v = v[i + j]; + + VertexKick(); + } + } + + // + + return required; +} + +int GPUState::PH_Line(GPUReg* r, int size) +{ + int required = 1; + + int vertices = 0; + + if(r->LINE.PLL) + { + required++; + + for(int i = 1; i < size; i++) + { + if(r[i].ai32 == 0x55555555) + { + vertices = i - 1; + } + } + + if(vertices < 2) + { + return 0; + } + } + else + { + vertices = 2; + } + + required += vertices; + + if(r->LINE.IIP) required += vertices - 1; + + // + + SetPrim(r); + + // + + for(int i = 0, j = 0; j < vertices; j++) + { + if(j >= 2) VertexKick(); + + m_v.RGB = r[r[0].LINE.IIP ? i : 0].RGB; + + if(j == 0 || r[0].LINE.IIP) i++; + + m_v.XY = r[i++].XY; + + VertexKick(); + } + + // + + return required; +} + +int GPUState::PH_Sprite(GPUReg* r, int size) +{ + int required = 2; + + if(r[0].SPRITE.TME) required++; + if(r[0].SPRITE.SIZE == 0) required++; + + if(size < required) return 0; + + // + + SetPrim(r); + + if(r[0].SPRITE.TME) + { + SetCLUT(&r[2]); + } + + // + + int i = 0; + + m_v.RGB = r[i++].RGB; + + m_v.XY = r[i++].XY; + + if(r[0].SPRITE.TME) + { + m_v.UV.X = r[i].UV.U; + m_v.UV.Y = r[i].UV.V; + + i++; + } + + VertexKick(); + + int w = 0; + int h = 0; + + switch(r[0].SPRITE.SIZE) + { + case 0: w = r[i].XY.X; h = r[i].XY.Y; i++; break; + case 1: w = h = 1; break; + case 2: w = h = 8; break; + case 3: w = h = 16; break; + default: __assume(0); + } + + m_v.XY.X += w; + m_v.XY.Y += h; + + if(r[0].SPRITE.TME) + { + m_v.UV.X += w; + m_v.UV.Y += h; + } + + VertexKick(); + + // + + return required; +} + +int GPUState::PH_Move(GPUReg* r, int size) +{ + if(size < 4) return 0; + + Flush(); + + CPoint src, dst; + + src.x = r[1].XY.X; + src.y = r[1].XY.Y; + + dst.x = r[2].XY.X; + dst.y = r[2].XY.Y; + + int w = r[3].XY.X; + int h = r[3].XY.Y; + + m_mem.MoveRect(src, dst, w, h); + + Invalidate(CRect(dst, CSize(w, h))); + + // Dump(_T("m")); + + return 4; +} + +int GPUState::PH_Write(GPUReg* r, int size) +{ + if(size < 3) return 0; + + int w = r[2].XY.X; + int h = r[2].XY.Y; + + int required = 3 + ((w * h + 1) >> 1); + + if(size < required) return 0; + + Flush(); + + CRect r2; + + r2.left = r[1].XY.X; + r2.top = r[1].XY.Y; + r2.right = r2.left + w; + r2.bottom = r2.top + h; + + m_mem.WriteRect(r2, (const WORD*)&r[3]); + + Invalidate(r2); + + Dump(_T("w")); + + m_perfmon.Put(GSPerfMon::Swizzle, w * h * 2); + + return required; +} + +int GPUState::PH_Read(GPUReg* r, int size) +{ + if(size < 3) return 0; + + Flush(); + + int w = r[2].XY.X; + int h = r[2].XY.Y; + + CRect r2; + + r2.left = r[1].XY.X; + r2.top = r[1].XY.Y; + r2.right = r2.left + w; + r2.bottom = r2.top + h; + + m_read.bytes = ((w * h + 1) & ~1) * 2; + m_read.cur = 0; + m_read.Reserve(m_read.bytes); + + m_mem.ReadRect(r2, (WORD*)m_read.buff); + + Dump(_T("r")); + + m_env.STATUS.IMG = 1; + + return 3; +} + +int GPUState::PH_Environment(GPUReg* r, int size) +{ + Flush(); // TODO: only call when something really changes + + switch(r->PACKET.OPTION) + { + case 1: // draw mode setting + + m_env.STATUS.TX = r->MODE.TX; + m_env.STATUS.TY = r->MODE.TY; + m_env.STATUS.ABR = r->MODE.ABR; + m_env.STATUS.TP = r->MODE.TP; + m_env.STATUS.DTD = r->MODE.DTD; + m_env.STATUS.DFE = r->MODE.DFE; + + return 1; + + case 2: // texture window setting + + m_env.TWIN = r->TWIN; + + return 1; + + case 3: // set drawing area top left + + m_env.DRAREATL = r->DRAREA; + + return 1; + + case 4: // set drawing area bottom right + + m_env.DRAREABR = r->DRAREA; + + return 1; + + case 5: // drawing offset + + m_env.DROFF = r->DROFF; + + return 1; + + case 6: // mask setting + + m_env.STATUS.MD = r->MASK.MD; + m_env.STATUS.ME = r->MASK.ME; + + return 1; + } + + ASSERT(0); + + return 1; +} + +// + +GPUState::Buffer::Buffer() +{ + bytes = 0; + maxbytes = 4096; + buff = (BYTE*)_aligned_malloc(maxbytes, 16); + cur = 0; +} + +GPUState::Buffer::~Buffer() +{ + _aligned_free(buff); +} + +void GPUState::Buffer::Reserve(int size) +{ + if(size > maxbytes) + { + maxbytes = (maxbytes + size + 1023) & ~1023; + + buff = (BYTE*)_aligned_realloc(buff, maxbytes, 16); + } +} + +void GPUState::Buffer::Append(const BYTE* src, int size) +{ + Reserve(bytes + (int)size); + + memcpy(&buff[bytes], src, size); + + bytes += size; +} + +void GPUState::Buffer::Remove(int size) +{ + ASSERT(size <= bytes); + + if(size < bytes) + { + memmove(&buff[0], &buff[size], bytes - size); + + bytes -= size; + } + else + { + bytes = 0; + } + + #ifdef DEBUG + memset(&buff[bytes], 0xff, maxbytes - bytes); + #endif +} + +void GPUState::Buffer::RemoveAll() +{ + bytes = 0; +} diff --git a/plugins/GSdx/GPUState.h b/plugins/GSdx/GPUState.h new file mode 100644 index 0000000000..68d1eadd84 --- /dev/null +++ b/plugins/GSdx/GPUState.h @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPU.h" +#include "GPUDrawingEnvironment.h" +#include "GPULocalMemory.h" +#include "GPUVertex.h" +#include "GSAlignedClass.h" +#include "GSUtil.h" +#include "GSPerfMon.h" + +class GPUState : public GSAlignedClass<16> +{ + typedef void (GPUState::*GPUStatusCommandHandler)(GPUReg* r); + + GPUStatusCommandHandler m_fpGPUStatusCommandHandlers[256]; + + void SCH_Null(GPUReg* r); + void SCH_ResetGPU(GPUReg* r); + void SCH_ResetCommandBuffer(GPUReg* r); + void SCH_ResetIRQ(GPUReg* r); + void SCH_DisplayEnable(GPUReg* r); + void SCH_DMASetup(GPUReg* r); + void SCH_StartOfDisplayArea(GPUReg* r); + void SCH_HorizontalDisplayRange(GPUReg* r); + void SCH_VerticalDisplayRange(GPUReg* r); + void SCH_DisplayMode(GPUReg* r); + void SCH_GPUInfo(GPUReg* r); + + typedef int (GPUState::*GPUPacketHandler)(GPUReg* r, int size); + + GPUPacketHandler m_fpGPUPacketHandler[8]; + + int PH_Command(GPUReg* r, int size); + int PH_Polygon(GPUReg* r, int size); + int PH_Line(GPUReg* r, int size); + int PH_Sprite(GPUReg* r, int size); + int PH_Move(GPUReg* r, int size); + int PH_Write(GPUReg* r, int size); + int PH_Read(GPUReg* r, int size); + int PH_Environment(GPUReg* r, int size); + + class Buffer + { + public: + int bytes; + int maxbytes; + BYTE* buff; + int cur; + + public: + Buffer(); + ~Buffer(); + void Reserve(int size); + void Append(const BYTE* src, int size); + void Remove(int size); + void RemoveAll(); + }; + + Buffer m_write; + Buffer m_read; + + void SetPrim(GPUReg* r); + void SetCLUT(GPUReg* r); + void SetTPAGE(GPUReg* r); + +protected: + + int s_n; + + void Dump(LPCTSTR s, UINT32 TP, const CRect& r, int inc = true) + { + //if(m_perfmon.GetFrame() < 1000) + //if((m_env.TWIN.ai32 & 0xfffff) == 0) + //if(!m_env.STATUS.ME && !m_env.STATUS.MD) + return; + + if(inc) s_n++; + + //if(s_n < 86) return; + + int dir = 1; +#ifdef DEBUG + dir = 2; +#endif + CString str; + str.Format(_T("c:\\temp%d\\%04d_%s.bmp"), dir, s_n, s); + m_mem.SaveBMP(str, r, TP, m_env.CLUT.X, m_env.CLUT.Y); + } + + void Dump(LPCTSTR s, int inc = true) + { + Dump(s, 2, CRect(0, 0, 1024, 512), inc); + } + +public: + GPUDrawingEnvironment m_env; + GPULocalMemory m_mem; + GPUVertex m_v; + GSPerfMon m_perfmon; + UINT32 m_status[256]; + +public: + GPUState(const CSize& scale); + virtual ~GPUState(); + + virtual void Reset(); + virtual void Flush(); + virtual void FlushPrim() = 0; + virtual void ResetPrim() = 0; + virtual void VertexKick() = 0; + virtual void Invalidate(const CRect& r); + + void WriteData(const BYTE* mem, UINT32 size); + void ReadData(BYTE* mem, UINT32 size); + + void WriteStatus(UINT32 status); + UINT32 ReadStatus(); + + void Freeze(GPUFreezeData* data); + void Defrost(const GPUFreezeData* data); +}; + diff --git a/plugins/GSdx/GPUVertex.h b/plugins/GSdx/GPUVertex.h new file mode 100644 index 0000000000..7ae86ca426 --- /dev/null +++ b/plugins/GSdx/GPUVertex.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GPU.h" +#include "GSVector.h" + +#pragma pack(push, 1) + +__declspec(align(16)) struct GPUVertex +{ + union + { + struct + { + GPURegRGB RGB; + GPURegXY XY; + GPURegXY UV; + }; + + struct {__m128i m128i;}; + struct {__m128 m128;}; + }; + + GPUVertex() {memset(this, 0, sizeof(*this));} +}; + +struct GPUVertexNull +{ +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GS.cpp b/plugins/GSdx/GS.cpp new file mode 100644 index 0000000000..02aeb9e006 --- /dev/null +++ b/plugins/GSdx/GS.cpp @@ -0,0 +1,589 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSUtil.h" +#include "GSRendererHW9.h" +#include "GSRendererHW10.h" +#include "GSRendererSW.h" +#include "GSRendererNull.h" +#include "GSSettingsDlg.h" + +#define PS2E_LT_GS 0x01 +#define PS2E_GS_VERSION 0x0006 +#define PS2E_X86 0x01 // 32 bit +#define PS2E_X86_64 0x02 // 64 bit + +static HRESULT s_hr = E_FAIL; +static GSRendererBase* s_gs = NULL; +static void (*s_irq)() = NULL; +static BYTE* s_basemem = NULL; + +EXPORT_C_(UINT32) PS2EgetLibType() +{ + return PS2E_LT_GS; +} + +EXPORT_C_(char*) PS2EgetLibName() +{ + return GSUtil::GetLibName(); +} + +EXPORT_C_(UINT32) PS2EgetLibVersion2(UINT32 type) +{ + const UINT32 revision = 0; + const UINT32 build = 1; + + return (build << 0) | (revision << 8) | (PS2E_GS_VERSION << 16) | (PLUGIN_VERSION << 24); +} + +EXPORT_C_(UINT32) PS2EgetCpuPlatform() +{ +#if _M_AMD64 + return PS2E_X86_64; +#else + return PS2E_X86; +#endif +} + +EXPORT_C GSsetBaseMem(BYTE* mem) +{ + s_basemem = mem - 0x12000000; +} + +EXPORT_C_(INT32) GSinit() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + return 0; +} + +EXPORT_C GSshutdown() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); +} + +EXPORT_C GSclose() +{ + delete s_gs; + + s_gs = NULL; + + if(SUCCEEDED(s_hr)) + { + ::CoUninitialize(); + + s_hr = E_FAIL; + } +} + +static INT32 GSopen(void* dsp, char* title, int mt, int renderer) +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + if(!GSUtil::CheckDirectX() || !GSUtil::CheckSSE()) + { + return -1; + } + + GSclose(); + + // TODO + + int nloophack = AfxGetApp()->GetProfileInt(_T("Settings"), _T("nloophack"), 2); + + GSRendererSettings rs; + + rs.m_interlace = AfxGetApp()->GetProfileInt(_T("Settings"), _T("interlace"), 0); + rs.m_aspectratio = AfxGetApp()->GetProfileInt(_T("Settings"), _T("aspectratio"), 1); + rs.m_filter = AfxGetApp()->GetProfileInt(_T("Settings"), _T("filter"), 1); + rs.m_vsync = !!AfxGetApp()->GetProfileInt(_T("Settings"), _T("vsync"), FALSE); + rs.m_nativeres = !!AfxGetApp()->GetProfileInt(_T("Settings"), _T("nativeres"), FALSE); + + int threads = AfxGetApp()->GetProfileInt(_T("Settings"), _T("swthreads"), 1); + + switch(renderer) + { + default: + case 0: s_gs = new GSRendererHW9(s_basemem, !!mt, s_irq, nloophack, rs); break; + case 1: s_gs = new GSRendererSW(s_basemem, !!mt, s_irq, nloophack, rs, threads); break; + case 2: s_gs = new GSRendererNull(s_basemem, !!mt, s_irq, nloophack, rs); break; + case 3: s_gs = new GSRendererHW10(s_basemem, !!mt, s_irq, nloophack, rs); break; + case 4: s_gs = new GSRendererSW(s_basemem, !!mt, s_irq, nloophack, rs, threads); break; + case 5: s_gs = new GSRendererNull(s_basemem, !!mt, s_irq, nloophack, rs); break; + case 6: s_gs = new GSRendererSW(s_basemem, !!mt, s_irq, nloophack, rs, threads); break; + case 7: s_gs = new GSRendererNull(s_basemem, !!mt, s_irq, nloophack, rs); break; + } + + s_hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); + + if(!s_gs->Create(CString(title))) + { + GSclose(); + + return -1; + } + + s_gs->m_wnd.Show(); + + *(HWND*)dsp = s_gs->m_wnd; + + // if(mt) _mm_setcsr(MXCSR); + + return 0; +} + +EXPORT_C_(INT32) GSopen(void* dsp, char* title, int mt) +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + int renderer = AfxGetApp()->GetProfileInt(_T("Settings"), _T("renderer"), 0); + + return GSopen(dsp, title, mt, renderer); +} + +EXPORT_C GSreset() +{ + s_gs->Reset(); +} + +EXPORT_C GSgifSoftReset(int mask) +{ + s_gs->SoftReset((BYTE)mask); +} + +EXPORT_C GSwriteCSR(UINT32 csr) +{ + s_gs->WriteCSR(csr); +} + +EXPORT_C GSreadFIFO(BYTE* mem) +{ + s_gs->ReadFIFO(mem, 1); +} + +EXPORT_C GSreadFIFO2(BYTE* mem, UINT32 size) +{ + s_gs->ReadFIFO(mem, size); +} + +EXPORT_C GSgifTransfer1(BYTE* mem, UINT32 addr) +{ + s_gs->Transfer<0>(mem + addr, (0x4000 - addr) / 16); +} + +EXPORT_C GSgifTransfer2(BYTE* mem, UINT32 size) +{ + s_gs->Transfer<1>(mem, size); +} + +EXPORT_C GSgifTransfer3(BYTE* mem, UINT32 size) +{ + s_gs->Transfer<2>(mem, size); +} + +EXPORT_C GSvsync(int field) +{ + s_gs->VSync(field); +} + +EXPORT_C_(UINT32) GSmakeSnapshot(char* path) +{ + return s_gs->MakeSnapshot(CString(path) + _T("gsdx")); +} + +EXPORT_C GSkeyEvent(keyEvent* ev) +{ +} + +EXPORT_C_(INT32) GSfreeze(int mode, GSFreezeData* data) +{ + if(mode == FREEZE_SAVE) + { + return s_gs->Freeze(data, false); + } + else if(mode == FREEZE_SIZE) + { + return s_gs->Freeze(data, true); + } + else if(mode == FREEZE_LOAD) + { + return s_gs->Defrost(data); + } + + return 0; +} + +EXPORT_C GSconfigure() +{ + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + GSSettingsDlg dlg; + + if(IDOK == dlg.DoModal()) + { + GSshutdown(); + GSinit(); + } +} + +EXPORT_C_(INT32) GStest() +{ + return 0; + + // TODO + + /* + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + CComPtr dev; + + return SUCCEEDED(D3D10CreateDevice(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_SDK_VERSION, &dev)) ? 0 : -1; + */ +} + +EXPORT_C GSabout() +{ +} + +EXPORT_C GSirqCallback(void (*irq)()) +{ + s_irq = irq; +} + +EXPORT_C GSsetGameCRC(DWORD crc, int options) +{ + s_gs->SetGameCRC(crc, options); +} + +EXPORT_C GSgetLastTag(UINT32* tag) +{ + s_gs->GetLastTag(tag); +} + +EXPORT_C GSsetFrameSkip(int frameskip) +{ + s_gs->SetFrameSkip(frameskip); +} + +EXPORT_C GSReplay(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) +{ + int renderer = -1; + + { + char* start = lpszCmdLine; + char* end = NULL; + long n = strtol(lpszCmdLine, &end, 10); + if(end > start) {renderer = n; lpszCmdLine = end;} + } + + while(*lpszCmdLine == ' ') lpszCmdLine++; + + ::SetPriorityClass(::GetCurrentProcess(), HIGH_PRIORITY_CLASS); + + CAtlArray buff; + + if(FILE* fp = fopen(lpszCmdLine, "rb")) + { + GSinit(); + + BYTE regs[0x2000]; + GSsetBaseMem(regs); + + HWND hWnd = NULL; + GSopen(&hWnd, _T(""), true, renderer); + + DWORD crc; + fread(&crc, 4, 1, fp); + GSsetGameCRC(crc, 0); + + GSFreezeData fd; + fread(&fd.size, 4, 1, fp); + fd.data = new BYTE[fd.size]; + fread(fd.data, fd.size, 1, fp); + GSfreeze(FREEZE_LOAD, &fd); + delete [] fd.data; + + fread(regs, 0x2000, 1, fp); + + long start = ftell(fp); + + unsigned int index, size, addr; + + GSvsync(1); + + while(1) + { + switch(fgetc(fp)) + { + case EOF: + fseek(fp, start, 0); + if(!IsWindowVisible(hWnd)) return; + break; + case 0: + index = fgetc(fp); + fread(&size, 4, 1, fp); + switch(index) + { + case 0: + if(buff.GetCount() < 0x4000) buff.SetCount(0x4000); + addr = 0x4000 - size; + fread(buff.GetData() + addr, size, 1, fp); + GSgifTransfer1(buff.GetData(), addr); + break; + case 1: + if(buff.GetCount() < size) buff.SetCount(size); + fread(buff.GetData(), size, 1, fp); + GSgifTransfer2(buff.GetData(), size / 16); + break; + case 2: + if(buff.GetCount() < size) buff.SetCount(size); + fread(buff.GetData(), size, 1, fp); + GSgifTransfer3(buff.GetData(), size / 16); + break; + } + break; + case 1: + GSvsync(fgetc(fp)); + if(!IsWindowVisible(hWnd)) return; + break; + case 2: + fread(&size, 4, 1, fp); + if(buff.GetCount() < size) buff.SetCount(size); + GSreadFIFO2(buff.GetData(), size / 16); + break; + case 3: + fread(regs, 0x2000, 1, fp); + break; + default: + return; + } + } + + GSclose(); + + GSshutdown(); + + fclose(fp); + } +} + +EXPORT_C GSBenchmark(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) +{ + ::SetPriorityClass(::GetCurrentProcess(), HIGH_PRIORITY_CLASS); + + FILE* file = _tfopen(_T("c:\\log.txt"), _T("a")); + + _ftprintf(file, _T("-------------------------\n\n")); + + if(1) + { + GSLocalMemory mem; + + static struct {int psm; LPCSTR name;} s_format[] = + { + {PSM_PSMCT32, "32"}, + {PSM_PSMCT24, "24"}, + {PSM_PSMCT16, "16"}, + {PSM_PSMCT16S, "16S"}, + {PSM_PSMT8, "8"}, + {PSM_PSMT4, "4"}, + {PSM_PSMT8H, "8H"}, + {PSM_PSMT4HL, "4HL"}, + {PSM_PSMT4HH, "4HH"}, + {PSM_PSMZ32, "32Z"}, + {PSM_PSMZ24, "24Z"}, + {PSM_PSMZ16, "16Z"}, + {PSM_PSMZ16S, "16ZS"}, + }; + + BYTE* ptr = (BYTE*)_aligned_malloc(1024 * 1024 * 4, 16); + + for(int i = 0; i < 1024 * 1024 * 4; i++) ptr[i] = (BYTE)i; + + // + + for(int tbw = 5; tbw <= 10; tbw++) + { + int n = 256 << ((10 - tbw) * 2); + + int w = 1 << tbw; + int h = 1 << tbw; + + _ftprintf(file, _T("%d x %d\n\n"), w, h); + + for(int i = 0; i < countof(s_format); i++) + { + const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[s_format[i].psm]; + + GSLocalMemory::writeImage wi = psm.wi; + GSLocalMemory::readImage ri = psm.ri; + GSLocalMemory::readTexture rtx = psm.rtx; + GSLocalMemory::readTexture rtxP = psm.rtxP; + + GIFRegBITBLTBUF BITBLTBUF; + + BITBLTBUF.SBP = 0; + BITBLTBUF.SBW = w / 64; + BITBLTBUF.SPSM = s_format[i].psm; + BITBLTBUF.DBP = 0; + BITBLTBUF.DBW = w / 64; + BITBLTBUF.DPSM = s_format[i].psm; + + GIFRegTRXPOS TRXPOS; + + TRXPOS.SSAX = 0; + TRXPOS.SSAY = 0; + TRXPOS.DSAX = 0; + TRXPOS.DSAY = 0; + + GIFRegTRXREG TRXREG; + + TRXREG.RRW = w; + TRXREG.RRH = h; + + CRect r(0, 0, w, h); + + GIFRegTEX0 TEX0; + + TEX0.TBP0 = 0; + TEX0.TBW = w / 64; + + GIFRegTEXA TEXA; + + TEXA.TA0 = 0; + TEXA.TA1 = 0x80; + TEXA.AEM = 0; + + int trlen = w * h * psm.trbpp / 8; + int len = w * h * psm.bpp / 8; + + clock_t start, end; + + _ftprintf(file, _T("[%4s] "), s_format[i].name); + + start = clock(); + + for(int j = 0; j < n; j++) + { + int x = 0; + int y = 0; + + (mem.*wi)(x, y, ptr, trlen, BITBLTBUF, TRXPOS, TRXREG); + } + + end = clock(); + + _ftprintf(file, _T("%6d %6d | "), (int)((float)trlen * n / (end - start) / 1000), (int)((float)(w * h) * n / (end - start) / 1000)); + + start = clock(); + + for(int j = 0; j < n; j++) + { + int x = 0; + int y = 0; + + (mem.*ri)(x, y, ptr, trlen, BITBLTBUF, TRXPOS, TRXREG); + } + + end = clock(); + + _ftprintf(file, _T("%6d %6d | "), (int)((float)trlen * n / (end - start) / 1000), (int)((float)(w * h) * n / (end - start) / 1000)); + + start = clock(); + + for(int j = 0; j < n; j++) + { + (mem.*rtx)(r, ptr, w * 4, TEX0, TEXA); + } + + end = clock(); + + _ftprintf(file, _T("%6d %6d "), (int)((float)len * n / (end - start) / 1000), (int)((float)(w * h) * n / (end - start) / 1000)); + + if(psm.pal > 0) + { + start = clock(); + + for(int j = 0; j < n; j++) + { + (mem.*rtxP)(r, ptr, w, TEX0, TEXA); + } + + end = clock(); + + _ftprintf(file, _T("| %6d %6d "), (int)((float)len * n / (end - start) / 1000), (int)((float)(w * h) * n / (end - start) / 1000)); + } + + _ftprintf(file, _T("\n")); + + fflush(file); + } + + _ftprintf(file, _T("\n")); + } + + _aligned_free(ptr); + } + + // + + if(0) + { + GSLocalMemory mem; + + BYTE* ptr = (BYTE*)_aligned_malloc(1024 * 1024 * 4, 16); + + for(int i = 0; i < 1024 * 1024 * 4; i++) ptr[i] = (BYTE)i; + + const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[PSM_PSMCT32]; + + GSLocalMemory::writeImage wi = psm.wi; + + GIFRegBITBLTBUF BITBLTBUF; + + BITBLTBUF.DBP = 0; + BITBLTBUF.DBW = 32; + BITBLTBUF.DPSM = PSM_PSMCT32; + + GIFRegTRXPOS TRXPOS; + + TRXPOS.DSAX = 0; + TRXPOS.DSAY = 1; + + GIFRegTRXREG TRXREG; + + TRXREG.RRW = 256; + TRXREG.RRH = 256; + + int trlen = 256 * 256 * psm.trbpp / 8; + + int x = 0; + int y = 0; + + (mem.*wi)(x, y, ptr, trlen, BITBLTBUF, TRXPOS, TRXREG); + } + + // + + fclose(file); +} + diff --git a/plugins/GSdx/GS.h b/plugins/GSdx/GS.h new file mode 100644 index 0000000000..6623688670 --- /dev/null +++ b/plugins/GSdx/GS.h @@ -0,0 +1,1101 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * Special Notes: + * + * Register definitions and most of the enums originate from sps2dev-0.4.0 + * Copyright (C) 2002 Terratron Technologies Inc. All Rights Reserved. + * + */ + +#pragma once + +#define PLUGIN_VERSION 14 + +#include "GSVector.h" + +#pragma pack(push, 1) + +// +// sps2registers.h +// + +enum GS_REG +{ + GS_PMODE = 0x12000000, + GS_SMODE1 = 0x12000010, + GS_SMODE2 = 0x12000020, + GS_SRFSH = 0x12000030, + GS_SYNCH1 = 0x12000040, + GS_SYNCH2 = 0x12000050, + GS_SYNCV = 0x12000060, + GS_DISPFB1 = 0x12000070, + GS_DISPLAY1 = 0x12000080, + GS_DISPFB2 = 0x12000090, + GS_DISPLAY2 = 0x120000a0, + GS_EXTBUF = 0x120000b0, + GS_EXTDATA = 0x120000c0, + GS_EXTWRITE = 0x120000d0, + GS_BGCOLOR = 0x120000e0, + GS_UNKNOWN = 0x12000400, + GS_CSR = 0x12001000, + GS_IMR = 0x12001010, + GS_BUSDIR = 0x12001040, + GS_SIGLBLID = 0x12001080 +}; + +enum GS_PRIM +{ + GS_POINTLIST = 0, + GS_LINELIST = 1, + GS_LINESTRIP = 2, + GS_TRIANGLELIST = 3, + GS_TRIANGLESTRIP = 4, + GS_TRIANGLEFAN = 5, + GS_SPRITE = 6, + GS_INVALID = 7, +}; + +enum GS_PRIM_CLASS +{ + GS_POINT_CLASS = 0, + GS_LINE_CLASS = 1, + GS_TRIANGLE_CLASS = 2, + GS_SPRITE_CLASS = 3, + GS_INVALID_CLASS = 7, +}; + +enum GIF_REG +{ + GIF_REG_PRIM = 0x00, + GIF_REG_RGBA = 0x01, + GIF_REG_STQ = 0x02, + GIF_REG_UV = 0x03, + GIF_REG_XYZF2 = 0x04, + GIF_REG_XYZ2 = 0x05, + GIF_REG_TEX0_1 = 0x06, + GIF_REG_TEX0_2 = 0x07, + GIF_REG_CLAMP_1 = 0x08, + GIF_REG_CLAMP_2 = 0x09, + GIF_REG_FOG = 0x0a, + GIF_REG_XYZF3 = 0x0c, + GIF_REG_XYZ3 = 0x0d, + GIF_REG_A_D = 0x0e, + GIF_REG_NOP = 0x0f, +}; + +enum GIF_A_D_REG +{ + GIF_A_D_REG_PRIM = 0x00, + GIF_A_D_REG_RGBAQ = 0x01, + GIF_A_D_REG_ST = 0x02, + GIF_A_D_REG_UV = 0x03, + GIF_A_D_REG_XYZF2 = 0x04, + GIF_A_D_REG_XYZ2 = 0x05, + GIF_A_D_REG_TEX0_1 = 0x06, + GIF_A_D_REG_TEX0_2 = 0x07, + GIF_A_D_REG_CLAMP_1 = 0x08, + GIF_A_D_REG_CLAMP_2 = 0x09, + GIF_A_D_REG_FOG = 0x0a, + GIF_A_D_REG_XYZF3 = 0x0c, + GIF_A_D_REG_XYZ3 = 0x0d, + GIF_A_D_REG_NOP = 0x0f, + GIF_A_D_REG_TEX1_1 = 0x14, + GIF_A_D_REG_TEX1_2 = 0x15, + GIF_A_D_REG_TEX2_1 = 0x16, + GIF_A_D_REG_TEX2_2 = 0x17, + GIF_A_D_REG_XYOFFSET_1 = 0x18, + GIF_A_D_REG_XYOFFSET_2 = 0x19, + GIF_A_D_REG_PRMODECONT = 0x1a, + GIF_A_D_REG_PRMODE = 0x1b, + GIF_A_D_REG_TEXCLUT = 0x1c, + GIF_A_D_REG_SCANMSK = 0x22, + GIF_A_D_REG_MIPTBP1_1 = 0x34, + GIF_A_D_REG_MIPTBP1_2 = 0x35, + GIF_A_D_REG_MIPTBP2_1 = 0x36, + GIF_A_D_REG_MIPTBP2_2 = 0x37, + GIF_A_D_REG_TEXA = 0x3b, + GIF_A_D_REG_FOGCOL = 0x3d, + GIF_A_D_REG_TEXFLUSH = 0x3f, + GIF_A_D_REG_SCISSOR_1 = 0x40, + GIF_A_D_REG_SCISSOR_2 = 0x41, + GIF_A_D_REG_ALPHA_1 = 0x42, + GIF_A_D_REG_ALPHA_2 = 0x43, + GIF_A_D_REG_DIMX = 0x44, + GIF_A_D_REG_DTHE = 0x45, + GIF_A_D_REG_COLCLAMP = 0x46, + GIF_A_D_REG_TEST_1 = 0x47, + GIF_A_D_REG_TEST_2 = 0x48, + GIF_A_D_REG_PABE = 0x49, + GIF_A_D_REG_FBA_1 = 0x4a, + GIF_A_D_REG_FBA_2 = 0x4b, + GIF_A_D_REG_FRAME_1 = 0x4c, + GIF_A_D_REG_FRAME_2 = 0x4d, + GIF_A_D_REG_ZBUF_1 = 0x4e, + GIF_A_D_REG_ZBUF_2 = 0x4f, + GIF_A_D_REG_BITBLTBUF = 0x50, + GIF_A_D_REG_TRXPOS = 0x51, + GIF_A_D_REG_TRXREG = 0x52, + GIF_A_D_REG_TRXDIR = 0x53, + GIF_A_D_REG_HWREG = 0x54, + GIF_A_D_REG_SIGNAL = 0x60, + GIF_A_D_REG_FINISH = 0x61, + GIF_A_D_REG_LABEL = 0x62, +}; + +enum GIF_FLG +{ + GIF_FLG_PACKED = 0, + GIF_FLG_REGLIST = 1, + GIF_FLG_IMAGE = 2, + GIF_FLG_IMAGE2 = 3 +}; + +enum GS_PSM +{ + PSM_PSMCT32 = 0, // 0000-0000 + PSM_PSMCT24 = 1, // 0000-0001 + PSM_PSMCT16 = 2, // 0000-0010 + PSM_PSMCT16S = 10, // 0000-1010 + PSM_PSMT8 = 19, // 0001-0011 + PSM_PSMT4 = 20, // 0001-0100 + PSM_PSMT8H = 27, // 0001-1011 + PSM_PSMT4HL = 36, // 0010-0100 + PSM_PSMT4HH = 44, // 0010-1100 + PSM_PSMZ32 = 48, // 0011-0000 + PSM_PSMZ24 = 49, // 0011-0001 + PSM_PSMZ16 = 50, // 0011-0010 + PSM_PSMZ16S = 58, // 0011-1010 +}; + +enum GS_TFX +{ + TFX_MODULATE = 0, + TFX_DECAL = 1, + TFX_HIGHLIGHT = 2, + TFX_HIGHLIGHT2 = 3, + TFX_NONE = 4, +}; + +enum GS_CLAMP +{ + CLAMP_REPEAT = 0, + CLAMP_CLAMP = 1, + CLAMP_REGION_CLAMP = 2, + CLAMP_REGION_REPEAT = 3, +}; + +enum GS_ZTST +{ + ZTST_NEVER = 0, + ZTST_ALWAYS = 1, + ZTST_GEQUAL = 2, + ZTST_GREATER = 3, +}; + +enum GS_ATST +{ + ATST_NEVER = 0, + ATST_ALWAYS = 1, + ATST_LESS = 2, + ATST_LEQUAL = 3, + ATST_EQUAL = 4, + ATST_GEQUAL = 5, + ATST_GREATER = 6, + ATST_NOTEQUAL = 7, +}; + +enum GS_AFAIL +{ + AFAIL_KEEP = 0, + AFAIL_FB_ONLY = 1, + AFAIL_ZB_ONLY = 2, + AFAIL_RGB_ONLY = 3, +}; + +// +// sps2regstructs.h +// + +#define REG32(name) \ +union name \ +{ \ + UINT32 ai32; \ + struct { \ + +#define REG64(name) \ +union name \ +{ \ + UINT64 i64; \ + UINT32 ai32[2]; \ + void operator = (const GSVector4i& v) {GSVector4i::storel(this, v);} \ + operator GSVector4i() const {return GSVector4i::loadl(this);} \ + struct { \ + +#define REG128(name)\ +union name \ +{ \ + UINT64 ai64[2]; \ + UINT32 ai32[4]; \ + struct { \ + +#define REG32_(prefix, name) REG32(prefix##name) +#define REG64_(prefix, name) REG64(prefix##name) +#define REG128_(prefix, name) REG128(prefix##name) + +#define REG_END }; }; +#define REG_END2 }; + +#define REG32_SET(name) \ +union name \ +{ \ + UINT32 ai32; \ + +#define REG64_SET(name) \ +union name \ +{ \ + UINT64 i64; \ + UINT32 ai32[2]; \ + +#define REG128_SET(name)\ +union name \ +{ \ + __m128i ai128; \ + UINT64 ai64[2]; \ + UINT32 ai32[4]; \ + +#define REG_SET_END }; + +REG64_(GSReg, BGCOLOR) + UINT32 R:8; + UINT32 G:8; + UINT32 B:8; + UINT32 _PAD1:8; + UINT32 _PAD2:32; +REG_END + +REG64_(GSReg, BUSDIR) + UINT32 DIR:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GSReg, CSR) + UINT32 rSIGNAL:1; + UINT32 rFINISH:1; + UINT32 rHSINT:1; + UINT32 rVSINT:1; + UINT32 rEDWINT:1; + UINT32 rZERO1:1; + UINT32 rZERO2:1; + UINT32 r_PAD1:1; + UINT32 rFLUSH:1; + UINT32 rRESET:1; + UINT32 r_PAD2:2; + UINT32 rNFIELD:1; + UINT32 rFIELD:1; + UINT32 rFIFO:2; + UINT32 rREV:8; + UINT32 rID:8; + UINT32 wSIGNAL:1; + UINT32 wFINISH:1; + UINT32 wHSINT:1; + UINT32 wVSINT:1; + UINT32 wEDWINT:1; + UINT32 wZERO1:1; + UINT32 wZERO2:1; + UINT32 w_PAD1:1; + UINT32 wFLUSH:1; + UINT32 wRESET:1; + UINT32 w_PAD2:2; + UINT32 wNFIELD:1; + UINT32 wFIELD:1; + UINT32 wFIFO:2; + UINT32 wREV:8; + UINT32 wID:8; +REG_END + +REG64_(GSReg, DISPFB) // (-1/2) + UINT32 FBP:9; + UINT32 FBW:6; + UINT32 PSM:5; + UINT32 _PAD:12; + UINT32 DBX:11; + UINT32 DBY:11; + UINT32 _PAD2:10; +REG_END2 + UINT32 Block() {return FBP<<5;} +REG_END2 + +REG64_(GSReg, DISPLAY) // (-1/2) + UINT32 DX:12; + UINT32 DY:11; + UINT32 MAGH:4; + UINT32 MAGV:2; + UINT32 _PAD:3; + UINT32 DW:12; + UINT32 DH:11; + UINT32 _PAD2:9; +REG_END + +REG64_(GSReg, EXTBUF) + UINT32 EXBP:14; + UINT32 EXBW:6; + UINT32 FBIN:2; + UINT32 WFFMD:1; + UINT32 EMODA:2; + UINT32 EMODC:2; + UINT32 _PAD1:5; + UINT32 WDX:11; + UINT32 WDY:11; + UINT32 _PAD2:10; +REG_END + +REG64_(GSReg, EXTDATA) + UINT32 SX:12; + UINT32 SY:11; + UINT32 SMPH:4; + UINT32 SMPV:2; + UINT32 _PAD1:3; + UINT32 WW:12; + UINT32 WH:11; + UINT32 _PAD2:9; +REG_END + +REG64_(GSReg, EXTWRITE) + UINT32 WRITE:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GSReg, IMR) + UINT32 _PAD1:8; + UINT32 SIGMSK:1; + UINT32 FINISHMSK:1; + UINT32 HSMSK:1; + UINT32 VSMSK:1; + UINT32 EDWMSK:1; + UINT32 _PAD2:19; + UINT32 _PAD3:32; +REG_END + +REG64_(GSReg, PMODE) + UINT32 EN1:1; + UINT32 EN2:1; + UINT32 CRTMD:3; + UINT32 MMOD:1; + UINT32 AMOD:1; + UINT32 SLBG:1; + UINT32 ALP:8; + UINT32 _PAD:16; + UINT32 _PAD1:32; +REG_END + +REG64_(GSReg, SIGLBLID) + UINT32 SIGID:32; + UINT32 LBLID:32; +REG_END + +REG64_(GSReg, SMODE1) + UINT32 RC:3; + UINT32 LC:7; + UINT32 T1248:2; + UINT32 SLCK:1; + UINT32 CMOD:2; + UINT32 EX:1; + UINT32 PRST:1; + UINT32 SINT:1; + UINT32 XPCK:1; + UINT32 PCK2:2; + UINT32 SPML:4; + UINT32 GCONT:1; // YCrCb + UINT32 PHS:1; + UINT32 PVS:1; + UINT32 PEHS:1; + UINT32 PEVS:1; + UINT32 CLKSEL:2; + UINT32 NVCK:1; + UINT32 SLCK2:1; + UINT32 VCKSEL:2; + UINT32 VHP:1; + UINT32 _PAD1:27; +REG_END + +/* + +// pal + +CLKSEL=1 CMOD=3 EX=0 GCONT=0 LC=32 NVCK=1 PCK2=0 PEHS=0 PEVS=0 PHS=0 PRST=1 PVS=0 RC=4 SINT=0 SLCK=0 SLCK2=1 SPML=4 T1248=1 VCKSEL=1 VHP=0 XPCK=0 + +// ntsc + +CLKSEL=1 CMOD=2 EX=0 GCONT=0 LC=32 NVCK=1 PCK2=0 PEHS=0 PEVS=0 PHS=0 PRST=1 PVS=0 RC=4 SINT=0 SLCK=0 SLCK2=1 SPML=4 T1248=1 VCKSEL=1 VHP=0 XPCK=0 + +// ntsc progressive (SoTC) + +CLKSEL=1 CMOD=0 EX=0 GCONT=0 LC=32 NVCK=1 PCK2=0 PEHS=0 PEVS=0 PHS=0 PRST=1 PVS=0 RC=4 SINT=0 SLCK=0 SLCK2=1 SPML=2 T1248=1 VCKSEL=1 VHP=1 XPCK=0 + +*/ + +REG64_(GSReg, SMODE2) + UINT32 INT:1; + UINT32 FFMD:1; + UINT32 DPMS:2; + UINT32 _PAD2:28; + UINT32 _PAD3:32; +REG_END + +REG64_SET(GSReg) + GSRegBGCOLOR BGCOLOR; + GSRegBUSDIR BUSDIR; + GSRegCSR CSR; + GSRegDISPFB DISPFB; + GSRegDISPLAY DISPLAY; + GSRegEXTBUF EXTBUF; + GSRegEXTDATA EXTDATA; + GSRegEXTWRITE EXTWRITE; + GSRegIMR IMR; + GSRegPMODE PMODE; + GSRegSIGLBLID SIGLBLID; + GSRegSMODE1 SMODE1; + GSRegSMODE2 SMODE2; +REG_SET_END + +// +// sps2tags.h +// + +#define SET_GIF_REG(gifTag, iRegNo, uiValue) \ + {((GIFTag*)&gifTag)->ai64[1] |= (((uiValue) & 0xf) << ((iRegNo) << 2));} + +#ifdef _M_AMD64 +#define GET_GIF_REG(tag, reg) \ + (((tag).ai64[1] >> ((reg) << 2)) & 0xf) +#else +#define GET_GIF_REG(tag, reg) \ + (((tag).ai32[2 + ((reg) >> 3)] >> (((reg) & 7) << 2)) & 0xf) +#endif + +// +// GIFTag + +REG128(GIFTag) + UINT32 NLOOP:15; + UINT32 EOP:1; + UINT32 _PAD1:16; + UINT32 _PAD2:14; + UINT32 PRE:1; + UINT32 PRIM:11; + UINT32 FLG:2; // enum GIF_FLG + UINT32 NREG:4; + UINT64 REGS:64; +REG_END + +// GIFReg + +REG64_(GIFReg, ALPHA) + UINT32 A:2; + UINT32 B:2; + UINT32 C:2; + UINT32 D:2; + UINT32 _PAD1:24; + UINT32 FIX:8; + UINT32 _PAD2:24; +REG_END2 + __forceinline bool IsOpaque() const {return (A == B || C == 2 && FIX == 0) && D == 0 || (A == 0 && B == 2 && C == 2 && D == 2 && FIX == 0x80);} // output will be Cs/As +REG_END2 + +REG64_(GIFReg, BITBLTBUF) + UINT32 SBP:14; + UINT32 _PAD1:2; + UINT32 SBW:6; + UINT32 _PAD2:2; + UINT32 SPSM:6; + UINT32 _PAD3:2; + UINT32 DBP:14; + UINT32 _PAD4:2; + UINT32 DBW:6; + UINT32 _PAD5:2; + UINT32 DPSM:6; + UINT32 _PAD6:2; +REG_END + +REG64_(GIFReg, CLAMP) +union +{ + struct + { + UINT32 WMS:2; + UINT32 WMT:2; + UINT32 MINU:10; + UINT32 MAXU:10; + UINT32 _PAD1:8; + UINT32 _PAD2:2; + UINT32 MAXV:10; + UINT32 _PAD3:20; + }; + + struct + { + UINT64 _PAD4:24; + UINT64 MINV:10; + UINT64 _PAD5:30; + }; +}; +REG_END + +REG64_(GIFReg, COLCLAMP) + UINT32 CLAMP:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, DIMX) + UINT32 DM00:3; + UINT32 _PAD00:1; + UINT32 DM01:3; + UINT32 _PAD01:1; + UINT32 DM02:3; + UINT32 _PAD02:1; + UINT32 DM03:3; + UINT32 _PAD03:1; + + UINT32 DM10:3; + UINT32 _PAD10:1; + UINT32 DM11:3; + UINT32 _PAD11:1; + UINT32 DM12:3; + UINT32 _PAD12:1; + UINT32 DM13:3; + UINT32 _PAD13:1; + + UINT32 DM20:3; + UINT32 _PAD20:1; + UINT32 DM21:3; + UINT32 _PAD21:1; + UINT32 DM22:3; + UINT32 _PAD22:1; + UINT32 DM23:3; + UINT32 _PAD23:1; + + UINT32 DM30:3; + UINT32 _PAD30:1; + UINT32 DM31:3; + UINT32 _PAD31:1; + UINT32 DM32:3; + UINT32 _PAD32:1; + UINT32 DM33:3; + UINT32 _PAD33:1; +REG_END + +REG64_(GIFReg, DTHE) + UINT32 DTHE:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, FBA) + UINT32 FBA:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, FINISH) + UINT32 _PAD1:32; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, FOG) + UINT32 _PAD1:32; + UINT32 _PAD2:24; + UINT32 F:8; +REG_END + +REG64_(GIFReg, FOGCOL) + UINT32 FCR:8; + UINT32 FCG:8; + UINT32 FCB:8; + UINT32 _PAD1:8; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, FRAME) + UINT32 FBP:9; + UINT32 _PAD1:7; + UINT32 FBW:6; + UINT32 _PAD2:2; + UINT32 PSM:6; + UINT32 _PAD3:2; + UINT32 FBMSK:32; +REG_END2 + UINT32 Block() const {return FBP << 5;} +REG_END2 + +REG64_(GIFReg, HWREG) + UINT32 DATA_LOWER:32; + UINT32 DATA_UPPER:32; +REG_END + +REG64_(GIFReg, LABEL) + UINT32 ID:32; + UINT32 IDMSK:32; +REG_END + +REG64_(GIFReg, MIPTBP1) + UINT64 TBP1:14; + UINT64 TBW1:6; + UINT64 TBP2:14; + UINT64 TBW2:6; + UINT64 TBP3:14; + UINT64 TBW3:6; + UINT64 _PAD:4; +REG_END + +REG64_(GIFReg, MIPTBP2) + UINT64 TBP4:14; + UINT64 TBW4:6; + UINT64 TBP5:14; + UINT64 TBW5:6; + UINT64 TBP6:14; + UINT64 TBW6:6; + UINT64 _PAD:4; +REG_END + +REG64_(GIFReg, NOP) + UINT32 _PAD1:32; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, PABE) + UINT32 PABE:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, PRIM) + UINT32 PRIM:3; + UINT32 IIP:1; + UINT32 TME:1; + UINT32 FGE:1; + UINT32 ABE:1; + UINT32 AA1:1; + UINT32 FST:1; + UINT32 CTXT:1; + UINT32 FIX:1; + UINT32 _PAD1:21; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, PRMODE) + UINT32 _PRIM:3; + UINT32 IIP:1; + UINT32 TME:1; + UINT32 FGE:1; + UINT32 ABE:1; + UINT32 AA1:1; + UINT32 FST:1; + UINT32 CTXT:1; + UINT32 FIX:1; + UINT32 _PAD2:21; + UINT32 _PAD3:32; +REG_END + +REG64_(GIFReg, PRMODECONT) + UINT32 AC:1; + UINT32 _PAD1:31; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, RGBAQ) + UINT32 R:8; + UINT32 G:8; + UINT32 B:8; + UINT32 A:8; + float Q; +REG_END + +REG64_(GIFReg, SCANMSK) + UINT32 MSK:2; + UINT32 _PAD1:30; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, SCISSOR) + UINT32 SCAX0:11; + UINT32 _PAD1:5; + UINT32 SCAX1:11; + UINT32 _PAD2:5; + UINT32 SCAY0:11; + UINT32 _PAD3:5; + UINT32 SCAY1:11; + UINT32 _PAD4:5; +REG_END + +REG64_(GIFReg, SIGNAL) + UINT32 ID:32; + UINT32 IDMSK:32; +REG_END + +REG64_(GIFReg, ST) + float S; + float T; +REG_END + +REG64_(GIFReg, TEST) + UINT32 ATE:1; + UINT32 ATST:3; + UINT32 AREF:8; + UINT32 AFAIL:2; + UINT32 DATE:1; + UINT32 DATM:1; + UINT32 ZTE:1; + UINT32 ZTST:2; + UINT32 _PAD1:13; + UINT32 _PAD2:32; +REG_END2 + __forceinline bool DoFirstPass() {return !ATE || ATST != 0;} // not all pixels fail automatically + __forceinline bool DoSecondPass() {return ATE && ATST != 1 && AFAIL != 0;} // pixels may fail, write fb/z + __forceinline bool NoSecondPass() {return ATE && ATST != 1 && AFAIL == 0;} // pixels may fail, no output +REG_END2 + +REG64_(GIFReg, TEX0) +union +{ + struct + { + UINT32 TBP0:14; + UINT32 TBW:6; + UINT32 PSM:6; + UINT32 TW:4; + UINT32 _PAD1:2; + UINT32 _PAD2:2; + UINT32 TCC:1; + UINT32 TFX:2; + UINT32 CBP:14; + UINT32 CPSM:4; + UINT32 CSM:1; + UINT32 CSA:5; + UINT32 CLD:3; + }; + + struct + { + UINT64 _PAD3:30; + UINT64 TH:4; + UINT64 _PAD4:30; + }; +}; +REG_END + +REG64_(GIFReg, TEX1) + UINT32 LCM:1; + UINT32 _PAD1:1; + UINT32 MXL:3; + UINT32 MMAG:1; + UINT32 MMIN:3; + UINT32 MTBA:1; + UINT32 _PAD2:9; + UINT32 L:2; + UINT32 _PAD3:11; + UINT32 K:12; + UINT32 _PAD4:20; +REG_END2 + bool IsLinear() const + { + bool mmag = (MMAG & 1); + bool mmin = (MMIN == 1) || (MMIN & 4); + + return !LCM ? mmag || mmin : K <= 0 ? mmag : mmin; + } +REG_END2 + +REG64_(GIFReg, TEX2) + UINT32 _PAD1:20; + UINT32 PSM:6; + UINT32 _PAD2:6; + UINT32 _PAD3:5; + UINT32 CBP:14; + UINT32 CPSM:4; + UINT32 CSM:1; + UINT32 CSA:5; + UINT32 CLD:3; +REG_END + +REG64_(GIFReg, TEXA) + UINT32 TA0:8; + UINT32 _PAD1:7; + UINT32 AEM:1; + UINT32 _PAD2:16; + UINT32 TA1:8; + UINT32 _PAD3:24; +REG_END + +REG64_(GIFReg, TEXCLUT) + UINT32 CBW:6; + UINT32 COU:6; + UINT32 COV:10; + UINT32 _PAD1:10; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, TEXFLUSH) + UINT32 _PAD1:32; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, TRXDIR) + UINT32 XDIR:2; + UINT32 _PAD1:30; + UINT32 _PAD2:32; +REG_END + +REG64_(GIFReg, TRXPOS) + UINT32 SSAX:11; + UINT32 _PAD1:5; + UINT32 SSAY:11; + UINT32 _PAD2:5; + UINT32 DSAX:11; + UINT32 _PAD3:5; + UINT32 DSAY:11; + UINT32 DIR:2; + UINT32 _PAD4:3; +REG_END + +REG64_(GIFReg, TRXREG) + UINT32 RRW:12; + UINT32 _PAD1:20; + UINT32 RRH:12; + UINT32 _PAD2:20; +REG_END + +// GSState::GIFPackedRegHandlerUV and GSState::GIFRegHandlerUV will make sure that the _PAD1/2 bits are set to zero + +REG64_(GIFReg, UV) + UINT32 U:16; +// UINT32 _PAD1:2; + UINT32 V:16; +// UINT32 _PAD2:2; + UINT32 _PAD3:32; +REG_END + +// GSState::GIFRegHandlerXYOFFSET will make sure that the _PAD1/2 bits are set to zero + +REG64_(GIFReg, XYOFFSET) + UINT32 OFX; // :16; UINT32 _PAD1:16; + UINT32 OFY; // :16; UINT32 _PAD2:16; +REG_END + +REG64_(GIFReg, XYZ) + UINT32 X:16; + UINT32 Y:16; + UINT32 Z:32; +REG_END + +REG64_(GIFReg, XYZF) + UINT32 X:16; + UINT32 Y:16; + UINT32 Z:24; + UINT32 F:8; +REG_END + +REG64_(GIFReg, ZBUF) + UINT32 ZBP:9; + UINT32 _PAD1:15; + // UINT32 PSM:4; + // UINT32 _PAD2:4; + UINT32 PSM:6; + UINT32 _PAD2:2; + UINT32 ZMSK:1; + UINT32 _PAD3:31; +REG_END2 + UINT32 Block() const {return ZBP << 5;} +REG_END2 + +REG64_SET(GIFReg) + GIFRegALPHA ALPHA; + GIFRegBITBLTBUF BITBLTBUF; + GIFRegCLAMP CLAMP; + GIFRegCOLCLAMP COLCLAMP; + GIFRegDIMX DIMX; + GIFRegDTHE DTHE; + GIFRegFBA FBA; + GIFRegFINISH FINISH; + GIFRegFOG FOG; + GIFRegFOGCOL FOGCOL; + GIFRegFRAME FRAME; + GIFRegHWREG HWREG; + GIFRegLABEL LABEL; + GIFRegMIPTBP1 MIPTBP1; + GIFRegMIPTBP2 MIPTBP2; + GIFRegNOP NOP; + GIFRegPABE PABE; + GIFRegPRIM PRIM; + GIFRegPRMODE PRMODE; + GIFRegPRMODECONT PRMODECONT; + GIFRegRGBAQ RGBAQ; + GIFRegSCANMSK SCANMSK; + GIFRegSCISSOR SCISSOR; + GIFRegSIGNAL SIGNAL; + GIFRegST ST; + GIFRegTEST TEST; + GIFRegTEX0 TEX0; + GIFRegTEX1 TEX1; + GIFRegTEX2 TEX2; + GIFRegTEXA TEXA; + GIFRegTEXCLUT TEXCLUT; + GIFRegTEXFLUSH TEXFLUSH; + GIFRegTRXDIR TRXDIR; + GIFRegTRXPOS TRXPOS; + GIFRegTRXREG TRXREG; + GIFRegUV UV; + GIFRegXYOFFSET XYOFFSET; + GIFRegXYZ XYZ; + GIFRegXYZF XYZF; + GIFRegZBUF ZBUF; +REG_SET_END + +// GIFPacked + +REG128_(GIFPacked, PRIM) + UINT32 PRIM:11; + UINT32 _PAD1:21; + UINT32 _PAD2:32; + UINT32 _PAD3:32; + UINT32 _PAD4:32; +REG_END + +REG128_(GIFPacked, RGBA) + UINT32 R:8; + UINT32 _PAD1:24; + UINT32 G:8; + UINT32 _PAD2:24; + UINT32 B:8; + UINT32 _PAD3:24; + UINT32 A:8; + UINT32 _PAD4:24; +REG_END + +REG128_(GIFPacked, STQ) + float S; + float T; + float Q; + UINT32 _PAD1:32; +REG_END + +REG128_(GIFPacked, UV) + UINT32 U:14; + UINT32 _PAD1:18; + UINT32 V:14; + UINT32 _PAD2:18; + UINT32 _PAD3:32; + UINT32 _PAD4:32; +REG_END + +REG128_(GIFPacked, XYZF2) + UINT32 X:16; + UINT32 _PAD1:16; + UINT32 Y:16; + UINT32 _PAD2:16; + UINT32 _PAD3:4; + UINT32 Z:24; + UINT32 _PAD4:4; + UINT32 _PAD5:4; + UINT32 F:8; + UINT32 _PAD6:3; + UINT32 ADC:1; + UINT32 _PAD7:16; +REG_END + +REG128_(GIFPacked, XYZ2) + UINT32 X:16; + UINT32 _PAD1:16; + UINT32 Y:16; + UINT32 _PAD2:16; + UINT32 Z:32; + UINT32 _PAD3:15; + UINT32 ADC:1; + UINT32 _PAD4:16; +REG_END + +REG128_(GIFPacked, FOG) + UINT32 _PAD1:32; + UINT32 _PAD2:32; + UINT32 _PAD3:32; + UINT32 _PAD4:4; + UINT32 F:8; + UINT32 _PAD5:20; +REG_END + +REG128_(GIFPacked, A_D) + UINT64 DATA:64; + UINT32 ADDR:8; // enum GIF_A_D_REG + UINT32 _PAD1:24; + UINT32 _PAD2:32; +REG_END + +REG128_(GIFPacked, NOP) + UINT32 _PAD1:32; + UINT32 _PAD2:32; + UINT32 _PAD3:32; + UINT32 _PAD4:32; +REG_END + +REG128_SET(GIFPackedReg) + GIFReg r; + GIFPackedPRIM PRIM; + GIFPackedRGBA RGBA; + GIFPackedSTQ STQ; + GIFPackedUV UV; + GIFPackedXYZF2 XYZF2; + GIFPackedXYZ2 XYZ2; + GIFPackedFOG FOG; + GIFPackedA_D A_D; + GIFPackedNOP NOP; +REG_SET_END + +__declspec(align(16)) struct GIFPath +{ + GIFTag tag; + UINT32 nreg; + UINT32 _pad[3]; + GSVector4i regs; + + void SetTag(const void* mem) + { + GSVector4i v = GSVector4i::load(mem); + GSVector4i::store(&tag, v); + nreg = 0; + regs = v.uph8(v >> 4) & 0x0f0f0f0f; + } + + DWORD GetReg() + { + return regs.u8[nreg]; // (DWORD)GET_GIF_REG(tag, nreg); + } +}; + +#pragma pack(pop) + +enum {KEYPRESS=1, KEYRELEASE=2}; +struct keyEvent {UINT32 key, event;}; + +enum {FREEZE_LOAD=0, FREEZE_SAVE=1, FREEZE_SIZE=2}; +struct GSFreezeData {int size; BYTE* data;}; + +enum stateType {ST_WRITE, ST_TRANSFER, ST_VSYNC}; diff --git a/plugins/GSdx/GSAlignedClass.cpp b/plugins/GSdx/GSAlignedClass.cpp new file mode 100644 index 0000000000..e9cca21b8a --- /dev/null +++ b/plugins/GSdx/GSAlignedClass.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSAlignedClass.h" diff --git a/plugins/GSdx/GSAlignedClass.h b/plugins/GSdx/GSAlignedClass.h new file mode 100644 index 0000000000..69ee8d1025 --- /dev/null +++ b/plugins/GSdx/GSAlignedClass.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +template class GSAlignedClass +{ +public: + GSAlignedClass() {} + + void* operator new (size_t size) + { + return _aligned_malloc(size, i); + } + + void operator delete (void* p) + { + _aligned_free(p); + } + + void* operator new [] (size_t size) + { + return _aligned_malloc(size, i); + } + + void operator delete [] (void* p) + { + _aligned_free(p); + } +}; diff --git a/plugins/GSdx/GSBlock.cpp b/plugins/GSdx/GSBlock.cpp new file mode 100644 index 0000000000..1a8554290f --- /dev/null +++ b/plugins/GSdx/GSBlock.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSBlock.h" + +const GSVector4i GSBlock::m_r16mask(0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15); +const GSVector4i GSBlock::m_r8mask(0, 4, 2, 6, 8, 12, 10, 14, 1, 5, 3, 7, 9, 13, 11, 15); +const GSVector4i GSBlock::m_r4mask(0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15); + +const GSVector4i GSBlock::m_xxxa(0x00008000); +const GSVector4i GSBlock::m_xxbx(0x00007c00); +const GSVector4i GSBlock::m_xgxx(0x000003e0); +const GSVector4i GSBlock::m_rxxx(0x0000001f); + +const GSVector4i GSBlock::m_uw8hmask0 = GSVector4i(0, 0, 0, 0, 1, 1, 1, 1, 8, 8, 8, 8, 9, 9, 9, 9); +const GSVector4i GSBlock::m_uw8hmask1 = GSVector4i(2, 2, 2, 2, 3, 3, 3, 3, 10, 10, 10, 10, 11, 11, 11, 11); +const GSVector4i GSBlock::m_uw8hmask2 = GSVector4i(4, 4, 4, 4, 5, 5, 5, 5, 12, 12, 12, 12, 13, 13, 13, 13); +const GSVector4i GSBlock::m_uw8hmask3 = GSVector4i(6, 6, 6, 6, 7, 7, 7, 7, 14, 14, 14, 14, 15, 15, 15, 15); diff --git a/plugins/GSdx/GSBlock.h b/plugins/GSdx/GSBlock.h new file mode 100644 index 0000000000..6885b07f21 --- /dev/null +++ b/plugins/GSdx/GSBlock.h @@ -0,0 +1,2289 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSTables.h" +#include "GSVector.h" + +class GSBlock +{ + static const GSVector4i m_r16mask; + static const GSVector4i m_r8mask; + static const GSVector4i m_r4mask; + + static const GSVector4i m_xxxa; + static const GSVector4i m_xxbx; + static const GSVector4i m_xgxx; + static const GSVector4i m_rxxx; + + static const GSVector4i m_uw8hmask0; + static const GSVector4i m_uw8hmask1; + static const GSVector4i m_uw8hmask2; + static const GSVector4i m_uw8hmask3; + +public: + template __forceinline static void WriteColumn32(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s0 = (const GSVector4i*)&src[srcpitch * 0]; + const GSVector4i* s1 = (const GSVector4i*)&src[srcpitch * 1]; + + GSVector4i v0 = GSVector4i::load(&s0[0]); + GSVector4i v1 = GSVector4i::load(&s0[1]); + GSVector4i v2 = GSVector4i::load(&s1[0]); + GSVector4i v3 = GSVector4i::load(&s1[1]); + + GSVector4i::sw64(v0, v2, v1, v3); + + if(mask == 0xffffffff) + { + ((GSVector4i*)dst)[i * 4 + 0] = v0; + ((GSVector4i*)dst)[i * 4 + 1] = v1; + ((GSVector4i*)dst)[i * 4 + 2] = v2; + ((GSVector4i*)dst)[i * 4 + 3] = v3; + } + else + { + GSVector4i v4((int)mask); + + #if _M_SSE >= 0x401 + + if(mask == 0xff000000 || mask == 0x00ffffff) + { + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend8(v0, v4); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend8(v1, v4); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend8(v2, v4); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend8(v3, v4); + } + else + { + + #endif + + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend(v0, v4); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend(v1, v4); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend(v2, v4); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend(v3, v4); + + #if _M_SSE >= 0x401 + + } + + #endif + } + + #else + + const BYTE* d = &columnTable32[(i & 3) << 1][0]; + + for(int j = 0; j < 2; j++, d += 8, src += srcpitch) + { + for(int i = 0; i < 8; i++) + { + if(mask == 0xffffffff) + { + ((DWORD*)dst)[d[i]] = ((DWORD*)src)[i]; + } + else + { + ((DWORD*)dst)[d[i]] = (((DWORD*)dst)[d[i]] & ~mask) | (((DWORD*)src)[i] & mask); + } + } + } + + #endif + } + + template __forceinline static void WriteColumn16(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s0 = (const GSVector4i*)&src[srcpitch * 0]; + const GSVector4i* s1 = (const GSVector4i*)&src[srcpitch * 1]; + + GSVector4i v0 = GSVector4i::load(&s0[0]); + GSVector4i v1 = GSVector4i::load(&s0[1]); + GSVector4i v2 = GSVector4i::load(&s1[0]); + GSVector4i v3 = GSVector4i::load(&s1[1]); + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw64(v0, v1, v2, v3); + + ((GSVector4i*)dst)[i * 4 + 0] = v0; + ((GSVector4i*)dst)[i * 4 + 1] = v2; + ((GSVector4i*)dst)[i * 4 + 2] = v1; + ((GSVector4i*)dst)[i * 4 + 3] = v3; + + #else + + const BYTE* d = &columnTable16[(i & 3) << 1][0]; + + for(int j = 0; j < 2; j++, d += 16, src += srcpitch) + { + for(int i = 0; i < 16; i++) + { + ((WORD*)dst)[d[i]] = ((WORD*)src)[i]; + } + } + + #endif + } + + template __forceinline static void WriteColumn8(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + GSVector4i v0 = GSVector4i::load(&src[srcpitch * 0]); + GSVector4i v1 = GSVector4i::load(&src[srcpitch * 1]); + GSVector4i v2 = GSVector4i::load(&src[srcpitch * 2]); + GSVector4i v3 = GSVector4i::load(&src[srcpitch * 3]); + + if((i & 1) == 0) + { + v2 = v2.yxwz(); + v3 = v3.yxwz(); + } + else + { + v0 = v0.yxwz(); + v1 = v1.yxwz(); + } + + GSVector4i::sw8(v0, v2, v1, v3); + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw64(v0, v1, v2, v3); + + ((GSVector4i*)dst)[i * 4 + 0] = v0; + ((GSVector4i*)dst)[i * 4 + 1] = v2; + ((GSVector4i*)dst)[i * 4 + 2] = v1; + ((GSVector4i*)dst)[i * 4 + 3] = v3; + + #else + + const BYTE* d = &columnTable8[(i & 3) << 2][0]; + + for(int j = 0; j < 4; j++, d += 16, src += srcpitch) + { + for(int i = 0; i < 16; i++) + { + dst[d[i]] = src[i]; + } + } + + #endif + } + + template __forceinline static void WriteColumn4(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + // TODO: pshufb + + #if _M_SSE >= 0x200 + + GSVector4i v0 = GSVector4i::load(&src[srcpitch * 0]); + GSVector4i v1 = GSVector4i::load(&src[srcpitch * 1]); + GSVector4i v2 = GSVector4i::load(&src[srcpitch * 2]); + GSVector4i v3 = GSVector4i::load(&src[srcpitch * 3]); + + if((i & 1) == 0) + { + v2 = v2.yxwzlh(); + v3 = v3.yxwzlh(); + } + else + { + v0 = v0.yxwzlh(); + v1 = v1.yxwzlh(); + } + + GSVector4i::sw4(v0, v2, v1, v3); + GSVector4i::sw8(v0, v1, v2, v3); + GSVector4i::sw8(v0, v2, v1, v3); + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 4 + 0] = v0; + ((GSVector4i*)dst)[i * 4 + 1] = v1; + ((GSVector4i*)dst)[i * 4 + 2] = v2; + ((GSVector4i*)dst)[i * 4 + 3] = v3; + + #else + + const WORD* d = &columnTable4[(i & 3) << 2][0]; + + for(int j = 0; j < 4; j++, d += 32, src += srcpitch) + { + for(int i = 0; i < 32; i++) + { + DWORD addr = d[i]; + BYTE c = (src[i >> 1] >> ((i & 1) << 2)) & 0x0f; + DWORD shift = (addr & 1) << 2; + dst[addr >> 1] = (dst[addr >> 1] & (0xf0 >> shift)) | (c << shift); + } + } + + #endif + } + + template static void WriteColumn32(int y, BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + switch((y >> 1) & 3) + { + case 0: WriteColumn32<0, aligned, mask>(dst, src, srcpitch); break; + case 1: WriteColumn32<1, aligned, mask>(dst, src, srcpitch); break; + case 2: WriteColumn32<2, aligned, mask>(dst, src, srcpitch); break; + case 3: WriteColumn32<3, aligned, mask>(dst, src, srcpitch); break; + default: __assume(0); + } + } + + template static void WriteColumn16(int y, BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + switch((y >> 1) & 3) + { + case 0: WriteColumn16<0, aligned>(dst, src, srcpitch); break; + case 1: WriteColumn16<1, aligned>(dst, src, srcpitch); break; + case 2: WriteColumn16<2, aligned>(dst, src, srcpitch); break; + case 3: WriteColumn16<3, aligned>(dst, src, srcpitch); break; + default: __assume(0); + } + } + + template static void WriteColumn8(int y, BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + switch((y >> 2) & 3) + { + case 0: WriteColumn8<0, aligned>(dst, src, srcpitch); break; + case 1: WriteColumn8<1, aligned>(dst, src, srcpitch); break; + case 2: WriteColumn8<2, aligned>(dst, src, srcpitch); break; + case 3: WriteColumn8<3, aligned>(dst, src, srcpitch); break; + default: __assume(0); + } + } + + template static void WriteColumn4(int y, BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + switch((y >> 2) & 3) + { + case 0: WriteColumn4<0, aligned>(dst, src, srcpitch); break; + case 1: WriteColumn4<1, aligned>(dst, src, srcpitch); break; + case 2: WriteColumn4<2, aligned>(dst, src, srcpitch); break; + case 3: WriteColumn4<3, aligned>(dst, src, srcpitch); break; + default: __assume(0); + } + } + + template static void WriteBlock32(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + WriteColumn32<0, aligned, mask>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn32<1, aligned, mask>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn32<2, aligned, mask>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn32<3, aligned, mask>(dst, src, srcpitch); + + #else + + const BYTE* d = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, d += 8, src += srcpitch) + { + for(int i = 0; i < 8; i++) + { + if(mask == 0xffffffff) + { + ((DWORD*)dst)[d[i]] = ((DWORD*)src)[i]; + } + else + { + ((DWORD*)dst)[d[i]] = (((DWORD*)dst)[d[i]] & ~mask) | (((DWORD*)src)[i] & mask); + } + } + } + + #endif + } + + template static void WriteBlock16(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + WriteColumn16<0, aligned>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn16<1, aligned>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn16<2, aligned>(dst, src, srcpitch); + src += srcpitch * 2; + WriteColumn16<3, aligned>(dst, src, srcpitch); + + #else + + const BYTE* d = &columnTable16[0][0]; + + for(int j = 0; j < 8; j++, d += 16, src += srcpitch) + { + for(int i = 0; i < 16; i++) + { + ((WORD*)dst)[d[i]] = ((WORD*)src)[i]; + } + } + + #endif + } + + template static void WriteBlock8(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + WriteColumn8<0, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn8<1, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn8<2, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn8<3, aligned>(dst, src, srcpitch); + + #else + + const BYTE* d = &columnTable8[0][0]; + + for(int j = 0; j < 16; j++, d += 16, src += srcpitch) + { + for(int i = 0; i < 16; i++) + { + dst[d[i]] = src[i]; + } + } + + #endif + } + + template static void WriteBlock4(BYTE* RESTRICT dst, const BYTE* RESTRICT src, int srcpitch) + { + #if _M_SSE >= 0x200 + + WriteColumn4<0, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn4<1, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn4<2, aligned>(dst, src, srcpitch); + src += srcpitch * 4; + WriteColumn4<3, aligned>(dst, src, srcpitch); + + #else + + const WORD* d = &columnTable4[0][0]; + + for(int j = 0; j < 16; j++, d += 32, src += srcpitch) + { + for(int i = 0; i < 32; i++) + { + DWORD addr = d[i]; + BYTE c = (src[i >> 1] >> ((i & 1) << 2)) & 0x0f; + DWORD shift = (addr & 1) << 2; + dst[addr >> 1] = (dst[addr >> 1] & (0xf0 >> shift)) | (c << shift); + } + } + + #endif + } + + template __forceinline static void ReadColumn32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0]; + GSVector4i v1 = s[i * 4 + 1]; + GSVector4i v2 = s[i * 4 + 2]; + GSVector4i v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + GSVector4i* d0 = (GSVector4i*)&dst[dstpitch * 0]; + GSVector4i* d1 = (GSVector4i*)&dst[dstpitch * 1]; + + GSVector4i::store(&d0[0], v0); + GSVector4i::store(&d0[1], v1); + GSVector4i::store(&d1[0], v2); + GSVector4i::store(&d1[1], v3); + + #else + + const BYTE* s = &columnTable32[(i & 3) << 1][0]; + + for(int j = 0; j < 2; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[i] = ((DWORD*)src)[s[i]]; + } + } + + #endif + } + + template __forceinline static void ReadColumn16(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x301 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0].shuffle8(m_r16mask); + GSVector4i v1 = s[i * 4 + 1].shuffle8(m_r16mask); + GSVector4i v2 = s[i * 4 + 2].shuffle8(m_r16mask); + GSVector4i v3 = s[i * 4 + 3].shuffle8(m_r16mask); + + GSVector4i::sw32(v0, v1, v2, v3); + GSVector4i::sw64(v0, v1, v2, v3); + + GSVector4i* d0 = (GSVector4i*)&dst[dstpitch * 0]; + GSVector4i* d1 = (GSVector4i*)&dst[dstpitch * 1]; + + GSVector4i::store(&d0[0], v0); + GSVector4i::store(&d0[1], v2); + GSVector4i::store(&d1[0], v1); + GSVector4i::store(&d1[1], v3); + + #elif _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0]; + GSVector4i v1 = s[i * 4 + 1]; + GSVector4i v2 = s[i * 4 + 2]; + GSVector4i v3 = s[i * 4 + 3]; + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v2, v3); + GSVector4i::sw16(v0, v2, v1, v3); + + GSVector4i* d0 = (GSVector4i*)&dst[dstpitch * 0]; + GSVector4i* d1 = (GSVector4i*)&dst[dstpitch * 1]; + + GSVector4i::store(&d0[0], v0); + GSVector4i::store(&d0[1], v1); + GSVector4i::store(&d1[0], v2); + GSVector4i::store(&d1[1], v3); + + #else + + const BYTE* s = &columnTable16[(i & 3) << 1][0]; + + for(int j = 0; j < 2; j++, s += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + ((WORD*)dst)[i] = ((WORD*)src)[s[i]]; + } + } + + #endif + } + + template __forceinline static void ReadColumn8(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x301 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + if((i & 1) == 0) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + } + else + { + v2 = s[i * 4 + 0]; + v3 = s[i * 4 + 1]; + v0 = s[i * 4 + 2]; + v1 = s[i * 4 + 3]; + } + + v0 = v0.shuffle8(m_r8mask); + v1 = v1.shuffle8(m_r8mask); + v2 = v2.shuffle8(m_r8mask); + v3 = v3.shuffle8(m_r8mask); + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v3, v2); + + GSVector4i::store(&dst[dstpitch * 0], v0); + GSVector4i::store(&dst[dstpitch * 1], v3); + GSVector4i::store(&dst[dstpitch * 2], v1); + GSVector4i::store(&dst[dstpitch * 3], v2); + + #elif _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0]; + GSVector4i v1 = s[i * 4 + 1]; + GSVector4i v2 = s[i * 4 + 2]; + GSVector4i v3 = s[i * 4 + 3]; + + GSVector4i::sw8(v0, v1, v2, v3); + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw8(v0, v2, v1, v3); + GSVector4i::sw64(v0, v1, v2, v3); + + if((i & 1) == 0) + { + v2 = v2.yxwz(); + v3 = v3.yxwz(); + } + else + { + v0 = v0.yxwz(); + v1 = v1.yxwz(); + } + + GSVector4i::store(&dst[dstpitch * 0], v0); + GSVector4i::store(&dst[dstpitch * 1], v1); + GSVector4i::store(&dst[dstpitch * 2], v2); + GSVector4i::store(&dst[dstpitch * 3], v3); + + #else + + const BYTE* s = &columnTable8[(i & 3) << 2][0]; + + for(int j = 0; j < 4; j++, s += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + dst[i] = src[s[i]]; + } + } + + #endif + } + + template __forceinline static void ReadColumn4(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x301 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0].xzyw(); + GSVector4i v1 = s[i * 4 + 1].xzyw(); + GSVector4i v2 = s[i * 4 + 2].xzyw(); + GSVector4i v3 = s[i * 4 + 3].xzyw(); + + GSVector4i::sw64(v0, v1, v2, v3); + GSVector4i::sw4(v0, v2, v1, v3); + GSVector4i::sw8(v0, v1, v2, v3); + + v0 = v0.shuffle8(m_r4mask); + v1 = v1.shuffle8(m_r4mask); + v2 = v2.shuffle8(m_r4mask); + v3 = v3.shuffle8(m_r4mask); + + if((i & 1) == 0) + { + GSVector4i::sw16rh(v0, v1, v2, v3); + } + else + { + GSVector4i::sw16rl(v0, v1, v2, v3); + } + + GSVector4i::store(&dst[dstpitch * 0], v0); + GSVector4i::store(&dst[dstpitch * 1], v1); + GSVector4i::store(&dst[dstpitch * 2], v2); + GSVector4i::store(&dst[dstpitch * 3], v3); + + #elif _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = s[i * 4 + 0]; + GSVector4i v1 = s[i * 4 + 1]; + GSVector4i v2 = s[i * 4 + 2]; + GSVector4i v3 = s[i * 4 + 3]; + + GSVector4i::sw32(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v2, v3); + GSVector4i::sw4(v0, v2, v1, v3); + GSVector4i::sw8(v0, v1, v2, v3); + GSVector4i::sw16(v0, v2, v1, v3); + + v0 = v0.xzyw(); + v1 = v1.xzyw(); + v2 = v2.xzyw(); + v3 = v3.xzyw(); + + GSVector4i::sw64(v0, v1, v2, v3); + + if((i & 1) == 0) + { + v2 = v2.yxwzlh(); + v3 = v3.yxwzlh(); + } + else + { + v0 = v0.yxwzlh(); + v1 = v1.yxwzlh(); + } + + GSVector4i::store(&dst[dstpitch * 0], v0); + GSVector4i::store(&dst[dstpitch * 1], v1); + GSVector4i::store(&dst[dstpitch * 2], v2); + GSVector4i::store(&dst[dstpitch * 3], v3); + + #else + + const WORD* s = &columnTable4[(i & 3) << 2][0]; + + for(int j = 0; j < 4; j++, s += 32, dst += dstpitch) + { + for(int i = 0; i < 32; i++) + { + DWORD addr = s[i]; + BYTE c = (src[addr >> 1] >> ((addr & 1) << 2)) & 0x0f; + int shift = (i & 1) << 2; + dst[i >> 1] = (dst[i >> 1] & (0xf0 >> shift)) | (c << shift); + } + } + + #endif + } + + template static void ReadColumn32(int y, const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + switch((y >> 1) & 3) + { + case 0: ReadColumn32<0, aligned>(src, dst, dstpitch); break; + case 1: ReadColumn32<1, aligned>(src, dst, dstpitch); break; + case 2: ReadColumn32<2, aligned>(src, dst, dstpitch); break; + case 3: ReadColumn32<3, aligned>(src, dst, dstpitch); break; + default: __assume(0); + } + } + + template static void ReadColumn16(int y, const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + switch((y >> 1) & 3) + { + case 0: ReadColumn16<0, aligned>(src, dst, dstpitch); break; + case 1: ReadColumn16<1, aligned>(src, dst, dstpitch); break; + case 2: ReadColumn16<2, aligned>(src, dst, dstpitch); break; + case 3: ReadColumn16<3, aligned>(src, dst, dstpitch); break; + default: __assume(0); + } + } + + template static void ReadColumn8(int y, const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + switch((y >> 2) & 3) + { + case 0: ReadColumn8<0, aligned>(src, dst, dstpitch); break; + case 1: ReadColumn8<1, aligned>(src, dst, dstpitch); break; + case 2: ReadColumn8<2, aligned>(src, dst, dstpitch); break; + case 3: ReadColumn8<3, aligned>(src, dst, dstpitch); break; + default: __assume(0); + } + } + + template static void ReadColumn4(int y, const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + switch((y >> 2) & 3) + { + case 0: ReadColumn4<0, aligned>(src, dst, dstpitch); break; + case 1: ReadColumn4<1, aligned>(src, dst, dstpitch); break; + case 2: ReadColumn4<2, aligned>(src, dst, dstpitch); break; + case 3: ReadColumn4<3, aligned>(src, dst, dstpitch); break; + default: __assume(0); + } + } + + template static void ReadBlock32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + ReadColumn32<0, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn32<1, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn32<2, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn32<3, aligned>(src, dst, dstpitch); + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[i] = ((DWORD*)src)[s[i]]; + } + } + + #endif + } + + template static void ReadBlock16(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + ReadColumn16<0, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn16<1, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn16<2, aligned>(src, dst, dstpitch); + dst += dstpitch * 2; + ReadColumn16<3, aligned>(src, dst, dstpitch); + + #else + + const BYTE* s = &columnTable16[0][0]; + + for(int j = 0; j < 8; j++, s += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + ((WORD*)dst)[i] = ((WORD*)src)[s[i]]; + } + } + + #endif + } + + template static void ReadBlock8(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + ReadColumn8<0, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn8<1, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn8<2, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn8<3, aligned>(src, dst, dstpitch); + + #else + + const BYTE* s = &columnTable8[0][0]; + + for(int j = 0; j < 16; j++, s += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + dst[i] = src[s[i]]; + } + } + + #endif + } + + template static void ReadBlock4(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + ReadColumn4<0, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn4<1, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn4<2, aligned>(src, dst, dstpitch); + dst += dstpitch * 4; + ReadColumn4<3, aligned>(src, dst, dstpitch); + + #else + + const WORD* s = &columnTable4[0][0]; + + for(int j = 0; j < 16; j++, s += 32, dst += dstpitch) + { + for(int i = 0; i < 32; i++) + { + DWORD addr = s[i]; + BYTE c = (src[addr >> 1] >> ((addr & 1) << 2)) & 0x0f; + int shift = (i & 1) << 2; + dst[i >> 1] = (dst[i >> 1] & (0xf0 >> shift)) | (c << shift); + } + } + + #endif + } + + __forceinline static void ReadBlock4P(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + GSVector4i mask(0x0f0f0f0f); + + for(int i = 0; i < 2; i++) + { + // col 0, 2 + + v0 = s[i * 8 + 0]; + v1 = s[i * 8 + 1]; + v2 = s[i * 8 + 2]; + v3 = s[i * 8 + 3]; + + GSVector4i::sw8(v0, v1, v2, v3); + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw8(v0, v2, v1, v3); + + GSVector4i::store(&dst[dstpitch * 0 + 0], (v0 & mask)); + GSVector4i::store(&dst[dstpitch * 0 + 16], (v1 & mask)); + GSVector4i::store(&dst[dstpitch * 1 + 0], (v2 & mask)); + GSVector4i::store(&dst[dstpitch * 1 + 16], (v3 & mask)); + + dst += dstpitch * 2; + + GSVector4i::store(&dst[dstpitch * 0 + 0], (v0.andnot(mask)).yxwz() >> 4); + GSVector4i::store(&dst[dstpitch * 0 + 16], (v1.andnot(mask)).yxwz() >> 4); + GSVector4i::store(&dst[dstpitch * 1 + 0], (v2.andnot(mask)).yxwz() >> 4); + GSVector4i::store(&dst[dstpitch * 1 + 16], (v3.andnot(mask)).yxwz() >> 4); + + dst += dstpitch * 2; + + // col 1, 3 + + v0 = s[i * 8 + 4]; + v1 = s[i * 8 + 5]; + v2 = s[i * 8 + 6]; + v3 = s[i * 8 + 7]; + + GSVector4i::sw8(v0, v1, v2, v3); + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw8(v0, v2, v1, v3); + + GSVector4i::store(&dst[dstpitch * 0 + 0], (v0 & mask).yxwz()); + GSVector4i::store(&dst[dstpitch * 0 + 16], (v1 & mask).yxwz()); + GSVector4i::store(&dst[dstpitch * 1 + 0], (v2 & mask).yxwz()); + GSVector4i::store(&dst[dstpitch * 1 + 16], (v3 & mask).yxwz()); + + dst += dstpitch * 2; + + GSVector4i::store(&dst[dstpitch * 0 + 0], (v0.andnot(mask)) >> 4); + GSVector4i::store(&dst[dstpitch * 0 + 16], (v1.andnot(mask)) >> 4); + GSVector4i::store(&dst[dstpitch * 1 + 0], (v2.andnot(mask)) >> 4); + GSVector4i::store(&dst[dstpitch * 1 + 16], (v3.andnot(mask)) >> 4); + + dst += dstpitch * 2; + } + + #else + + // TODO + + #endif + } + + __forceinline static void ReadBlock8HP(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + v0 = ((v0 >> 24).ps32(v1 >> 24)).pu16((v2 >> 24).ps32(v3 >> 24)); + + GSVector4i::storel(dst, v0); + + dst += dstpitch; + + GSVector4i::storeh(dst, v0); + + dst += dstpitch; + } + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((BYTE*)dst)[i] = ((DWORD*)src)[s[i]] >> 24; + } + } + + #endif + } + + __forceinline static void ReadBlock4HLP(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + GSVector4i mask(0x0f0f0f0f); + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + v0 = ((v0 >> 24).ps32(v1 >> 24)).pu16((v2 >> 24).ps32(v3 >> 24)) & mask; + + GSVector4i::storel(dst, v0); + + dst += dstpitch; + + GSVector4i::storeh(dst, v0); + + dst += dstpitch; + } + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((BYTE*)dst)[i] = (((DWORD*)src)[s[i]] >> 24) & 0xf; + } + } + + #endif + } + + __forceinline static void ReadBlock4HHP(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + v0 = ((v0 >> 28).ps32(v1 >> 28)).pu16((v2 >> 28).ps32(v3 >> 28)); + + GSVector4i::storel(dst, v0); + + dst += dstpitch; + + GSVector4i::storeh(dst, v0); + + dst += dstpitch; + } + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((BYTE*)dst)[i] = ((DWORD*)src)[s[i]] >> 28; + } + } + + #endif + } + + static void UnpackBlock24(const BYTE* RESTRICT src, int srcpitch, DWORD* RESTRICT dst) + { + #if _M_SSE >= 0x200 + + GSVector4i mask = GSVector4i::x00ffffff(); + + for(int i = 0; i < 4; i++, src += srcpitch * 2) + { + GSVector4i v0 = GSVector4i::load(src); + GSVector4i v1 = GSVector4i::load(src + 16, src + srcpitch); + GSVector4i v2 = GSVector4i::load(src + srcpitch + 8); + + ((GSVector4i*)dst)[i * 4 + 0] = v0.upl32(v0.srl<3>()).upl64(v0.srl<6>().upl32(v0.srl<9>())) & mask; + + v0 = v0.srl<12>(v1); + + ((GSVector4i*)dst)[i * 4 + 1] = v0.upl32(v0.srl<3>()).upl64(v0.srl<6>().upl32(v0.srl<9>())) & mask; + + v0 = v1.srl<8>(v2); + + ((GSVector4i*)dst)[i * 4 + 2] = v0.upl32(v0.srl<3>()).upl64(v0.srl<6>().upl32(v0.srl<9>())) & mask; + + v0 = v2.srl<4>(); + + ((GSVector4i*)dst)[i * 4 + 3] = v0.upl32(v0.srl<3>()).upl64(v0.srl<6>().upl32(v0.srl<9>())) & mask; + } + + #else + + for(int j = 0, diff = srcpitch - 8 * 3; j < 8; j++, src += diff, dst += 8) + { + for(int i = 0; i < 8; i++, src += 3) + { + dst[i] = (src[2] << 16) | (src[1] << 8) | src[0]; + } + } + + #endif + } + + static void UnpackBlock8H(const BYTE* RESTRICT src, int srcpitch, DWORD* RESTRICT dst) + { + #if _M_SSE >= 0x200 + + GSVector4i zero = GSVector4i::zero(); + + for(int i = 0; i < 4; i++, src += srcpitch * 2) + { + GSVector4i v = GSVector4i::load(src, src + srcpitch); + + GSVector4i v0 = zero.upl8(v); + GSVector4i v1 = zero.uph8(v); + + ((GSVector4i*)dst)[i * 4 + 0] = zero.upl16(v0); + ((GSVector4i*)dst)[i * 4 + 1] = zero.uph16(v0); + ((GSVector4i*)dst)[i * 4 + 2] = zero.upl16(v1); + ((GSVector4i*)dst)[i * 4 + 3] = zero.uph16(v1); + } + + #else + + for(int j = 0; j < 8; j++, src += srcpitch, dst += 8) + { + for(int i = 0; i < 8; i++) + { + dst[i] = src[i] << 24; + } + } + + #endif + } + + static void UnpackBlock4HL(const BYTE* RESTRICT src, int srcpitch, DWORD* RESTRICT dst) + { + #if _M_SSE >= 0x200 + + GSVector4i zero = GSVector4i::zero(); + GSVector4i mask(0x0f0f0f0f); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = v & mask; + GSVector4i hi = (v >> 4) & mask; + + GSVector4i v0 = lo.upl8(hi); + GSVector4i v1 = lo.uph8(hi); + + GSVector4i v2 = zero.upl8(v0); + GSVector4i v3 = zero.uph8(v0); + GSVector4i v4 = zero.upl8(v1); + GSVector4i v5 = zero.uph8(v1); + + ((GSVector4i*)dst)[i * 8 + 0] = zero.upl16(v2); + ((GSVector4i*)dst)[i * 8 + 1] = zero.uph16(v2); + ((GSVector4i*)dst)[i * 8 + 2] = zero.upl16(v3); + ((GSVector4i*)dst)[i * 8 + 3] = zero.uph16(v3); + ((GSVector4i*)dst)[i * 8 + 4] = zero.upl16(v4); + ((GSVector4i*)dst)[i * 8 + 5] = zero.uph16(v4); + ((GSVector4i*)dst)[i * 8 + 6] = zero.upl16(v5); + ((GSVector4i*)dst)[i * 8 + 7] = zero.uph16(v5); + } + + #else + + for(int j = 0; j < 8; j++, src += srcpitch, dst += 8) + { + for(int i = 0; i < 4; i++) + { + dst[i * 2 + 0] = (src[i] & 0x0f) << 24; + dst[i * 2 + 1] = (src[i] & 0xf0) << 20; + } + } + + #endif + } + + static void UnpackBlock4HH(const BYTE* RESTRICT src, int srcpitch, DWORD* RESTRICT dst) + { + #if _M_SSE >= 0x200 + + GSVector4i zero = GSVector4i::zero(); + GSVector4i mask(0xf0f0f0f0); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = (v << 4) & mask; + GSVector4i hi = v & mask; + + GSVector4i v0 = lo.upl8(hi); + GSVector4i v1 = lo.uph8(hi); + + GSVector4i v2 = zero.upl8(v0); + GSVector4i v3 = zero.uph8(v0); + GSVector4i v4 = zero.upl8(v1); + GSVector4i v5 = zero.uph8(v1); + + ((GSVector4i*)dst)[i * 8 + 0] = zero.upl16(v2); + ((GSVector4i*)dst)[i * 8 + 1] = zero.uph16(v2); + ((GSVector4i*)dst)[i * 8 + 2] = zero.upl16(v3); + ((GSVector4i*)dst)[i * 8 + 3] = zero.uph16(v3); + ((GSVector4i*)dst)[i * 8 + 4] = zero.upl16(v4); + ((GSVector4i*)dst)[i * 8 + 5] = zero.uph16(v4); + ((GSVector4i*)dst)[i * 8 + 6] = zero.upl16(v5); + ((GSVector4i*)dst)[i * 8 + 7] = zero.uph16(v5); + } + + #else + + for(int j = 0; j < 8; j++, src += srcpitch, dst += 8) + { + for(int i = 0; i < 4; i++) + { + dst[i * 2 + 0] = (src[i] & 0x0f) << 28; + dst[i * 2 + 1] = (src[i] & 0xf0) << 24; + } + } + + #endif + } + + template static void ExpandBlock24(const DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const GIFRegTEXA& TEXA) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i TA0(TEXA.TA0 << 24); + GSVector4i mask = GSVector4i::x00ffffff(); + + for(int i = 0; i < 4; i++, dst += dstpitch * 2) + { + GSVector4i v0 = s[i * 4 + 0] & mask; + GSVector4i v1 = s[i * 4 + 1] & mask; + GSVector4i v2 = s[i * 4 + 2] & mask; + GSVector4i v3 = s[i * 4 + 3] & mask; + + GSVector4i* d0 = (GSVector4i*)&dst[dstpitch * 0]; + GSVector4i* d1 = (GSVector4i*)&dst[dstpitch * 1]; + + if(AEM) + { + d0[0] = v0 | TA0.andnot(v0 == GSVector4i::zero()); // TA0 & (v0 != GSVector4i::zero()) + d0[1] = v1 | TA0.andnot(v1 == GSVector4i::zero()); // TA0 & (v1 != GSVector4i::zero()) + d1[0] = v2 | TA0.andnot(v2 == GSVector4i::zero()); // TA0 & (v2 != GSVector4i::zero()) + d1[1] = v3 | TA0.andnot(v3 == GSVector4i::zero()); // TA0 & (v3 != GSVector4i::zero()) + } + else + { + d0[0] = v0 | TA0; + d0[1] = v1 | TA0; + d1[0] = v2 | TA0; + d1[1] = v3 | TA0; + } + } + + #else + + DWORD TA0 = TEXA.TA0 << 24; + + for(int j = 0; j < 8; j++, src += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + DWORD c = src[i] & 0xffffff; + + if(AEM) + { + ((DWORD*)dst)[i] = c | (c ? TA0 : 0); + } + else + { + ((DWORD*)dst)[i] = c | TA0; + } + } + } + + #endif + } + + static void ExpandBlock16(const WORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const GIFRegTEXA& TEXA) // do not inline, uses too many xmm regs + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i TA0(TEXA.TA0 << 24); + GSVector4i TA1(TEXA.TA1 << 24); + GSVector4i rm = m_rxxx; + GSVector4i gm = m_xgxx; + GSVector4i bm = m_xxbx; + // GSVector4i am = m_xxxa; + GSVector4i l, h; + + if(TEXA.AEM) + { + for(int i = 0; i < 8; i++, dst += dstpitch) + { + GSVector4i v0 = s[i * 2 + 0]; +/* + l = v0.upl16(); + h = v0.uph16(); + + ((GSVector4i*)dst)[0] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA1.blend(TA0, l < am).andnot(l == GSVector4i::zero()); + ((GSVector4i*)dst)[1] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA1.blend(TA0, h < am).andnot(h == GSVector4i::zero()); +*/ + l = v0.upl16(v0); + h = v0.uph16(v0); + + ((GSVector4i*)dst)[0] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA0.blend8(TA1, l.sra16(15)).andnot(l == GSVector4i::zero()); + ((GSVector4i*)dst)[1] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA0.blend8(TA1, h.sra16(15)).andnot(h == GSVector4i::zero()); + + GSVector4i v1 = s[i * 2 + 1]; +/* + l = v1.upl16(); + h = v1.uph16(); + + ((GSVector4i*)dst)[2] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA1.blend(TA0, l < am).andnot(l == GSVector4i::zero()); + ((GSVector4i*)dst)[3] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA1.blend(TA0, h < am).andnot(h == GSVector4i::zero()); +*/ + l = v1.upl16(v1); + h = v1.uph16(v1); + + ((GSVector4i*)dst)[2] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA0.blend8(TA1, l.sra16(15)).andnot(l == GSVector4i::zero()); + ((GSVector4i*)dst)[3] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA0.blend8(TA1, h.sra16(15)).andnot(h == GSVector4i::zero()); + } + } + else + { + for(int i = 0; i < 8; i++, dst += dstpitch) + { + GSVector4i v0 = s[i * 2 + 0]; +/* + l = v0.upl16(); + h = v0.uph16(); + + ((GSVector4i*)dst)[0] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA1.blend(TA0, l < am); + ((GSVector4i*)dst)[1] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA1.blend(TA0, h < am); +*/ + l = v0.upl16(v0); + h = v0.uph16(v0); + + ((GSVector4i*)dst)[0] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA0.blend(TA1, l.sra16(15)); + ((GSVector4i*)dst)[1] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA0.blend(TA1, h.sra16(15)); + + GSVector4i v1 = s[i * 2 + 1]; +/* + l = v1.upl16(); + h = v1.uph16(); + + ((GSVector4i*)dst)[2] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA1.blend(TA0, l < am); + ((GSVector4i*)dst)[3] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA1.blend(TA0, h < am); +*/ + l = v1.upl16(v1); + h = v1.uph16(v1); + + ((GSVector4i*)dst)[2] = ((l & rm) << 3) | ((l & gm) << 6) | ((l & bm) << 9) | TA0.blend(TA1, l.sra16(15)); + ((GSVector4i*)dst)[3] = ((h & rm) << 3) | ((h & gm) << 6) | ((h & bm) << 9) | TA0.blend(TA1, h.sra16(15)); + } + } + + #else + + DWORD TA0 = TEXA.TA0 << 24; + DWORD TA1 = TEXA.TA1 << 24; + + if(TEXA.AEM) + { + for(int j = 0; j < 8; j++, src += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + ((DWORD*)dst)[i] = ((src[i] & 0x8000) ? TA1 : src[i] ? TA0 : 0) | ((src[i] & 0x7c00) << 9) | ((src[i] & 0x03e0) << 6) | ((src[i] & 0x001f) << 3); + } + } + } + else + { + for(int j = 0; j < 8; j++, src += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + ((DWORD*)dst)[i] = ((src[i] & 0x8000) ? TA1 : TA0) | ((src[i] & 0x7c00) << 9) | ((src[i] & 0x03e0) << 6) | ((src[i] & 0x001f) << 3); + } + } + } + + #endif + } + + __forceinline static void ExpandBlock8_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 16; j++, dst += dstpitch) + { + ((const GSVector4i*)src)[j].gather32_8(pal, (GSVector4i*)dst); + } + } + + __forceinline static void ExpandBlock8_16(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 16; j++, dst += dstpitch) + { + ((const GSVector4i*)src)[j].gather16_8(pal, (GSVector4i*)dst); + } + } + + __forceinline static void ExpandBlock4_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const UINT64* RESTRICT pal) + { + for(int j = 0; j < 16; j++, dst += dstpitch) + { + ((const GSVector4i*)src)[j].gather64_8(pal, (GSVector4i*)dst); + } + } + + __forceinline static void ExpandBlock4_16(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const UINT64* RESTRICT pal) + { + for(int j = 0; j < 16; j++, dst += dstpitch) + { + ((const GSVector4i*)src)[j].gather32_8(pal, (GSVector4i*)dst); + } + } + + __forceinline static void ExpandBlock8H_32(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + const GSVector4i* s = (const GSVector4i*)src; + + ((GSVector4i*)dst)[0] = (s[j * 2 + 0] >> 24).gather32_32<>(pal); + ((GSVector4i*)dst)[1] = (s[j * 2 + 1] >> 24).gather32_32<>(pal); + } + } + + __forceinline static void ExpandBlock8H_16(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = (s[j * 2 + 0] >> 24).gather32_32<>(pal); + GSVector4i v1 = (s[j * 2 + 1] >> 24).gather32_32<>(pal); + + ((GSVector4i*)dst)[0] = v0.pu32(v1); + + #else + + for(int i = 0; i < 8; i++) + { + ((WORD*)dst)[i] = (WORD)pal[src[j * 8 + i] >> 24]; + } + + #endif + } + } + + __forceinline static void ExpandBlock4HL_32(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + const GSVector4i* s = (const GSVector4i*)src; + + ((GSVector4i*)dst)[0] = ((s[j * 2 + 0] >> 24) & 0xf).gather32_32<>(pal); + ((GSVector4i*)dst)[1] = ((s[j * 2 + 1] >> 24) & 0xf).gather32_32<>(pal); + } + } + + __forceinline static void ExpandBlock4HL_16(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = ((s[j * 2 + 0] >> 24) & 0xf).gather32_32<>(pal); + GSVector4i v1 = ((s[j * 2 + 1] >> 24) & 0xf).gather32_32<>(pal); + + ((GSVector4i*)dst)[0] = v0.pu32(v1); + + #else + + for(int i = 0; i < 8; i++) + { + ((WORD*)dst)[i] = (WORD)pal[(src[j * 8 + i] >> 24) & 0xf]; + } + + #endif + } + } + + __forceinline static void ExpandBlock4HH_32(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + const GSVector4i* s = (const GSVector4i*)src; + + ((GSVector4i*)dst)[0] = (s[j * 2 + 0] >> 28).gather32_32<>(pal); + ((GSVector4i*)dst)[1] = (s[j * 2 + 1] >> 28).gather32_32<>(pal); + } + } + + __forceinline static void ExpandBlock4HH_16(DWORD* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + for(int j = 0; j < 8; j++, dst += dstpitch) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0 = (s[j * 2 + 0] >> 28).gather32_32<>(pal); + GSVector4i v1 = (s[j * 2 + 1] >> 28).gather32_32<>(pal); + + ((GSVector4i*)dst)[0] = v0.pu32(v1); + + #else + + for(int i = 0; i < 8; i++) + { + ((WORD*)dst)[i] = (WORD)pal[src[j * 8 + i] >> 28]; + } + + #endif + } + } + + __forceinline static void UnpackAndWriteBlock24(const BYTE* RESTRICT src, int srcpitch, BYTE* RESTRICT dst) + { + #if _M_SSE >= 0x200 + + GSVector4i mask(0x00ffffff); + + for(int i = 0; i < 4; i++, src += srcpitch * 2) + { + GSVector4i v4 = GSVector4i::load(src); + GSVector4i v5 = GSVector4i::load(src + 16, src + srcpitch); + GSVector4i v6 = GSVector4i::load(src + srcpitch + 8); + + GSVector4i v0 = v4.upl32(v4.srl<3>()).upl64(v4.srl<6>().upl32(v4.srl<9>())); + + v4 = v4.srl<12>(v5); + + GSVector4i v1 = v4.upl32(v4.srl<3>()).upl64(v4.srl<6>().upl32(v4.srl<9>())); + + v4 = v5.srl<8>(v6); + + GSVector4i v2 = v4.upl32(v4.srl<3>()).upl64(v4.srl<6>().upl32(v4.srl<9>())); + + v4 = v6.srl<4>(); + + GSVector4i v3 = v4.upl32(v4.srl<3>()).upl64(v4.srl<6>().upl32(v4.srl<9>())); + + GSVector4i::sw64(v0, v2, v1, v3); + + #ifdef _M_AMD64 + + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend8(v0, mask); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend8(v1, mask); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend8(v2, mask); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend8(v3, mask); + + #else + + // here blend is faster than blend8 because vc8 has a little problem optimizing register usage for pblendvb (3rd op must be xmm0) + + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend(v0, mask); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend(v1, mask); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend(v2, mask); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend(v3, mask); + + #endif + } + + #else + + const BYTE* d = &columnTable32[0][0]; + + for(int j = 0, diff = srcpitch - 8 * 3; j < 8; j++, src += diff, d += 8) + { + for(int i = 0; i < 8; i++, src += 3) + { + ((DWORD*)dst)[d[i]] = (((DWORD*)dst)[d[i]] & ~0x00ffffff) | (src[2] << 16) | (src[1] << 8) | src[0]; + } + } + + #endif + } + + __forceinline static void UnpackAndWriteBlock8H(const BYTE* RESTRICT src, int srcpitch, BYTE* RESTRICT dst) + { + #if _M_SSE >= 0x301 + + GSVector4i mask(0xff000000); + + GSVector4i mask0 = m_uw8hmask0; + GSVector4i mask1 = m_uw8hmask1; + GSVector4i mask2 = m_uw8hmask2; + GSVector4i mask3 = m_uw8hmask3; + + for(int i = 0; i < 4; i++, src += srcpitch * 2) + { + GSVector4i v4 = GSVector4i::load(src, src + srcpitch); + + GSVector4i v0 = v4.shuffle8(mask0); + GSVector4i v1 = v4.shuffle8(mask1); + GSVector4i v2 = v4.shuffle8(mask2); + GSVector4i v3 = v4.shuffle8(mask3); + + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend8(v0, mask); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend8(v1, mask); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend8(v2, mask); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend8(v3, mask); + } + + #elif _M_SSE >= 0x200 + + GSVector4i mask(0xff000000); + + for(int i = 0; i < 4; i++, src += srcpitch * 2) + { + GSVector4i v4 = GSVector4i::load(src, src + srcpitch); + + GSVector4i v5 = v4.upl8(v4); + GSVector4i v6 = v4.uph8(v4); + + GSVector4i v0 = v5.upl16(v5); + GSVector4i v1 = v5.uph16(v5); + GSVector4i v2 = v6.upl16(v6); + GSVector4i v3 = v6.uph16(v6); + + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 4 + 0] = ((GSVector4i*)dst)[i * 4 + 0].blend8(v0, mask); + ((GSVector4i*)dst)[i * 4 + 1] = ((GSVector4i*)dst)[i * 4 + 1].blend8(v1, mask); + ((GSVector4i*)dst)[i * 4 + 2] = ((GSVector4i*)dst)[i * 4 + 2].blend8(v2, mask); + ((GSVector4i*)dst)[i * 4 + 3] = ((GSVector4i*)dst)[i * 4 + 3].blend8(v3, mask); + } + + #else + + const BYTE* d = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, src += srcpitch, dst += 8) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[d[i]] = (((DWORD*)dst)[d[i]] & ~0xff000000) | (src[i] << 24); + } + } + + #endif + } + + __forceinline static void UnpackAndWriteBlock4HL(const BYTE* RESTRICT src, int srcpitch, BYTE* RESTRICT dst) + { + #if _M_SSE >= 0x301 + + GSVector4i mask(0x0f0f0f0f); + GSVector4i mask0 = m_uw8hmask0; + GSVector4i mask1 = m_uw8hmask1; + GSVector4i mask2 = m_uw8hmask2; + GSVector4i mask3 = m_uw8hmask3; + GSVector4i mask4(0x0f000000); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = v & mask; + GSVector4i hi = (v >> 4) & mask; + + { + GSVector4i v4 = lo.upl8(hi); + + GSVector4i v0 = v4.shuffle8(mask0); + GSVector4i v1 = v4.shuffle8(mask1); + GSVector4i v2 = v4.shuffle8(mask2); + GSVector4i v3 = v4.shuffle8(mask3); + + ((GSVector4i*)dst)[i * 8 + 0] = ((GSVector4i*)dst)[i * 8 + 0].blend(v0, mask4); + ((GSVector4i*)dst)[i * 8 + 1] = ((GSVector4i*)dst)[i * 8 + 1].blend(v1, mask4); + ((GSVector4i*)dst)[i * 8 + 2] = ((GSVector4i*)dst)[i * 8 + 2].blend(v2, mask4); + ((GSVector4i*)dst)[i * 8 + 3] = ((GSVector4i*)dst)[i * 8 + 3].blend(v3, mask4); + } + + { + GSVector4i v4 = lo.uph8(hi); + + GSVector4i v0 = v4.shuffle8(mask0); + GSVector4i v1 = v4.shuffle8(mask1); + GSVector4i v2 = v4.shuffle8(mask2); + GSVector4i v3 = v4.shuffle8(mask3); + + ((GSVector4i*)dst)[i * 8 + 4] = ((GSVector4i*)dst)[i * 8 + 4].blend(v0, mask4); + ((GSVector4i*)dst)[i * 8 + 5] = ((GSVector4i*)dst)[i * 8 + 5].blend(v1, mask4); + ((GSVector4i*)dst)[i * 8 + 6] = ((GSVector4i*)dst)[i * 8 + 6].blend(v2, mask4); + ((GSVector4i*)dst)[i * 8 + 7] = ((GSVector4i*)dst)[i * 8 + 7].blend(v3, mask4); + } + } + + #elif _M_SSE >= 0x200 +/* + __declspec(align(16)) DWORD block[8 * 8]; + + UnpackBlock4HL(src, srcpitch, block); + + WriteBlock32(dst, (BYTE*)block, sizeof(block) / 8); +*/ + GSVector4i mask(0x0f0f0f0f); + GSVector4i mask2(0x0f000000); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = v & mask; + GSVector4i hi = (v >> 4) & mask; + + { + GSVector4i v4 = lo.upl8(hi); + + GSVector4i v5 = v4.upl8(v4); + GSVector4i v6 = v4.uph8(v4); + + GSVector4i v0 = v5.upl16(v5); + GSVector4i v1 = v5.uph16(v5); + GSVector4i v2 = v6.upl16(v6); + GSVector4i v3 = v6.uph16(v6); + + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 8 + 0] = ((GSVector4i*)dst)[i * 8 + 0].blend(v0, mask2); + ((GSVector4i*)dst)[i * 8 + 1] = ((GSVector4i*)dst)[i * 8 + 1].blend(v1, mask2); + ((GSVector4i*)dst)[i * 8 + 2] = ((GSVector4i*)dst)[i * 8 + 2].blend(v2, mask2); + ((GSVector4i*)dst)[i * 8 + 3] = ((GSVector4i*)dst)[i * 8 + 3].blend(v3, mask2); + } + + { + GSVector4i v4 = lo.uph8(hi); + + GSVector4i v5 = v4.upl8(v4); + GSVector4i v6 = v4.uph8(v4); + + GSVector4i v0 = v5.upl16(v5); + GSVector4i v1 = v5.uph16(v5); + GSVector4i v2 = v6.upl16(v6); + GSVector4i v3 = v6.uph16(v6); + + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 8 + 4] = ((GSVector4i*)dst)[i * 8 + 4].blend(v0, mask2); + ((GSVector4i*)dst)[i * 8 + 5] = ((GSVector4i*)dst)[i * 8 + 5].blend(v1, mask2); + ((GSVector4i*)dst)[i * 8 + 6] = ((GSVector4i*)dst)[i * 8 + 6].blend(v2, mask2); + ((GSVector4i*)dst)[i * 8 + 7] = ((GSVector4i*)dst)[i * 8 + 7].blend(v3, mask2); + } + } + + #else + + const BYTE* d = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, d += 8, src += srcpitch) + { + for(int i = 0; i < 4; i++) + { + ((DWORD*)dst)[d[i * 2 + 0]] = (((DWORD*)dst)[d[i * 2 + 0]] & ~0x0f000000) | ((src[i] & 0x0f) << 24); + ((DWORD*)dst)[d[i * 2 + 1]] = (((DWORD*)dst)[d[i * 2 + 1]] & ~0x0f000000) | ((src[i] & 0xf0) << 20); + } + } + + #endif + } + + __forceinline static void UnpackAndWriteBlock4HH(const BYTE* RESTRICT src, int srcpitch, BYTE* RESTRICT dst) + { + #if _M_SSE >= 0x301 + + GSVector4i mask(0xf0f0f0f0); + GSVector4i mask0 = m_uw8hmask0; + GSVector4i mask1 = m_uw8hmask1; + GSVector4i mask2 = m_uw8hmask2; + GSVector4i mask3 = m_uw8hmask3; + GSVector4i mask4(0xf0000000); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = (v << 4) & mask; + GSVector4i hi = v & mask; + + { + GSVector4i v4 = lo.upl8(hi); + + GSVector4i v0 = v4.shuffle8(mask0); + GSVector4i v1 = v4.shuffle8(mask1); + GSVector4i v2 = v4.shuffle8(mask2); + GSVector4i v3 = v4.shuffle8(mask3); + + ((GSVector4i*)dst)[i * 8 + 0] = ((GSVector4i*)dst)[i * 8 + 0].blend(v0, mask4); + ((GSVector4i*)dst)[i * 8 + 1] = ((GSVector4i*)dst)[i * 8 + 1].blend(v1, mask4); + ((GSVector4i*)dst)[i * 8 + 2] = ((GSVector4i*)dst)[i * 8 + 2].blend(v2, mask4); + ((GSVector4i*)dst)[i * 8 + 3] = ((GSVector4i*)dst)[i * 8 + 3].blend(v3, mask4); + } + + { + GSVector4i v4 = lo.uph8(hi); + + GSVector4i v0 = v4.shuffle8(mask0); + GSVector4i v1 = v4.shuffle8(mask1); + GSVector4i v2 = v4.shuffle8(mask2); + GSVector4i v3 = v4.shuffle8(mask3); + + ((GSVector4i*)dst)[i * 8 + 4] = ((GSVector4i*)dst)[i * 8 + 4].blend(v0, mask4); + ((GSVector4i*)dst)[i * 8 + 5] = ((GSVector4i*)dst)[i * 8 + 5].blend(v1, mask4); + ((GSVector4i*)dst)[i * 8 + 6] = ((GSVector4i*)dst)[i * 8 + 6].blend(v2, mask4); + ((GSVector4i*)dst)[i * 8 + 7] = ((GSVector4i*)dst)[i * 8 + 7].blend(v3, mask4); + } + } + + #elif _M_SSE >= 0x200 +/* + __declspec(align(16)) DWORD block[8 * 8]; + + UnpackBlock4HH(src, srcpitch, block); + + WriteBlock32(dst, (BYTE*)block, sizeof(block) / 8); +*/ + GSVector4i mask(0xf0f0f0f0); + GSVector4i mask2(0xf0000000); + + for(int i = 0; i < 2; i++, src += srcpitch * 4) + { + GSVector4i v( + *(DWORD*)&src[srcpitch * 0], + *(DWORD*)&src[srcpitch * 1], + *(DWORD*)&src[srcpitch * 2], + *(DWORD*)&src[srcpitch * 3]); + + GSVector4i lo = (v << 4) & mask; + GSVector4i hi = v & mask; + + { + GSVector4i v4 = lo.upl8(hi); + + GSVector4i v5 = v4.upl8(v4); + GSVector4i v6 = v4.uph8(v4); + + GSVector4i v0 = v5.upl16(v5); + GSVector4i v1 = v5.uph16(v5); + GSVector4i v2 = v6.upl16(v6); + GSVector4i v3 = v6.uph16(v6); + + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 8 + 0] = ((GSVector4i*)dst)[i * 8 + 0].blend(v0, mask2); + ((GSVector4i*)dst)[i * 8 + 1] = ((GSVector4i*)dst)[i * 8 + 1].blend(v1, mask2); + ((GSVector4i*)dst)[i * 8 + 2] = ((GSVector4i*)dst)[i * 8 + 2].blend(v2, mask2); + ((GSVector4i*)dst)[i * 8 + 3] = ((GSVector4i*)dst)[i * 8 + 3].blend(v3, mask2); + } + + { + GSVector4i v4 = lo.uph8(hi); + + GSVector4i v5 = v4.upl8(v4); + GSVector4i v6 = v4.uph8(v4); + + GSVector4i v0 = v5.upl16(v5); + GSVector4i v1 = v5.uph16(v5); + GSVector4i v2 = v6.upl16(v6); + GSVector4i v3 = v6.uph16(v6); + + GSVector4i::sw64(v0, v2, v1, v3); + + ((GSVector4i*)dst)[i * 8 + 4] = ((GSVector4i*)dst)[i * 8 + 4].blend(v0, mask2); + ((GSVector4i*)dst)[i * 8 + 5] = ((GSVector4i*)dst)[i * 8 + 5].blend(v1, mask2); + ((GSVector4i*)dst)[i * 8 + 6] = ((GSVector4i*)dst)[i * 8 + 6].blend(v2, mask2); + ((GSVector4i*)dst)[i * 8 + 7] = ((GSVector4i*)dst)[i * 8 + 7].blend(v3, mask2); + } + } + + #else + + const BYTE* d = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, d += 8, src += srcpitch) + { + for(int i = 0; i < 4; i++) + { + ((DWORD*)dst)[d[i * 2 + 0]] = (((DWORD*)dst)[d[i * 2 + 0]] & ~0xf0000000) | ((src[i] & 0x0f) << 28); + ((DWORD*)dst)[d[i * 2 + 1]] = (((DWORD*)dst)[d[i * 2 + 1]] & ~0xf0000000) | ((src[i] & 0xf0) << 24); + } + } + + #endif + } + + template __forceinline static void ReadAndExpandBlock24(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const GIFRegTEXA& TEXA) + { + #if _M_SSE >= 0x200 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i TA0(TEXA.TA0 << 24); + GSVector4i mask = GSVector4i::x00ffffff(); + + for(int i = 0; i < 4; i++, dst += dstpitch * 2) + { + GSVector4i v0 = s[i * 4 + 0]; + GSVector4i v1 = s[i * 4 + 1]; + GSVector4i v2 = s[i * 4 + 2]; + GSVector4i v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + v0 &= mask; + v1 &= mask; + v2 &= mask; + v3 &= mask; + + GSVector4i* d0 = (GSVector4i*)&dst[dstpitch * 0]; + GSVector4i* d1 = (GSVector4i*)&dst[dstpitch * 1]; + + if(AEM) + { + d0[0] = v0 | TA0.andnot(v0 == GSVector4i::zero()); // TA0 & (v0 != GSVector4i::zero()) + d0[1] = v1 | TA0.andnot(v1 == GSVector4i::zero()); // TA0 & (v1 != GSVector4i::zero()) + d1[0] = v2 | TA0.andnot(v2 == GSVector4i::zero()); // TA0 & (v2 != GSVector4i::zero()) + d1[1] = v3 | TA0.andnot(v3 == GSVector4i::zero()); // TA0 & (v3 != GSVector4i::zero()) + } + else + { + d0[0] = v0 | TA0; + d0[1] = v1 | TA0; + d1[0] = v2 | TA0; + d1[1] = v3 | TA0; + } + } + + #else + + DWORD TA0 = TEXA.TA0 << 24; + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + DWORD c = ((DWORD*)src)[s[i]] & 0xffffff; + + if(AEM) + { + ((DWORD*)dst)[i] = c | (c ? TA0 : 0); + } + else + { + ((DWORD*)dst)[i] = c | TA0; + } + } + } + + #endif + } + + __forceinline static void ReadAndExpandBlock8_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + GSVector4i mask = m_r8mask; + + for(int i = 0; i < 2; i++) + { + v0 = s[i * 8 + 0].shuffle8(mask); + v1 = s[i * 8 + 1].shuffle8(mask); + v2 = s[i * 8 + 2].shuffle8(mask); + v3 = s[i * 8 + 3].shuffle8(mask); + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v3, v2); + + v0.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v3.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v1.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v2.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + + v2 = s[i * 8 + 4].shuffle8(mask); + v3 = s[i * 8 + 5].shuffle8(mask); + v0 = s[i * 8 + 6].shuffle8(mask); + v1 = s[i * 8 + 7].shuffle8(mask); + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v3, v2); + + v0.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v3.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v1.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v2.gather32_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + } + + #elif _M_SSE >= 0x200 + + __declspec(align(16)) BYTE block[16 * 16]; + + ReadBlock8(src, (BYTE*)block, sizeof(block) / 16); + + ExpandBlock8_32(block, dst, dstpitch, pal); + + #else + + const BYTE* s = &columnTable8[0][0]; + + for(int j = 0; j < 16; j++, s += 16, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + ((DWORD*)dst)[i] = pal[src[s[i]]]; + } + } + + #endif + } + + // TODO: ReadAndExpandBlock8_16 + + __forceinline static void ReadAndExpandBlock4_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const UINT64* RESTRICT pal) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + GSVector4i mask = m_r4mask; + + for(int i = 0; i < 2; i++) + { + v0 = s[i * 8 + 0].xzyw(); + v1 = s[i * 8 + 1].xzyw(); + v2 = s[i * 8 + 2].xzyw(); + v3 = s[i * 8 + 3].xzyw(); + + GSVector4i::sw64(v0, v1, v2, v3); + GSVector4i::sw4(v0, v2, v1, v3); + GSVector4i::sw8(v0, v1, v2, v3); + + v0 = v0.shuffle8(mask); + v1 = v1.shuffle8(mask); + v2 = v2.shuffle8(mask); + v3 = v3.shuffle8(mask); + + GSVector4i::sw16rh(v0, v1, v2, v3); + + v0.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v1.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v2.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v3.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + + v0 = s[i * 8 + 4].xzyw(); + v1 = s[i * 8 + 5].xzyw(); + v2 = s[i * 8 + 6].xzyw(); + v3 = s[i * 8 + 7].xzyw(); + + GSVector4i::sw64(v0, v1, v2, v3); + GSVector4i::sw4(v0, v2, v1, v3); + GSVector4i::sw8(v0, v1, v2, v3); + + v0 = v0.shuffle8(mask); + v1 = v1.shuffle8(mask); + v2 = v2.shuffle8(mask); + v3 = v3.shuffle8(mask); + + GSVector4i::sw16rl(v0, v1, v2, v3); + + v0.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v1.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v2.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + v3.gather64_8<>(pal, (GSVector4i*)dst); + dst += dstpitch; + } + + #elif _M_SSE >= 0x200 + + __declspec(align(16)) BYTE block[(32 / 2) * 16]; + + ReadBlock4(src, (BYTE*)block, sizeof(block) / 16); + + ExpandBlock4_32(block, dst, dstpitch, pal); + + #else + + const WORD* s = &columnTable4[0][0]; + + for(int j = 0; j < 16; j++, s += 32, dst += dstpitch) + { + for(int i = 0; i < 16; i++) + { + BYTE a0 = s[i * 2 + 0]; + BYTE a1 = s[i * 2 + 1]; + + BYTE c0 = (src[a0 >> 1] >> ((a0 & 1) << 2)) & 0x0f; + BYTE c1 = (src[a1 >> 1] >> ((a1 & 1) << 2)) & 0x0f; + + ((UINT64*)dst)[i] = pal[(c1 << 4) | c0]; + } + } + + #endif + } + + // TODO: ReadAndExpandBlock4_16 + + __forceinline static void ReadAndExpandBlock8H_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + (v0 >> 24).gather32_32<>(pal, (GSVector4i*)&dst[0]); + (v1 >> 24).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + + (v2 >> 24).gather32_32<>(pal, (GSVector4i*)&dst[0]); + (v3 >> 24).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + } + + #elif _M_SSE >= 0x200 + + __declspec(align(16)) DWORD block[8 * 8]; + + ReadBlock32(src, (BYTE*)block, sizeof(block) / 8); + + ExpandBlock8H_32(block, dst, dstpitch, pal); + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[i] = pal[((DWORD*)src)[s[i]] >> 24]; + } + } + + #endif + } + + // TODO: ReadAndExpandBlock8H_16 + + __forceinline static void ReadAndExpandBlock4HL_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + ((v0 >> 24) & 0xf).gather32_32<>(pal, (GSVector4i*)&dst[0]); + ((v1 >> 24) & 0xf).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + + ((v2 >> 24) & 0xf).gather32_32<>(pal, (GSVector4i*)&dst[0]); + ((v3 >> 24) & 0xf).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + } + + #elif _M_SSE >= 0x200 + + __declspec(align(16)) DWORD block[8 * 8]; + + ReadBlock32(src, (BYTE*)block, sizeof(block) / 8); + + ExpandBlock4HL_32(block, dst, dstpitch, pal); + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[i] = pal[(((DWORD*)src)[s[i]] >> 24) & 0xf]; + } + } + + #endif + } + + // TODO: ReadAndExpandBlock4HL_16 + + __forceinline static void ReadAndExpandBlock4HH_32(const BYTE* RESTRICT src, BYTE* RESTRICT dst, int dstpitch, const DWORD* RESTRICT pal) + { + #if _M_SSE >= 0x401 + + const GSVector4i* s = (const GSVector4i*)src; + + GSVector4i v0, v1, v2, v3; + + for(int i = 0; i < 4; i++) + { + v0 = s[i * 4 + 0]; + v1 = s[i * 4 + 1]; + v2 = s[i * 4 + 2]; + v3 = s[i * 4 + 3]; + + GSVector4i::sw64(v0, v1, v2, v3); + + (v0 >> 28).gather32_32<>(pal, (GSVector4i*)&dst[0]); + (v1 >> 28).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + + (v2 >> 28).gather32_32<>(pal, (GSVector4i*)&dst[0]); + (v3 >> 28).gather32_32<>(pal, (GSVector4i*)&dst[16]); + + dst += dstpitch; + } + + #elif _M_SSE >= 0x200 + + __declspec(align(16)) DWORD block[8 * 8]; + + ReadBlock32(src, (BYTE*)block, sizeof(block) / 8); + + ExpandBlock4HH_32(block, dst, dstpitch, pal); + + #else + + const BYTE* s = &columnTable32[0][0]; + + for(int j = 0; j < 8; j++, s += 8, dst += dstpitch) + { + for(int i = 0; i < 8; i++) + { + ((DWORD*)dst)[i] = pal[((DWORD*)src)[s[i]] >> 28]; + } + } + + #endif + } + + // TODO: ReadAndExpandBlock4HH_16 +}; diff --git a/plugins/GSdx/GSCapture.cpp b/plugins/GSdx/GSCapture.cpp new file mode 100644 index 0000000000..75249b468d --- /dev/null +++ b/plugins/GSdx/GSCapture.cpp @@ -0,0 +1,539 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSCapture.h" +#include "GSVector.h" + +// +// GSSource +// + +#ifdef __INTEL_COMPILER +interface __declspec(uuid("59C193BB-C520-41F3-BC1D-E245B80A86FA")) +#else +[uuid("59C193BB-C520-41F3-BC1D-E245B80A86FA")] interface +#endif +IGSSource : public IUnknown +{ + STDMETHOD(DeliverNewSegment)() PURE; + STDMETHOD(DeliverFrame)(const void* bits, int pitch, bool rgba) PURE; + STDMETHOD(DeliverEOS)() PURE; +}; + +#ifdef __INTEL_COMPILER +class __declspec(uuid("F8BB6F4F-0965-4ED4-BA74-C6A01E6E6C77")) +#else +[uuid("F8BB6F4F-0965-4ED4-BA74-C6A01E6E6C77")] class +#endif +GSSource : public CBaseFilter, private CCritSec, public IGSSource +{ + CSize m_size; + REFERENCE_TIME m_atpf; + REFERENCE_TIME m_now; + + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv) + { + return + QI(IGSSource) + __super::NonDelegatingQueryInterface(riid, ppv); + } + + class GSSourceOutputPin : public CBaseOutputPin + { + CSize m_size; + CAtlArray m_mts; + + public: + GSSourceOutputPin(CSize size, REFERENCE_TIME atpf, CBaseFilter* pFilter, CCritSec* pLock, HRESULT& hr) + : CBaseOutputPin("GSSourceOutputPin", pFilter, pLock, &hr, L"Output") + , m_size(size) + { + CMediaType mt; + mt.majortype = MEDIATYPE_Video; + mt.formattype = FORMAT_VideoInfo; + + VIDEOINFOHEADER vih; + memset(&vih, 0, sizeof(vih)); + vih.AvgTimePerFrame = atpf; + vih.bmiHeader.biSize = sizeof(vih.bmiHeader); + vih.bmiHeader.biWidth = m_size.cx; + vih.bmiHeader.biHeight = m_size.cy; + + #if _M_SSE >= 0x200 + + // YUY2 + + mt.subtype = MEDIASUBTYPE_YUY2; + mt.lSampleSize = m_size.cx * m_size.cy * 2; + + vih.bmiHeader.biCompression = '2YUY'; + vih.bmiHeader.biPlanes = 1; + vih.bmiHeader.biBitCount = 16; + vih.bmiHeader.biSizeImage = m_size.cx * m_size.cy * 2; + mt.SetFormat((BYTE*)&vih, sizeof(vih)); + + m_mts.Add(mt); + + #endif + + // RGB32 + + mt.subtype = MEDIASUBTYPE_RGB32; + mt.lSampleSize = m_size.cx * m_size.cy * 4; + + vih.bmiHeader.biCompression = BI_RGB; + vih.bmiHeader.biPlanes = 1; + vih.bmiHeader.biBitCount = 32; + vih.bmiHeader.biSizeImage = m_size.cx * m_size.cy * 4; + mt.SetFormat((BYTE*)&vih, sizeof(vih)); + + m_mts.Add(mt); + } + + HRESULT GSSourceOutputPin::DecideBufferSize(IMemAllocator* pAlloc, ALLOCATOR_PROPERTIES* pProperties) + { + ASSERT(pAlloc && pProperties); + + HRESULT hr; + + pProperties->cBuffers = 1; + pProperties->cbBuffer = m_mt.lSampleSize; + + ALLOCATOR_PROPERTIES Actual; + + if(FAILED(hr = pAlloc->SetProperties(pProperties, &Actual))) + { + return hr; + } + + if(Actual.cbBuffer < pProperties->cbBuffer) + { + return E_FAIL; + } + + ASSERT(Actual.cBuffers == pProperties->cBuffers); + + return S_OK; + } + + HRESULT CheckMediaType(const CMediaType* pmt) + { + for(int i = 0, j = m_mts.GetCount(); i < j; i++) + { + if(m_mts[i].majortype == pmt->majortype && m_mts[i].subtype == pmt->subtype) + { + return S_OK; + } + } + + return E_FAIL; + } + + HRESULT GetMediaType(int i, CMediaType* pmt) + { + CheckPointer(pmt, E_POINTER); + + if(i < 0) return E_INVALIDARG; + if(i > 1) return VFW_S_NO_MORE_ITEMS; + + *pmt = m_mts[i]; + + return S_OK; + } + + STDMETHODIMP Notify(IBaseFilter* pSender, Quality q) + { + return E_NOTIMPL; + } + + const CMediaType& CurrentMediaType() + { + return m_mt; + } + }; + + CAutoPtr m_output; + +public: + + GSSource(int w, int h, int fps, IUnknown* pUnk, HRESULT& hr) + : CBaseFilter(NAME("GSSource"), pUnk, this, __uuidof(this), &hr) + , m_output(NULL) + , m_size(w, h) + , m_atpf(10000000i64 / fps) + , m_now(0) + { + m_output.Attach(new GSSourceOutputPin(m_size, m_atpf, this, this, hr)); + + // FIXME + if(fps == 60) m_atpf = 166834; // = 10000000i64 / 59.94 + } + + DECLARE_IUNKNOWN; + + int GetPinCount() + { + return 1; + } + + CBasePin* GetPin(int n) + { + return n == 0 ? m_output.m_p : NULL; + } + + // IGSSource + + STDMETHODIMP DeliverNewSegment() + { + m_now = 0; + + return m_output->DeliverNewSegment(0, _I64_MAX, 1.0); + } + + STDMETHODIMP DeliverFrame(const void* bits, int pitch, bool rgba) + { + if(!m_output || !m_output->IsConnected()) + { + return E_UNEXPECTED; + } + + CComPtr sample; + + if(FAILED(m_output->GetDeliveryBuffer(&sample, NULL, NULL, 0))) + { + return E_FAIL; + } + + REFERENCE_TIME start = m_now; + REFERENCE_TIME stop = m_now + m_atpf; + + sample->SetTime(&start, &stop); + sample->SetSyncPoint(TRUE); + + const CMediaType& mt = m_output->CurrentMediaType(); + + BYTE* src = (BYTE*)bits; + + BYTE* dst = NULL; + sample->GetPointer(&dst); + + int w = m_size.cx; + int h = m_size.cy; + int srcpitch = pitch; + + #if _M_SSE >= 0x200 + + if(mt.subtype == MEDIASUBTYPE_YUY2) + { + int dstpitch = ((VIDEOINFOHEADER*)mt.Format())->bmiHeader.biWidth * 2; + + const GSVector4 ys(0.098f, 0.504f, 0.257f, 0.0f); + const GSVector4 us(0.439f / 2, -0.291f / 2, -0.148f / 2, 0.0f); + const GSVector4 vs(-0.071f / 2, -0.368f / 2, 0.439f / 2, 0.0f); + const GSVector4 offset(16, 128, 16, 128); + + if(rgba) + { + for(int j = 0; j < h; j++, dst += dstpitch, src += srcpitch) + { + DWORD* s = (DWORD*)src; + WORD* d = (WORD*)dst; + + for(int i = 0; i < w; i += 2) + { + GSVector4 c0 = GSVector4(s[i + 0]); + GSVector4 c1 = GSVector4(s[i + 1]); + GSVector4 c2 = c0 + c1; + + GSVector4 lo = (c0 * ys).hadd(c2 * vs); + GSVector4 hi = (c1 * ys).hadd(c2 * us); + + GSVector4 c = lo.hadd(hi) + offset; + + *((DWORD*)&d[i]) = GSVector4i(c).rgba32(); + } + } + } + else + { + for(int j = 0; j < h; j++, dst += dstpitch, src += srcpitch) + { + DWORD* s = (DWORD*)src; + WORD* d = (WORD*)dst; + + for(int i = 0; i < w; i += 2) + { + GSVector4 c0 = GSVector4(s[i + 0]).zyxw(); + GSVector4 c1 = GSVector4(s[i + 1]).zyxw(); + GSVector4 c2 = c0 + c1; + + GSVector4 lo = (c0 * ys).hadd(c2 * vs); + GSVector4 hi = (c1 * ys).hadd(c2 * us); + + GSVector4 c = lo.hadd(hi) + offset; + + *((DWORD*)&d[i]) = GSVector4i(c).rgba32(); + } + } + } + } + else + + #endif + + if(mt.subtype == MEDIASUBTYPE_RGB32) + { + int dstpitch = ((VIDEOINFOHEADER*)mt.Format())->bmiHeader.biWidth * 4; + + dst += dstpitch * (h - 1); + dstpitch = -dstpitch; + + for(int j = 0; j < h; j++, dst += dstpitch, src += srcpitch) + { + if(rgba) + { + #if _M_SSE >= 0x301 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i mask(2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15); + + for(int i = 0, w4 = w >> 2; i < w4; i++) + { + d[i] = s[i].shuffle8(mask); + } + + #elif _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + for(int i = 0, w4 = w >> 2; i < w4; i++) + { + d[i] = ((s[i] & 0x00ff0000) >> 16) | ((s[i] & 0x000000ff) << 16) | (s[i] & 0x0000ff00); + } + + #else + + DWORD* s = (DWORD*)src; + DWORD* d = (DWORD*)dst; + + for(int i = 0; i < w; i++) + { + d[i] = ((s[i] & 0x00ff0000) >> 16) | ((s[i] & 0x000000ff) << 16) | (s[i] & 0x0000ff00); + } + + #endif + } + else + { + memcpy(dst, src, w * 4); + } + } + } + else + { + return E_FAIL; + } + + if(FAILED(m_output->Deliver(sample))) + { + return E_FAIL; + } + + m_now = stop; + + return S_OK; + } + + STDMETHODIMP DeliverEOS() + { + return m_output->DeliverEndOfStream(); + } +}; + +// +// GSCapture +// + +GSCapture::GSCapture() + : m_capturing(false) +{ +} + +GSCapture::~GSCapture() +{ + EndCapture(); +} + +#define BeginEnumPins(pBaseFilter, pEnumPins, pPin) \ + {CComPtr pEnumPins; \ + if(pBaseFilter && SUCCEEDED(pBaseFilter->EnumPins(&pEnumPins))) \ + { \ + for(CComPtr pPin; S_OK == pEnumPins->Next(1, &pPin, 0); pPin = NULL) \ + { \ + +#define EndEnumPins }}} + +static IPin* GetFirstPin(IBaseFilter* pBF, PIN_DIRECTION dir) +{ + if(!pBF) return(NULL); + + BeginEnumPins(pBF, pEP, pPin) + { + PIN_DIRECTION dir2; + pPin->QueryDirection(&dir2); + if(dir == dir2) + { + IPin* pRet = pPin.Detach(); + pRet->Release(); + return(pRet); + } + } + EndEnumPins + + return(NULL); +} + +bool GSCapture::BeginCapture(int fps) +{ + CAutoLock cAutoLock(this); + + ASSERT(fps != 0); + + EndCapture(); + + AFX_MANAGE_STATE(AfxGetStaticModuleState()); + + GSCaptureDlg dlg; + + if(IDOK != dlg.DoModal()) return false; + + m_size.cx = (dlg.m_width + 7) & ~7; + m_size.cy = (dlg.m_height + 7) & ~7; + + // + + HRESULT hr; + + CComPtr cgb; + CComPtr mux; + + if(FAILED(hr = m_graph.CoCreateInstance(CLSID_FilterGraph)) + || FAILED(hr = cgb.CoCreateInstance(CLSID_CaptureGraphBuilder2)) + || FAILED(hr = cgb->SetFiltergraph(m_graph)) + || FAILED(hr = cgb->SetOutputFileName(&MEDIASUBTYPE_Avi, CStringW(dlg.m_filename), &mux, NULL))) + { + return false; + } + + m_src = new GSSource(m_size.cx, m_size.cy, fps, NULL, hr); + + if(FAILED(hr = m_graph->AddFilter(m_src, L"Source")) + || FAILED(hr = m_graph->AddFilter(dlg.m_enc, L"Encoder"))) + { + return false; + } + + if(FAILED(hr = m_graph->ConnectDirect(GetFirstPin(m_src, PINDIR_OUTPUT), GetFirstPin(dlg.m_enc, PINDIR_INPUT), NULL)) + || FAILED(hr = m_graph->ConnectDirect(GetFirstPin(dlg.m_enc, PINDIR_OUTPUT), GetFirstPin(mux, PINDIR_INPUT), NULL))) + { + return false; + } + + BeginEnumFilters(m_graph, pEF, pBF) + { + CFilterInfo fi; + pBF->QueryFilterInfo(&fi); + printf("Filter [%p]: %s\n", pBF.p, CStringA(fi.achName)); + + BeginEnumPins(pBF, pEP, pPin) + { + CComPtr pPinTo; + pPin->ConnectedTo(&pPinTo); + + CPinInfo pi; + pPin->QueryPinInfo(&pi); + printf("- Pin [%p - %p]: %s (%s)\n", pPin.p, pPinTo.p, CStringA(pi.achName), pi.dir ? "out" : "in"); + + BeginEnumMediaTypes(pPin, pEMT, pmt) + { + } + EndEnumMediaTypes(pmt) + } + EndEnumPins + } + EndEnumFilters + + hr = CComQIPtr(m_graph)->Run(); + + CComQIPtr(m_src)->DeliverNewSegment(); + + m_capturing = true; + + return true; +} + +bool GSCapture::DeliverFrame(const void* bits, int pitch, bool rgba) +{ + CAutoLock cAutoLock(this); + + if(bits == NULL || pitch == 0) + { + ASSERT(0); + + return false; + } + + if(m_src) + { + CComQIPtr(m_src)->DeliverFrame(bits, pitch, rgba); + + return true; + } + + return false; +} + +bool GSCapture::EndCapture() +{ + CAutoLock cAutoLock(this); + + if(m_src) + { + CComQIPtr(m_src)->DeliverEOS(); + + m_src = NULL; + } + + if(m_graph) + { + CComQIPtr(m_graph)->Stop(); + + m_graph = NULL; + } + + m_capturing = false; + + return true; +} diff --git a/plugins/GSdx/GSCapture.h b/plugins/GSdx/GSCapture.h new file mode 100644 index 0000000000..74a1808160 --- /dev/null +++ b/plugins/GSdx/GSCapture.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSCaptureDlg.h" + +class GSCapture : protected CCritSec +{ + bool m_capturing; + CSize m_size; + CComPtr m_graph; + CComPtr m_src; + +public: + GSCapture(); + virtual ~GSCapture(); + + bool BeginCapture(int fps); + bool DeliverFrame(const void* bits, int pitch, bool rgba); + bool EndCapture(); + + bool IsCapturing() {return m_capturing;} + CSize GetSize() {return m_size;} +}; diff --git a/plugins/GSdx/GSCaptureDlg.cpp b/plugins/GSdx/GSCaptureDlg.cpp new file mode 100644 index 0000000000..e5a4f36ccf --- /dev/null +++ b/plugins/GSdx/GSCaptureDlg.cpp @@ -0,0 +1,243 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include +#include "GSCaptureDlg.h" + +// GSCaptureDlg dialog + +IMPLEMENT_DYNAMIC(GSCaptureDlg, CDialog) +GSCaptureDlg::GSCaptureDlg(CWnd* pParent /*=NULL*/) + : CDialog(GSCaptureDlg::IDD, pParent) +{ + m_width = AfxGetApp()->GetProfileInt(_T("Capture"), _T("Width"), 640); + m_height = AfxGetApp()->GetProfileInt(_T("Capture"), _T("Height"), 480); + m_filename = AfxGetApp()->GetProfileString(_T("Capture"), _T("FileName")); +} + +GSCaptureDlg::~GSCaptureDlg() +{ +} + +int GSCaptureDlg::GetSelCodec(Codec& c) +{ + int iSel = m_codeclist.GetCurSel(); + + if(iSel < 0) return 0; + + POSITION pos = (POSITION)m_codeclist.GetItemDataPtr(iSel); + + if(pos == NULL) return 2; + + c = m_codecs.GetAt(pos); + + if(!c.filter) + { + c.moniker->BindToObject(NULL, NULL, __uuidof(IBaseFilter), (void**)&c.filter); + + if(!c.filter) return 0; + } + + return 1; +} + +LRESULT GSCaptureDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) +{ + LRESULT ret = __super::DefWindowProc(message, wParam, lParam); + + if(message == WM_INITDIALOG) SendMessage(WM_KICKIDLE); + + return(ret); +} + +void GSCaptureDlg::DoDataExchange(CDataExchange* pDX) +{ + __super::DoDataExchange(pDX); + + DDX_Text(pDX, IDC_EDIT1, m_filename); + DDX_Control(pDX, IDC_COMBO1, m_codeclist); + DDX_Text(pDX, IDC_EDIT2, m_width); + DDX_Text(pDX, IDC_EDIT4, m_height); +} + +BOOL GSCaptureDlg::OnInitDialog() +{ + __super::OnInitDialog(); + + m_codecs.RemoveAll(); + + m_codeclist.ResetContent(); + m_codeclist.SetItemDataPtr(m_codeclist.AddString(_T("Uncompressed")), NULL); + + BeginEnumSysDev(CLSID_VideoCompressorCategory, moniker) + { + Codec c; + c.moniker = moniker; + + LPOLESTR strName = NULL; + if(FAILED(moniker->GetDisplayName(NULL, NULL, &strName))) + continue; + + c.DisplayName = strName; + CoTaskMemFree(strName); + + CComPtr pPB; + moniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pPB); + + CComVariant var; + if(FAILED(pPB->Read(CComBSTR(_T("FriendlyName")), &var, NULL))) + continue; + + c.FriendlyName = var.bstrVal; + + CStringW str = CStringW(c.DisplayName).MakeLower(); + CString prefix; + if(str.Find(L"@device:dmo:") == 0) prefix = _T("(DMO) "); + else if(str.Find(L"@device:sw:") == 0) prefix = _T("(DS) "); + else if(str.Find(L"@device:cm:") == 0) prefix = _T("(VfW) "); + c.FriendlyName = prefix + c.FriendlyName; + + m_codeclist.SetItemDataPtr(m_codeclist.AddString(c.FriendlyName), m_codecs.AddTail(c)); + } + EndEnumSysDev + + // + + CString DisplayNameToFind = AfxGetApp()->GetProfileString(_T("Capture"), _T("VideoCodecDisplayName")); + + for(int i = 0; i < m_codeclist.GetCount(); i++) + { + CString DisplayName; + + POSITION pos = (POSITION)m_codeclist.GetItemDataPtr(i); + + if(pos) DisplayName = m_codecs.GetAt(pos).DisplayName; + + if(DisplayName == DisplayNameToFind) + { + m_codeclist.SetCurSel(i); + break; + } + } + + // + + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +BEGIN_MESSAGE_MAP(GSCaptureDlg, CDialog) + ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle) + ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1) + ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2) + ON_UPDATE_COMMAND_UI(IDC_BUTTON2, OnUpdateButton2) + ON_BN_CLICKED(IDOK, OnBnClickedOk) + ON_UPDATE_COMMAND_UI(IDOK, OnUpdateOK) +END_MESSAGE_MAP() + +// GSCaptureDlg message handlers + +void GSCaptureDlg::OnKickIdle() +{ + UpdateDialogControls(this, false); +} + +void GSCaptureDlg::OnBnClickedButton1() +{ + UpdateData(); + + DWORD flags = OFN_EXPLORER|OFN_ENABLESIZING|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST; + + CFileDialog fd(FALSE, _T("avi"), m_filename, flags, _T("Avi files (*.avi)|*.avi||"), this, 0); + + if(fd.DoModal() == IDOK) + { + m_filename = fd.GetPathName(); + + UpdateData(FALSE); + } +} + +void GSCaptureDlg::OnBnClickedButton2() +{ + Codec c; + + if(GetSelCodec(c) != 1) return; + + if(CComQIPtr pSPP = c.filter) + { + CAUUID caGUID; + + memset(&caGUID, 0, sizeof(caGUID)); + + if(SUCCEEDED(pSPP->GetPages(&caGUID))) + { + IUnknown* lpUnk = NULL; + pSPP.QueryInterface(&lpUnk); + OleCreatePropertyFrame(m_hWnd, 0, 0, CStringW(c.FriendlyName), 1, (IUnknown**)&lpUnk, caGUID.cElems, caGUID.pElems, 0, 0, NULL); + lpUnk->Release(); + + if(caGUID.pElems) CoTaskMemFree(caGUID.pElems); + } + } + else if(CComQIPtr pAMVfWCD = c.filter) + { + if(pAMVfWCD->ShowDialog(VfwCompressDialog_QueryConfig, NULL) == S_OK) + { + pAMVfWCD->ShowDialog(VfwCompressDialog_Config, m_hWnd); + } + } +} + +void GSCaptureDlg::OnUpdateButton2(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(m_codeclist.GetCurSel() >= 0 && m_codeclist.GetItemDataPtr(m_codeclist.GetCurSel()) != NULL); +} + +void GSCaptureDlg::OnBnClickedOk() +{ + UpdateData(); + + Codec c; + + if(GetSelCodec(c) == 0) return; + + m_enc = c.filter; + + AfxGetApp()->WriteProfileInt(_T("Capture"), _T("Width"), m_width); + AfxGetApp()->WriteProfileInt(_T("Capture"), _T("Height"), m_height); + AfxGetApp()->WriteProfileString(_T("Capture"), _T("FileName"), m_filename); + AfxGetApp()->WriteProfileString(_T("Capture"), _T("VideoCodecDisplayName"), CString(c.DisplayName)); + + OnOK(); +} + +void GSCaptureDlg::OnUpdateOK(CCmdUI* pCmdUI) +{ + CString str; + + GetDlgItem(IDC_EDIT1)->GetWindowText(str); + + pCmdUI->Enable(!str.IsEmpty() && m_codeclist.GetCurSel() >= 0); +} diff --git a/plugins/GSdx/GSCaptureDlg.h b/plugins/GSdx/GSCaptureDlg.h new file mode 100644 index 0000000000..1ea328ac31 --- /dev/null +++ b/plugins/GSdx/GSCaptureDlg.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "resource.h" +#include "baseclasses/streams.h" + +// GSCaptureDlg dialog + +class GSCaptureDlg : public CDialog +{ + DECLARE_DYNAMIC(GSCaptureDlg) + +private: + struct Codec + { + CComPtr moniker; + CComPtr filter; + CString FriendlyName; + CComBSTR DisplayName; + }; + + CAtlList m_codecs; + + int GetSelCodec(Codec& c); + +public: + GSCaptureDlg(CWnd* pParent = NULL); // standard constructor + virtual ~GSCaptureDlg(); + + CComPtr m_enc; + +// Dialog Data + enum { IDD = IDD_CAPTURE }; + CString m_filename; + CComboBox m_codeclist; + +protected: + virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual BOOL OnInitDialog(); + + DECLARE_MESSAGE_MAP() + +public: + afx_msg void OnKickIdle(); + afx_msg void OnBnClickedButton1(); + afx_msg void OnBnClickedButton2(); + afx_msg void OnUpdateButton2(CCmdUI* pCmdUI); + afx_msg void OnBnClickedOk(); + afx_msg void OnUpdateOK(CCmdUI* pCmdUI); + int m_width; + int m_height; +}; diff --git a/plugins/GSdx/GSClut.cpp b/plugins/GSdx/GSClut.cpp new file mode 100644 index 0000000000..7cbc883e78 --- /dev/null +++ b/plugins/GSdx/GSClut.cpp @@ -0,0 +1,869 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSClut.h" +#include "GSLocalMemory.h" + +GSClut::GSClut(const GSLocalMemory* mem) + : m_mem(mem) +{ + BYTE* p = (BYTE*)VirtualAlloc(NULL, 2 * 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + + m_clut = (WORD*)&p[0]; // 1k + 1k for buffer overruns (sfex: PSM == PSM_PSMT8, CPSM == PSM_PSMCT32, CSA != 0) + m_buff32 = (DWORD*)&p[2048]; // 1k + m_buff64 = (UINT64*)&p[4096]; // 2k + m_write.dirty = true; + m_read.dirty = true; + + for(int i = 0; i < 16; i++) + { + for(int j = 0; j < 64; j++) + { + m_wc[0][i][j] = &GSClut::WriteCLUT_NULL; + m_wc[1][i][j] = &GSClut::WriteCLUT_NULL; + } + } + + m_wc[0][PSM_PSMCT32][PSM_PSMT8] = &GSClut::WriteCLUT32_I8_CSM1; + m_wc[0][PSM_PSMCT32][PSM_PSMT8H] = &GSClut::WriteCLUT32_I8_CSM1; + m_wc[0][PSM_PSMCT32][PSM_PSMT4] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT32][PSM_PSMT4HL] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT32][PSM_PSMT4HH] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT24][PSM_PSMT8] = &GSClut::WriteCLUT32_I8_CSM1; + m_wc[0][PSM_PSMCT24][PSM_PSMT8H] = &GSClut::WriteCLUT32_I8_CSM1; + m_wc[0][PSM_PSMCT24][PSM_PSMT4] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT24][PSM_PSMT4HL] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT24][PSM_PSMT4HH] = &GSClut::WriteCLUT32_I4_CSM1; + m_wc[0][PSM_PSMCT16][PSM_PSMT8] = &GSClut::WriteCLUT16_I8_CSM1; + m_wc[0][PSM_PSMCT16][PSM_PSMT8H] = &GSClut::WriteCLUT16_I8_CSM1; + m_wc[0][PSM_PSMCT16][PSM_PSMT4] = &GSClut::WriteCLUT16_I4_CSM1; + m_wc[0][PSM_PSMCT16][PSM_PSMT4HL] = &GSClut::WriteCLUT16_I4_CSM1; + m_wc[0][PSM_PSMCT16][PSM_PSMT4HH] = &GSClut::WriteCLUT16_I4_CSM1; + m_wc[0][PSM_PSMCT16S][PSM_PSMT8] = &GSClut::WriteCLUT16S_I8_CSM1; + m_wc[0][PSM_PSMCT16S][PSM_PSMT8H] = &GSClut::WriteCLUT16S_I8_CSM1; + m_wc[0][PSM_PSMCT16S][PSM_PSMT4] = &GSClut::WriteCLUT16S_I4_CSM1; + m_wc[0][PSM_PSMCT16S][PSM_PSMT4HL] = &GSClut::WriteCLUT16S_I4_CSM1; + m_wc[0][PSM_PSMCT16S][PSM_PSMT4HH] = &GSClut::WriteCLUT16S_I4_CSM1; + + m_wc[1][PSM_PSMCT32][PSM_PSMT8] = &GSClut::WriteCLUT32_CSM2<256>; + m_wc[1][PSM_PSMCT32][PSM_PSMT8H] = &GSClut::WriteCLUT32_CSM2<256>; + m_wc[1][PSM_PSMCT32][PSM_PSMT4] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT32][PSM_PSMT4HL] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT32][PSM_PSMT4HH] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT24][PSM_PSMT8] = &GSClut::WriteCLUT32_CSM2<256>; + m_wc[1][PSM_PSMCT24][PSM_PSMT8H] = &GSClut::WriteCLUT32_CSM2<256>; + m_wc[1][PSM_PSMCT24][PSM_PSMT4] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT24][PSM_PSMT4HL] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT24][PSM_PSMT4HH] = &GSClut::WriteCLUT32_CSM2<16>; + m_wc[1][PSM_PSMCT16][PSM_PSMT8] = &GSClut::WriteCLUT16_CSM2<256>; + m_wc[1][PSM_PSMCT16][PSM_PSMT8H] = &GSClut::WriteCLUT16_CSM2<256>; + m_wc[1][PSM_PSMCT16][PSM_PSMT4] = &GSClut::WriteCLUT16_CSM2<16>; + m_wc[1][PSM_PSMCT16][PSM_PSMT4HL] = &GSClut::WriteCLUT16_CSM2<16>; + m_wc[1][PSM_PSMCT16][PSM_PSMT4HH] = &GSClut::WriteCLUT16_CSM2<16>; + m_wc[1][PSM_PSMCT16S][PSM_PSMT8] = &GSClut::WriteCLUT16S_CSM2<256>; + m_wc[1][PSM_PSMCT16S][PSM_PSMT8H] = &GSClut::WriteCLUT16S_CSM2<256>; + m_wc[1][PSM_PSMCT16S][PSM_PSMT4] = &GSClut::WriteCLUT16S_CSM2<16>; + m_wc[1][PSM_PSMCT16S][PSM_PSMT4HL] = &GSClut::WriteCLUT16S_CSM2<16>; + m_wc[1][PSM_PSMCT16S][PSM_PSMT4HH] = &GSClut::WriteCLUT16S_CSM2<16>; +} + +GSClut::~GSClut() +{ + VirtualFree(m_clut, 0, MEM_RELEASE); +} + +void GSClut::Invalidate() +{ + m_write.dirty = true; +} + +bool GSClut::WriteTest(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + switch(TEX0.CLD) + { + case 0: return false; + case 1: break; + case 2: m_CBP[0] = TEX0.CBP; break; + case 3: m_CBP[1] = TEX0.CBP; break; + case 4: if(m_CBP[0] == TEX0.CBP) return false; m_CBP[0] = TEX0.CBP; break; + case 5: if(m_CBP[1] == TEX0.CBP) return false; m_CBP[1] = TEX0.CBP; break; + case 6: ASSERT(0); return false; + case 7: ASSERT(0); return false; + default: __assume(0); + } + + return m_write.IsDirty(TEX0, TEXCLUT); +} + +void GSClut::Write(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + m_write.TEX0 = TEX0; + m_write.TEXCLUT = TEXCLUT; + m_write.dirty = false; + m_read.dirty = true; + + (this->*m_wc[TEX0.CSM][TEX0.CPSM][TEX0.PSM])(TEX0, TEXCLUT); +} + +void GSClut::WriteCLUT32_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + ASSERT(TEX0.CSA == 0); + + WriteCLUT_T32_I8_CSM1(&m_mem->m_vm32[m_mem->BlockAddress32(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +void GSClut::WriteCLUT32_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + ASSERT(TEX0.CSA < 16); + + GSVector4i dummy; // this just forces stack alignment and enables inlining the next call + + WriteCLUT_T32_I4_CSM1(&m_mem->m_vm32[m_mem->BlockAddress32(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +void GSClut::WriteCLUT16_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + ASSERT(TEX0.CSA < 16); + + WriteCLUT_T16_I8_CSM1(&m_mem->m_vm16[m_mem->BlockAddress16(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +void GSClut::WriteCLUT16_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + ASSERT(TEX0.CSA < 32); + + WriteCLUT_T16_I4_CSM1(&m_mem->m_vm16[m_mem->BlockAddress16(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +void GSClut::WriteCLUT16S_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + WriteCLUT_T16_I8_CSM1(&m_mem->m_vm16[m_mem->BlockAddress16S(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +void GSClut::WriteCLUT16S_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + WriteCLUT_T16_I4_CSM1(&m_mem->m_vm16[m_mem->BlockAddress16S(0, 0, TEX0.CBP, 1)], m_clut + (TEX0.CSA << 4)); +} + +template void GSClut::WriteCLUT32_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + WORD* RESTRICT clut = m_clut + (TEX0.CSA << 4); + + DWORD base = m_mem->PixelAddress32(0, TEXCLUT.COV, TEX0.CBP, TEXCLUT.CBW); + int* offset = &m_mem->rowOffset32[TEXCLUT.COU << 4]; + + for(int i = 0; i < n; i++) + { + DWORD c = m_mem->ReadPixel32(base + offset[i]); + + clut[i] = (WORD)(c & 0xffff); + clut[i + 256] = (WORD)(c >> 16); + } +} + +template void GSClut::WriteCLUT16_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + WORD* RESTRICT clut = m_clut + (TEX0.CSA << 4); + + DWORD base = m_mem->PixelAddress16(0, TEXCLUT.COV, TEX0.CBP, TEXCLUT.CBW); + int* offset = &m_mem->rowOffset16[TEXCLUT.COU << 4]; + + for(int i = 0; i < n; i++) + { + clut[i] = (WORD)m_mem->ReadPixel16(base + offset[i]); + } +} + +template void GSClut::WriteCLUT16S_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + WORD* RESTRICT clut = m_clut + (TEX0.CSA << 4); + + DWORD base = m_mem->PixelAddress16S(0, TEXCLUT.COV, TEX0.CBP, TEXCLUT.CBW); + int* offset = &m_mem->rowOffset16S[TEXCLUT.COU << 4]; + + for(int i = 0; i < n; i++) + { + clut[i] = (WORD)m_mem->ReadPixel16(base + offset[i]); + } +} + +void GSClut::Read(const GIFRegTEX0& TEX0) +{ + if(m_read.IsDirty(TEX0)) + { + m_read.TEX0 = TEX0; + m_read.dirty = false; + + WORD* clut = m_clut + (TEX0.CSA << 4); + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + switch(TEX0.PSM) + { + case PSM_PSMT8: + case PSM_PSMT8H: + ReadCLUT_T32_I8(clut, m_buff32); + break; + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + ReadCLUT_T32_I4(clut, m_buff32, m_buff64); + break; + } + } + else if(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S) + { + switch(TEX0.PSM) + { + case PSM_PSMT8: + case PSM_PSMT8H: + ReadCLUT_T16_I8(clut, m_buff32); + break; + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + ReadCLUT_T16_I4(clut, m_buff32, m_buff64); + break; + } + } + } +} + +void GSClut::Read32(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) +{ + if(m_read.IsDirty(TEX0, TEXA)) + { + m_read.TEX0 = TEX0; + m_read.TEXA = TEXA; + m_read.dirty = false; + m_read.adirty = true; + + WORD* clut = m_clut + (TEX0.CSA << 4); + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + switch(TEX0.PSM) + { + case PSM_PSMT8: + case PSM_PSMT8H: + ReadCLUT_T32_I8(clut, m_buff32); + break; + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + // TODO: merge these functions + ReadCLUT_T32_I4(clut, m_buff32); + ExpandCLUT64_T32_I8(m_buff32, (UINT64*)m_buff64); // sw renderer does not need m_buff64 anymore + break; + } + } + else if(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S) + { + switch(TEX0.PSM) + { + case PSM_PSMT8: + case PSM_PSMT8H: + Expand16(clut, m_buff32, 256, TEXA); + break; + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + // TODO: merge these functions + Expand16(clut, m_buff32, 16, TEXA); + ExpandCLUT64_T32_I8(m_buff32, (UINT64*)m_buff64); // sw renderer does not need m_buff64 anymore + break; + } + } + } +} + +void GSClut::GetAlphaMinMax32(int& amin, int& amax) +{ + // call only after Read32 + + ASSERT(!m_read.dirty); + + if(m_read.adirty) + { + m_read.adirty = false; + + // DWORD bpp = GSLocalMemory::m_psm[m_read.TEX0.PSM].trbpp; + DWORD cbpp = GSLocalMemory::m_psm[m_read.TEX0.CPSM].trbpp; + DWORD pal = GSLocalMemory::m_psm[m_read.TEX0.PSM].pal; + + if(cbpp == 24 && m_read.TEXA.AEM == 0) + { + m_read.amin = m_read.TEXA.TA0; + m_read.amax = m_read.TEXA.TA0; + } + else + { + int amin = 255; + int amax = 0; + + const GSVector4i* p = (const GSVector4i*)m_buff32; + + for(int i = 0, j = pal >> 4; i < j; i++) + { + GSVector4i v0 = (p[i * 4 + 0] >> 24).ps32(p[i * 4 + 1] >> 24); + GSVector4i v1 = (p[i * 4 + 2] >> 24).ps32(p[i * 4 + 3] >> 24); + + GSVector4i v2 = v0.min_i16(v1); + GSVector4i v3 = v0.max_i16(v1); + + v2 = v2.min_i16(v2.zwxy()); + v3 = v3.max_i16(v3.zwxy()); + v2 = v2.min_i16(v2.zwxyl()); + v3 = v3.max_i16(v3.zwxyl()); + v2 = v2.min_i16(v2.yxwzl()); + v3 = v3.max_i16(v3.yxwzl()); + + amin = min(amin, v2.extract16<0>()); + amax = max(amax, v3.extract16<0>()); + } + + m_read.amin = amin; + m_read.amax = amax; + } + } + + amin = m_read.amin; + amax = m_read.amax; +} + +// + +void GSClut::WriteCLUT_T32_I8_CSM1(const DWORD* RESTRICT src, WORD* RESTRICT clut) +{ + #if _M_SSE >= 0x200 + + for(int i = 0; i < 64; i += 16) + { + WriteCLUT_T32_I4_CSM1(&src[i + 0], &clut[i * 2 + 0]); + WriteCLUT_T32_I4_CSM1(&src[i + 64], &clut[i * 2 + 16]); + WriteCLUT_T32_I4_CSM1(&src[i + 128], &clut[i * 2 + 128]); + WriteCLUT_T32_I4_CSM1(&src[i + 192], &clut[i * 2 + 144]); + } + + #else + + for(int j = 0; j < 2; j++, src += 128, clut += 128) + { + for(int i = 0; i < 128; i++) + { + DWORD c = src[clutTableT32I8[i]]; + clut[i] = (WORD)(c & 0xffff); + clut[i + 256] = (WORD)(c >> 16); + } + } + + #endif +} + +__forceinline void GSClut::WriteCLUT_T32_I4_CSM1(const DWORD* RESTRICT src, WORD* RESTRICT clut) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)clut; + + GSVector4i v0 = s[0]; + GSVector4i v1 = s[1]; + GSVector4i v2 = s[2]; + GSVector4i v3 = s[3]; + + GSVector4i::sw64(v0, v1, v2, v3); + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw16(v0, v2, v1, v3); + GSVector4i::sw16(v0, v1, v2, v3); + + d[0] = v0; + d[1] = v1; + d[32] = v2; + d[33] = v3; + + #else + + for(int i = 0; i < 16; i++) + { + DWORD c = src[clutTableT32I4[i]]; + clut[i] = (WORD)(c & 0xffff); + clut[i + 256] = (WORD)(c >> 16); + } + + #endif +} + +void GSClut::WriteCLUT_T16_I8_CSM1(const WORD* RESTRICT src, WORD* RESTRICT clut) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)clut; + + for(int i = 0; i < 32; i += 4) + { + GSVector4i v0 = s[i + 0]; + GSVector4i v1 = s[i + 1]; + GSVector4i v2 = s[i + 2]; + GSVector4i v3 = s[i + 3]; + + GSVector4i::sw16(v0, v1, v2, v3); + GSVector4i::sw32(v0, v1, v2, v3); + GSVector4i::sw16(v0, v2, v1, v3); + + d[i + 0] = v0; + d[i + 1] = v2; + d[i + 2] = v1; + d[i + 3] = v3; + } + + #else + + for(int j = 0; j < 8; j++, src += 32, clut += 32) + { + for(int i = 0; i < 32; i++) + { + clut[i] = src[clutTableT16I8[i]]; + } + } + + #endif +} + +__forceinline void GSClut::WriteCLUT_T16_I4_CSM1(const WORD* RESTRICT src, WORD* RESTRICT clut) +{ + for(int i = 0; i < 16; i++) + { + clut[i] = src[clutTableT16I4[i]]; + } +} + +void GSClut::ReadCLUT_T32_I8(const WORD* RESTRICT clut, DWORD* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + for(int i = 0; i < 256; i += 16) + { + ReadCLUT_T32_I4(&clut[i], &dst[i]); + } + + #else + + for(int i = 0; i < 256; i++) + { + dst[i] = ((DWORD)clut[i + 256] << 16) | clut[i]; + } + + #endif +} + +__forceinline void GSClut::ReadCLUT_T32_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)clut; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i v0 = s[0]; + GSVector4i v1 = s[1]; + GSVector4i v2 = s[32]; + GSVector4i v3 = s[33]; + + GSVector4i::sw16(v0, v2, v1, v3); + + d[0] = v0; + d[1] = v1; + d[2] = v2; + d[3] = v3; + + #else + + for(int i = 0; i < 16; i++) + { + dst[i] = ((DWORD)clut[i + 256] << 16) | clut[i]; + } + + #endif +} + +__forceinline void GSClut::ReadCLUT_T32_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst32, UINT64* RESTRICT dst64) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)clut; + GSVector4i* d32 = (GSVector4i*)dst32; + GSVector4i* d64 = (GSVector4i*)dst64; + + GSVector4i s0 = s[0]; + GSVector4i s1 = s[1]; + GSVector4i s2 = s[32]; + GSVector4i s3 = s[33]; + + GSVector4i::sw16(s0, s2, s1, s3); + + d32[0] = s0; + d32[1] = s1; + d32[2] = s2; + d32[3] = s3; + + ExpandCLUT64_T32(s0, s0, s1, s2, s3, &d64[0]); + ExpandCLUT64_T32(s1, s0, s1, s2, s3, &d64[32]); + ExpandCLUT64_T32(s2, s0, s1, s2, s3, &d64[64]); + ExpandCLUT64_T32(s3, s0, s1, s2, s3, &d64[96]); + + #else + + for(int i = 0; i < 16; i++) + { + dst[i] = ((DWORD)clut[i + 256] << 16) | clut[i]; + } + + DWORD* d = (DWORD*)dst64; + + for(int j = 0; j < 16; j++, d += 32) + { + DWORD hi = dst32[j]; + + for(int i = 0; i < 16; i++) + { + d[i * 2 + 0] = dst32[i]; + d[i * 2 + 1] = hi; + } + } + + #endif +} + +void GSClut::ReadCLUT_T16_I8(const WORD* RESTRICT clut, DWORD* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + for(int i = 0; i < 256; i += 16) + { + ReadCLUT_T16_I4(&clut[i], &dst[i]); + } + + #else + + for(int i = 0; i < 256; i++) + { + dst[i] = (DWORD)clut[i]; + } + + #endif +} + +__forceinline void GSClut::ReadCLUT_T16_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)clut; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i v0 = s[0]; + GSVector4i v1 = s[1]; + + d[0] = v0.upl16(); + d[1] = v0.uph16(); + d[2] = v1.upl16(); + d[3] = v1.uph16(); + + #else + + for(int i = 0; i < 16; i++) + { + dst[i] = (DWORD)clut[i]; + } + + #endif +} + +__forceinline void GSClut::ReadCLUT_T16_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst32, UINT64* RESTRICT dst64) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)clut; + GSVector4i* d32 = (GSVector4i*)dst32; + GSVector4i* d64 = (GSVector4i*)dst64; + + GSVector4i v0 = s[0]; + GSVector4i v1 = s[1]; + + GSVector4i s0 = v0.upl16(); + GSVector4i s1 = v0.uph16(); + GSVector4i s2 = v1.upl16(); + GSVector4i s3 = v1.uph16(); + + d32[0] = s0; + d32[1] = s1; + d32[2] = s2; + d32[3] = s3; + + ExpandCLUT64_T16(s0, s0, s1, s2, s3, &d64[0]); + ExpandCLUT64_T16(s1, s0, s1, s2, s3, &d64[32]); + ExpandCLUT64_T16(s2, s0, s1, s2, s3, &d64[64]); + ExpandCLUT64_T16(s3, s0, s1, s2, s3, &d64[96]); + + #else + + for(int i = 0; i < 16; i++) + { + dst32[i] = (DWORD)clut[i]; + } + + DWORD* d = (DWORD*)dst64; + + for(int j = 0; j < 16; j++, d += 32) + { + DWORD hi = dst32[j] << 16; + + for(int i = 0; i < 16; i++) + { + d[i * 2 + 0] = hi | (dst32[i] & 0xffff); + } + } + + #endif +} + +void GSClut::ExpandCLUT64_T32_I8(const DWORD* RESTRICT src, UINT64* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i s0 = s[0]; + GSVector4i s1 = s[1]; + GSVector4i s2 = s[2]; + GSVector4i s3 = s[3]; + + ExpandCLUT64_T32(s0, s0, s1, s2, s3, &d[0]); + ExpandCLUT64_T32(s1, s0, s1, s2, s3, &d[32]); + ExpandCLUT64_T32(s2, s0, s1, s2, s3, &d[64]); + ExpandCLUT64_T32(s3, s0, s1, s2, s3, &d[96]); + + #else + + DWORD* d = (DWORD*)dst; + + for(int j = 0; j < 16; j++, d += 32) + { + DWORD hi = src[j]; + + for(int i = 0; i < 16; i++) + { + d[i * 2 + 0] = src[i]; + d[i * 2 + 1] = hi; + } + } + + #endif +} + +__forceinline void GSClut::ExpandCLUT64_T32(const GSVector4i& hi, const GSVector4i& lo0, const GSVector4i& lo1, const GSVector4i& lo2, const GSVector4i& lo3, GSVector4i* dst) +{ + ExpandCLUT64_T32(hi.xxxx(), lo0, &dst[0]); + ExpandCLUT64_T32(hi.xxxx(), lo1, &dst[2]); + ExpandCLUT64_T32(hi.xxxx(), lo2, &dst[4]); + ExpandCLUT64_T32(hi.xxxx(), lo3, &dst[6]); + ExpandCLUT64_T32(hi.yyyy(), lo0, &dst[8]); + ExpandCLUT64_T32(hi.yyyy(), lo1, &dst[10]); + ExpandCLUT64_T32(hi.yyyy(), lo2, &dst[12]); + ExpandCLUT64_T32(hi.yyyy(), lo3, &dst[14]); + ExpandCLUT64_T32(hi.zzzz(), lo0, &dst[16]); + ExpandCLUT64_T32(hi.zzzz(), lo1, &dst[18]); + ExpandCLUT64_T32(hi.zzzz(), lo2, &dst[20]); + ExpandCLUT64_T32(hi.zzzz(), lo3, &dst[22]); + ExpandCLUT64_T32(hi.wwww(), lo0, &dst[24]); + ExpandCLUT64_T32(hi.wwww(), lo1, &dst[26]); + ExpandCLUT64_T32(hi.wwww(), lo2, &dst[28]); + ExpandCLUT64_T32(hi.wwww(), lo3, &dst[30]); +} + +__forceinline void GSClut::ExpandCLUT64_T32(const GSVector4i& hi, const GSVector4i& lo, GSVector4i* dst) +{ + dst[0] = lo.upl32(hi); + dst[1] = lo.uph32(hi); +} + +void GSClut::ExpandCLUT64_T16_I8(const DWORD* RESTRICT src, UINT64* RESTRICT dst) +{ + #if _M_SSE >= 0x200 + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i s0 = s[0]; + GSVector4i s1 = s[1]; + GSVector4i s2 = s[2]; + GSVector4i s3 = s[3]; + + ExpandCLUT64_T16(s0, s0, s1, s2, s3, &d[0]); + ExpandCLUT64_T16(s1, s0, s1, s2, s3, &d[32]); + ExpandCLUT64_T16(s2, s0, s1, s2, s3, &d[64]); + ExpandCLUT64_T16(s3, s0, s1, s2, s3, &d[96]); + + #else + + DWORD* d = (DWORD*)dst; + + for(int j = 0; j < 16; j++, d += 32) + { + DWORD hi = src[j] << 16; + + for(int i = 0; i < 16; i++) + { + d[i * 2 + 0] = hi | (src[i] & 0xffff); + } + } + + #endif +} + +__forceinline void GSClut::ExpandCLUT64_T16(const GSVector4i& hi, const GSVector4i& lo0, const GSVector4i& lo1, const GSVector4i& lo2, const GSVector4i& lo3, GSVector4i* dst) +{ + ExpandCLUT64_T16(hi.xxxx(), lo0, &dst[0]); + ExpandCLUT64_T16(hi.xxxx(), lo1, &dst[2]); + ExpandCLUT64_T16(hi.xxxx(), lo2, &dst[4]); + ExpandCLUT64_T16(hi.xxxx(), lo3, &dst[6]); + ExpandCLUT64_T16(hi.yyyy(), lo0, &dst[8]); + ExpandCLUT64_T16(hi.yyyy(), lo1, &dst[10]); + ExpandCLUT64_T16(hi.yyyy(), lo2, &dst[12]); + ExpandCLUT64_T16(hi.yyyy(), lo3, &dst[14]); + ExpandCLUT64_T16(hi.zzzz(), lo0, &dst[16]); + ExpandCLUT64_T16(hi.zzzz(), lo1, &dst[18]); + ExpandCLUT64_T16(hi.zzzz(), lo2, &dst[20]); + ExpandCLUT64_T16(hi.zzzz(), lo3, &dst[22]); + ExpandCLUT64_T16(hi.wwww(), lo0, &dst[24]); + ExpandCLUT64_T16(hi.wwww(), lo1, &dst[26]); + ExpandCLUT64_T16(hi.wwww(), lo2, &dst[28]); + ExpandCLUT64_T16(hi.wwww(), lo3, &dst[30]); +} + +__forceinline void GSClut::ExpandCLUT64_T16(const GSVector4i& hi, const GSVector4i& lo, GSVector4i* dst) +{ + dst[0] = lo.upl16(hi); + dst[1] = lo.uph16(hi); +} + +// TODO + +static const GSVector4i s_am(0x00008000); +static const GSVector4i s_bm(0x00007c00); +static const GSVector4i s_gm(0x000003e0); +static const GSVector4i s_rm(0x0000001f); + +void GSClut::Expand16(const WORD* RESTRICT src, DWORD* RESTRICT dst, int w, const GIFRegTEXA& TEXA) +{ + #if _M_SSE >= 0x200 + + ASSERT((w & 7) == 0); + + const GSVector4i rm = s_rm; + const GSVector4i gm = s_gm; + const GSVector4i bm = s_bm; + // const GSVector4i am = s_am; + + GSVector4i TA0(TEXA.TA0 << 24); + GSVector4i TA1(TEXA.TA1 << 24); + + GSVector4i c, cl, ch; + + const GSVector4i* s = (const GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + if(!TEXA.AEM) + { + for(int i = 0, j = w >> 3; i < j; i++) + { + c = s[i]; + /* + cl = c.upl16(); + ch = c.uph16(); + d[i * 2 + 0] = ((cl & rm) << 3) | ((cl & gm) << 6) | ((cl & bm) << 9) | TA1.blend(TA0, cl < am); + d[i * 2 + 1] = ((ch & rm) << 3) | ((ch & gm) << 6) | ((ch & bm) << 9) | TA1.blend(TA0, ch < am); + */ + cl = c.upl16(c); + ch = c.uph16(c); + d[i * 2 + 0] = ((cl & rm) << 3) | ((cl & gm) << 6) | ((cl & bm) << 9) | TA0.blend8(TA1, cl.sra16(15)); + d[i * 2 + 1] = ((ch & rm) << 3) | ((ch & gm) << 6) | ((ch & bm) << 9) | TA0.blend8(TA1, ch.sra16(15)); + } + } + else + { + for(int i = 0, j = w >> 3; i < j; i++) + { + c = s[i]; + /* + cl = c.upl16(); + ch = c.uph16(); + d[i * 2 + 0] = ((cl & rm) << 3) | ((cl & gm) << 6) | ((cl & bm) << 9) | TA1.blend(TA0, cl < am).andnot(cl == GSVector4i::zero()); + d[i * 2 + 1] = ((ch & rm) << 3) | ((ch & gm) << 6) | ((ch & bm) << 9) | TA1.blend(TA0, ch < am).andnot(ch == GSVector4i::zero()); + */ + cl = c.upl16(c); + ch = c.uph16(c); + d[i * 2 + 0] = ((cl & rm) << 3) | ((cl & gm) << 6) | ((cl & bm) << 9) | TA0.blend8(TA1, cl.sra16(15)).andnot(cl == GSVector4i::zero()); + d[i * 2 + 1] = ((ch & rm) << 3) | ((ch & gm) << 6) | ((ch & bm) << 9) | TA0.blend8(TA1, ch.sra16(15)).andnot(ch == GSVector4i::zero()); + } + } + + #else + + DWORD TA0 = (DWORD)TEXA.TA0 << 24; + DWORD TA1 = (DWORD)TEXA.TA1 << 24; + + if(!TEXA.AEM) + { + for(int i = 0; i < w; i++) + { + dst[i] = ((src[i] & 0x8000) ? TA1 : TA0) | ((src[i] & 0x7c00) << 9) | ((src[i] & 0x03e0) << 6) | ((src[i] & 0x001f) << 3); + } + } + else + { + for(int i = 0; i < w; i++) + { + dst[i] = ((src[i] & 0x8000) ? TA1 : src[i] ? TA0 : 0) | ((src[i] & 0x7c00) << 9) | ((src[i] & 0x03e0) << 6) | ((src[i] & 0x001f) << 3); + } + } + + #endif +} + +// + +bool GSClut::WriteState::IsDirty(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) +{ + return dirty || !(GSVector4i::load(this) == GSVector4i::load(&TEX0, &TEXCLUT)).alltrue(); +} + +bool GSClut::ReadState::IsDirty(const GIFRegTEX0& TEX0) +{ + return dirty || !(GSVector4i::load(this) == GSVector4i::load(&TEX0, &this->TEXA)).alltrue(); +} + +bool GSClut::ReadState::IsDirty(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) +{ + return dirty || !(GSVector4i::load(this) == GSVector4i::load(&TEX0, &TEXA)).alltrue(); +} diff --git a/plugins/GSdx/GSClut.h b/plugins/GSdx/GSClut.h new file mode 100644 index 0000000000..98f34d8538 --- /dev/null +++ b/plugins/GSdx/GSClut.h @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSVector.h" +#include "GSTables.h" +#include "GSAlignedClass.h" + +class GSLocalMemory; + +__declspec(align(16)) class GSClut : public GSAlignedClass<16> +{ + const GSLocalMemory* m_mem; + + DWORD m_CBP[2]; + WORD* m_clut; + DWORD* m_buff32; + UINT64* m_buff64; + + __declspec(align(16)) struct WriteState + { + GIFRegTEX0 TEX0; + GIFRegTEXCLUT TEXCLUT; + bool dirty; + bool IsDirty(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + } m_write; + + __declspec(align(16)) struct ReadState + { + GIFRegTEX0 TEX0; + GIFRegTEXA TEXA; + bool dirty; + bool adirty; + int amin, amax; + bool IsDirty(const GIFRegTEX0& TEX0); + bool IsDirty(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA); + } m_read; + + typedef void (GSClut::*writeCLUT)(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + + writeCLUT m_wc[2][16][64]; + + void WriteCLUT32_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void WriteCLUT32_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void WriteCLUT16_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void WriteCLUT16_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void WriteCLUT16S_I8_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void WriteCLUT16S_I4_CSM1(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + + template void WriteCLUT32_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + template void WriteCLUT16_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + template void WriteCLUT16S_CSM2(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + + void WriteCLUT_NULL(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT) {} + + static void WriteCLUT_T32_I8_CSM1(const DWORD* RESTRICT src, WORD* RESTRICT clut); + static void WriteCLUT_T32_I4_CSM1(const DWORD* RESTRICT src, WORD* RESTRICT clut); + static void WriteCLUT_T16_I8_CSM1(const WORD* RESTRICT src, WORD* RESTRICT clut); + static void WriteCLUT_T16_I4_CSM1(const WORD* RESTRICT src, WORD* RESTRICT clut); + static void ReadCLUT_T32_I8(const WORD* RESTRICT clut, DWORD* RESTRICT dst); + static void ReadCLUT_T32_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst); + static void ReadCLUT_T32_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst32, UINT64* RESTRICT dst64); + static void ReadCLUT_T16_I8(const WORD* RESTRICT clut, DWORD* RESTRICT dst); + static void ReadCLUT_T16_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst); + static void ReadCLUT_T16_I4(const WORD* RESTRICT clut, DWORD* RESTRICT dst32, UINT64* RESTRICT dst64); + static void ExpandCLUT64_T32_I8(const DWORD* RESTRICT src, UINT64* RESTRICT dst); + static void ExpandCLUT64_T32(const GSVector4i& hi, const GSVector4i& lo0, const GSVector4i& lo1, const GSVector4i& lo2, const GSVector4i& lo3, GSVector4i* dst); + static void ExpandCLUT64_T32(const GSVector4i& hi, const GSVector4i& lo, GSVector4i* dst); + static void ExpandCLUT64_T16_I8(const DWORD* RESTRICT src, UINT64* RESTRICT dst); + static void ExpandCLUT64_T16(const GSVector4i& hi, const GSVector4i& lo0, const GSVector4i& lo1, const GSVector4i& lo2, const GSVector4i& lo3, GSVector4i* dst); + static void ExpandCLUT64_T16(const GSVector4i& hi, const GSVector4i& lo, GSVector4i* dst); + + static void Expand16(const WORD* RESTRICT src, DWORD* RESTRICT dst, int w, const GIFRegTEXA& TEXA); + +public: + GSClut(const GSLocalMemory* mem); + virtual ~GSClut(); + + void Invalidate(); + bool WriteTest(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void Write(const GIFRegTEX0& TEX0, const GIFRegTEXCLUT& TEXCLUT); + void Read(const GIFRegTEX0& TEX0); + void Read32(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA); + void GetAlphaMinMax32(int& amin, int& amax); + + DWORD operator [] (size_t i) const {return m_buff32[i];} + + operator const DWORD*() const {return m_buff32;} + operator const UINT64*() const {return m_buff64;} +}; diff --git a/plugins/GSdx/GSCrc.cpp b/plugins/GSdx/GSCrc.cpp new file mode 100644 index 0000000000..96d37f5a83 --- /dev/null +++ b/plugins/GSdx/GSCrc.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSCrc.h" + +CRC::Game CRC::m_games[] = +{ + {0x00000000, None, Unknown, false}, + {0x2113EA2E, MetalSlug6, Unknown, false}, + {0x42E05BAF, TomoyoAfter, JP, false}, + {0x7800DC84, Clannad, JP, false}, + {0xa39517ab, FFX, EU, true}, + {0xa39517ae, FFX, FR, true}, + {0x941bb7d9, FFX, DE, true}, + {0xa39517a9, FFX, IT, true}, + {0x941bb7de, FFX, ES, true}, + {0xb4414ea1, FFX, RU, true}, + {0xee97db5b, FFX, RU, true}, + {0xaec495cc, FFX, RU, true}, + {0xbb3d833a, FFX, US, true}, + {0x6a4efe60, FFX, JP, true}, + {0x3866ca7e, FFX, ASIA, true}, // int. + {0x658597e2, FFX, JP, true}, // int. + {0x9aac5309, FFX2, EU, true}, + {0x9aac530c, FFX2, FR, true}, + {0x9aac530a, FFX2, FR, true}, // ? + {0x9aac530d, FFX2, DE, true}, + {0x9aac530b, FFX2, IT, true}, + {0x48fe0c71, FFX2, US, true}, + {0xe1fd9a2d, FFX2, JP, true}, // int. + {0x78da0252, FFXII, EU, false}, + {0xc1274668, FFXII, EU, false}, + {0xdc2a467e, FFXII, EU, false}, + {0xca284668, FFXII, EU, false}, + {0x280AD120, FFXII, JP, false}, + {0x8BE3D7B2, ShadowHearts, Unknown, false}, + {0xDEFA4763, ShadowHearts, US, false}, + {0x21068223, Okami, US, false}, + {0x891f223f, Okami, FR, false}, + {0xC5DEFEA0, Okami, JP, false}, + {0x053D2239, MetalGearSolid3, US, false}, + {0x086273D2, MetalGearSolid3, FR, false}, + {0x26A6E286, MetalGearSolid3, EU, false}, + {0xAA31B5BF, MetalGearSolid3, Unknown, false}, + {0x9F185CE1, MetalGearSolid3, Unknown, false}, + {0x98D4BC93, MetalGearSolid3, EU, false}, + {0x86BC3040, MetalGearSolid3, US, false}, //Subsistance disc 1 + {0x0481AD8A, MetalGearSolid3, JP, false}, + {0x278722BF, DBZBT2, US, false}, + {0xFE961D28, DBZBT2, US, false}, + {0x0393B6BE, DBZBT2, EU, false}, + {0xE2F289ED, DBZBT2, JP, false}, // Sparking Neo! + {0x35AA84D1, DBZBT2, Unknown, false}, + {0x428113C2, DBZBT3, US, false}, + {0xA422BB13, DBZBT3, EU, false}, + {0x983c53d2, DBZBT3, Unknown, false}, + {0x72B3802A, SFEX3, US, false}, + {0x71521863, SFEX3, US, false}, + {0x28703748, Bully, US, false}, + {0xC78A495D, BullyCC, US, false}, + {0xC19A374E, SoTC, US, false}, + {0x7D8F539A, SoTC, EU, false}, + {0x3122B508, OnePieceGrandAdventure, US, false}, + {0x6F8545DB, ICO, US, false}, + {0xB01A4C95, ICO, JP, false}, + {0x5C991F4E, ICO, Unknown, false}, + {0xAEAD1CA3, GT4, JP, false}, + {0x44A61C8F, GT4, Unknown, false}, + {0x0086E35B, GT4, Unknown, false}, + {0x77E61C8A, GT4, Unknown, false}, + {0xC164550A, WildArms5, JPUNDUB, false}, + {0xC1640D2C, WildArms5, US, false}, + {0x0FCF8FE4, WildArms5, EU, false}, + {0x2294D322, WildArms5, JP, false}, + {0x565B6170, WildArms5, JP, false}, + {0x8B029334, Manhunt2, Unknown, false}, + {0x09F49E37, CrashBandicootWoC, Unknown, false}, + {0x013E349D, ResidentEvil4, US, false}, + {0x6BA2F6B9, ResidentEvil4, Unknown, false}, + {0x60FA8C69, ResidentEvil4, JP, false}, + {0x72E1E60E, Spartan, Unknown, false}, + {0x5ED8FB53, AceCombat4, JP, false}, + {0x1B9B7563, AceCombat4, Unknown, false}, + {0xEC432B24, Drakengard2, Unknown, false}, + {0xFC46EA61, Tekken5, JP, false}, + {0x1F88EE37, Tekken5, Unknown, false}, + {0x652050D2, Tekken5, Unknown, false}, + {0x9E98B8AE, IkkiTousen, JP, false}, + {0xD6385328, GodOfWar, US, false}, + {0xFB0E6D72, GodOfWar, EU, false}, + {0xA61A4C6D, GodOfWar, Unknown, false}, + {0xE23D532B, GodOfWar, Unknown, false}, + {0x2F123FD8, GodOfWar2, RU, false}, + {0x5D482F18, JackieChanAdv, Unknown, false}, + {0xf0a6d880, HarvestMoon, US, true}, + {0x75c01a04, NamcoXCapcom, US, false}, + {0xBF6F101F, GiTS, US, false}, + {0xA5768F53, GiTS, JP, false}, + {0x6BF11378, Onimusha3, US, false}, + {0xF442260C, MajokkoALaMode2, JP, false}, + {0x14FE77F7, TalesOfAbyss, US, false}, + {0x045D77E9, TalesOfAbyss, US, false}, // undub + {0xAA5EC3A3, TalesOfAbyss, JP, false}, +}; + +CAtlMap CRC::m_map; + +CRC::Game CRC::Lookup(DWORD crc) +{ + if(m_map.IsEmpty()) + { + for(int i = 0; i < countof(m_games); i++) + { + m_map[m_games[i].crc] = &m_games[i]; + } + } + + if(CAtlMap::CPair* pair = m_map.Lookup(crc)) + { + return *pair->m_value; + } + + return m_games[0]; +} diff --git a/plugins/GSdx/GSCrc.h b/plugins/GSdx/GSCrc.h new file mode 100644 index 0000000000..ec637787eb --- /dev/null +++ b/plugins/GSdx/GSCrc.h @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +class CRC +{ +public: + enum Title + { + None, + MetalSlug6, + TomoyoAfter, + Clannad, + FFX, + FFX2, + FFXII, + ShadowHearts, + Okami, + MetalGearSolid3, + DBZBT2, + DBZBT3, + SFEX3, + Bully, + BullyCC, + SoTC, + OnePieceGrandAdventure, + ICO, + GT4, + WildArms5, + Manhunt2, + CrashBandicootWoC, + ResidentEvil4, + Spartan, + AceCombat4, + Drakengard2, + Tekken5, + IkkiTousen, + GodOfWar, + GodOfWar2, + JackieChanAdv, + HarvestMoon, + NamcoXCapcom, + GiTS, + Onimusha3, + MajokkoALaMode2, + TalesOfAbyss, + TitleCount + }; + + enum Region + { + Unknown, + US, + EU, + JP, + JPUNDUB, + RU, + FR, + DE, + IT, + ES, + ASIA + }; + + struct Game + { + DWORD crc; + Title title; + Region region; + bool nloophack; + }; + +private: + static Game m_games[]; + static CAtlMap m_map; + +public: + static Game Lookup(DWORD crc); +}; diff --git a/plugins/GSdx/GSDevice.cpp b/plugins/GSdx/GSDevice.cpp new file mode 100644 index 0000000000..a421ba71f2 --- /dev/null +++ b/plugins/GSdx/GSDevice.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSDevice.h" diff --git a/plugins/GSdx/GSDevice.h b/plugins/GSdx/GSDevice.h new file mode 100644 index 0000000000..cb07eb2af0 --- /dev/null +++ b/plugins/GSdx/GSDevice.h @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTexture.h" +#include "GSVertex.h" + +#pragma pack(push, 1) + +struct MergeConstantBuffer +{ + GSVector4 BGColor; + + struct MergeConstantBuffer() {memset(this, 0, sizeof(*this));} +}; + +struct InterlaceConstantBuffer +{ + GSVector2 ZrH; + float hH; + float _pad[1]; + + struct InterlaceConstantBuffer() {memset(this, 0, sizeof(*this));} +}; + +#pragma pack(pop) + +template class GSDevice +{ + CAtlList m_pool; + +protected: + HWND m_hWnd; + bool m_vsync; + Texture m_backbuffer; + Texture m_merge; + Texture m_weavebob; + Texture m_blend; + Texture m_1x1; + Texture m_current; + + bool Fetch(int type, Texture& t, int w, int h, int format) + { + Recycle(t); + + for(POSITION pos = m_pool.GetHeadPosition(); pos; m_pool.GetNext(pos)) + { + const Texture& t2 = m_pool.GetAt(pos); + + if(t2.GetType() == type && t2.GetWidth() == w && t2.GetHeight() == h && t2.GetFormat() == format) + { + t = t2; + + m_pool.RemoveAt(pos); + + return true; + } + } + + return Create(type, t, w, h, format); + } + + virtual bool Create(int type, Texture& t, int w, int h, int format) = 0; + virtual void DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c) = 0; + virtual void DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset) = 0; + +public: + GSDevice() : m_hWnd(NULL) + { + } + + virtual ~GSDevice() + { + } + + virtual bool Create(HWND hWnd, bool vsync) + { + m_hWnd = hWnd; + m_vsync = vsync; + + return true; + } + + virtual bool Reset(int w, int h, bool fs) + { + m_pool.RemoveAll(); + m_backbuffer = Texture(); + m_merge = Texture(); + m_weavebob = Texture(); + m_blend = Texture(); + m_1x1 = Texture(); + m_current = Texture(); + + return true; + } + + virtual bool IsLost() = 0; + + virtual void Present(const CRect& r) = 0; + + virtual void BeginScene() = 0; + + virtual void EndScene() = 0; + + virtual void Draw(LPCTSTR str) = 0; + + virtual bool CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format = 0) = 0; + + virtual void ClearRenderTarget(Texture& t, const GSVector4& c) = 0; + + virtual void ClearRenderTarget(Texture& t, DWORD c) = 0; + + virtual void ClearDepth(Texture& t, float c) = 0; + + virtual void ClearStencil(Texture& t, BYTE c) = 0; + + virtual bool CreateRenderTarget(Texture& t, int w, int h, int format = 0) + { + return Fetch(GSTexture::RenderTarget, t, w, h, format); + } + + virtual bool CreateDepthStencil(Texture& t, int w, int h, int format = 0) + { + return Fetch(GSTexture::DepthStencil, t, w, h, format); + } + + virtual bool CreateTexture(Texture& t, int w, int h, int format = 0) + { + return Fetch(GSTexture::Texture, t, w, h, format); + } + + virtual bool CreateOffscreen(Texture& t, int w, int h, int format = 0) + { + return Fetch(GSTexture::Offscreen, t, w, h, format); + } + + void Recycle(Texture& t) + { + if(t) + { + m_pool.AddHead(t); + + while(m_pool.GetCount() > 200) + { + m_pool.RemoveTail(); + } + + t = Texture(); + } + } + + bool SaveCurrent(LPCTSTR fn) + { + return m_current.Save(fn); + } + + void GetCurrent(Texture& t) + { + t = m_current; + } + + void Merge(Texture* st, GSVector4* sr, GSVector4* dr, CSize fs, bool slbg, bool mmod, GSVector4& c) + { + if(!m_merge || m_merge.GetWidth() != fs.cx || m_merge.GetHeight() != fs.cy) + { + CreateRenderTarget(m_merge, fs.cx, fs.cy); + } + + // TODO: m_1x1 + + DoMerge(st, sr, dr, m_merge, slbg, mmod, c); + + m_current = m_merge; + } + + bool Interlace(CSize ds, int field, int mode, float yoffset) + { + if(!m_weavebob || m_weavebob.GetWidth() != ds.cx || m_weavebob.GetHeight() != ds.cy) + { + CreateRenderTarget(m_weavebob, ds.cx, ds.cy); + } + + if(mode == 0 || mode == 2) // weave or blend + { + // weave first + + DoInterlace(m_merge, m_weavebob, field, false, 0); + + if(mode == 2) + { + // blend + + if(!m_blend || m_blend.GetWidth() != ds.cx || m_blend.GetHeight() != ds.cy) + { + CreateRenderTarget(m_blend, ds.cx, ds.cy); + } + + DoInterlace(m_weavebob, m_blend, 2, false, 0); + + m_current = m_blend; + } + else + { + m_current = m_weavebob; + } + } + else if(mode == 1) // bob + { + DoInterlace(m_merge, m_weavebob, 3, true, yoffset * field); + + m_current = m_weavebob; + } + else + { + m_current = m_merge; + } + + return true; + } + + virtual bool IsCurrentRGBA() + { + return true; + } +}; diff --git a/plugins/GSdx/GSDevice10.cpp b/plugins/GSdx/GSDevice10.cpp new file mode 100644 index 0000000000..15a3326161 --- /dev/null +++ b/plugins/GSdx/GSDevice10.cpp @@ -0,0 +1,833 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSDevice10.h" +#include "resource.h" + +GSDevice10::GSDevice10() + : m_vb(NULL) + , m_vb_stride(0) + , m_layout(NULL) + , m_topology(D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED) + , m_vs(NULL) + , m_vs_cb(NULL) + , m_gs(NULL) + , m_ps(NULL) + , m_ps_cb(NULL) + , m_scissor(0, 0, 0, 0) + , m_viewport(0, 0) + , m_dss(NULL) + , m_sref(0) + , m_bs(NULL) + , m_bf(-1) + , m_rtv(NULL) + , m_dsv(NULL) +{ + memset(m_ps_srv, 0, sizeof(m_ps_srv)); + memset(m_ps_ss, 0, sizeof(m_ps_ss)); +} + +GSDevice10::~GSDevice10() +{ +} + +bool GSDevice10::Create(HWND hWnd, bool vsync) +{ + if(!__super::Create(hWnd, vsync)) + { + return false; + } + + HRESULT hr; + + DXGI_SWAP_CHAIN_DESC scd; + D3D10_BUFFER_DESC bd; + D3D10_SAMPLER_DESC sd; + D3D10_DEPTH_STENCIL_DESC dsd; + D3D10_RASTERIZER_DESC rd; + D3D10_BLEND_DESC bsd; + + memset(&scd, 0, sizeof(scd)); + + scd.BufferCount = 2; + scd.BufferDesc.Width = 1; + scd.BufferDesc.Height = 1; + scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + //scd.BufferDesc.RefreshRate.Numerator = 60; + //scd.BufferDesc.RefreshRate.Denominator = 1; + scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + scd.OutputWindow = hWnd; + scd.SampleDesc.Count = 1; + scd.SampleDesc.Quality = 0; + scd.Windowed = TRUE; + + UINT flags = 0; + +#ifdef DEBUG + flags |= D3D10_CREATE_DEVICE_DEBUG; +#endif + + hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_SDK_VERSION, &scd, &m_swapchain, &m_dev); + + if(FAILED(hr)) return false; + + // font +/* + // TODO: the driver crashes on alt-enter when using a font... + + D3DX10_FONT_DESC fd; + memset(&fd, 0, sizeof(fd)); + _tcscpy(fd.FaceName, _T("Arial")); + fd.Height = 20; + D3DX10CreateFontIndirect(m_dev, &fd, &m_font); +*/ + // convert + + D3D10_INPUT_ELEMENT_DESC il_convert[] = + { + {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D10_INPUT_PER_VERTEX_DATA, 0}, + }; + + hr = CompileShader(IDR_CONVERT10_FX, "vs_main", NULL, &m_convert.vs, il_convert, countof(il_convert), &m_convert.il); + + for(int i = 0; i < countof(m_convert.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + hr = CompileShader(IDR_CONVERT10_FX, main, NULL, &m_convert.ps[i]); + } + + memset(&bd, 0, sizeof(bd)); + + bd.Usage = D3D10_USAGE_DEFAULT; + bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; + bd.CPUAccessFlags = 0; + bd.MiscFlags = 0; + bd.ByteWidth = 4 * sizeof(GSVertexPT1); + + hr = m_dev->CreateBuffer(&bd, NULL, &m_convert.vb); + + memset(&dsd, 0, sizeof(dsd)); + + dsd.DepthEnable = false; + dsd.StencilEnable = false; + + hr = m_dev->CreateDepthStencilState(&dsd, &m_convert.dss); + + memset(&bsd, 0, sizeof(bsd)); + + bsd.BlendEnable[0] = false; + bsd.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; + + hr = m_dev->CreateBlendState(&bsd, &m_convert.bs); + + // merge + + memset(&bd, 0, sizeof(bd)); + + bd.ByteWidth = sizeof(MergeConstantBuffer); + bd.Usage = D3D10_USAGE_DEFAULT; + bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER; + bd.CPUAccessFlags = 0; + bd.MiscFlags = 0; + + hr = m_dev->CreateBuffer(&bd, NULL, &m_merge.cb); + + for(int i = 0; i < countof(m_merge.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + hr = CompileShader(IDR_MERGE10_FX, main, NULL, &m_merge.ps[i]); + } + + memset(&bsd, 0, sizeof(bsd)); + + bsd.BlendEnable[0] = true; + bsd.BlendOp = D3D10_BLEND_OP_ADD; + bsd.SrcBlend = D3D10_BLEND_SRC_ALPHA; + bsd.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; + bsd.BlendOpAlpha = D3D10_BLEND_OP_ADD; + bsd.SrcBlendAlpha = D3D10_BLEND_ONE; + bsd.DestBlendAlpha = D3D10_BLEND_ZERO; + bsd.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; + + hr = m_dev->CreateBlendState(&bsd, &m_merge.bs); + + // interlace + + memset(&bd, 0, sizeof(bd)); + + bd.ByteWidth = sizeof(InterlaceConstantBuffer); + bd.Usage = D3D10_USAGE_DEFAULT; + bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER; + bd.CPUAccessFlags = 0; + bd.MiscFlags = 0; + + hr = m_dev->CreateBuffer(&bd, NULL, &m_interlace.cb); + + for(int i = 0; i < countof(m_interlace.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + hr = CompileShader(IDR_INTERLACE10_FX, main, NULL, &m_interlace.ps[i]); + } + + // + + memset(&rd, 0, sizeof(rd)); + + rd.FillMode = D3D10_FILL_SOLID; + rd.CullMode = D3D10_CULL_NONE; + rd.FrontCounterClockwise = false; + rd.DepthBias = false; + rd.DepthBiasClamp = 0; + rd.SlopeScaledDepthBias = 0; + rd.DepthClipEnable = false; // ??? + rd.ScissorEnable = true; + rd.MultisampleEnable = false; + rd.AntialiasedLineEnable = false; + + hr = m_dev->CreateRasterizerState(&rd, &m_rs); + + m_dev->RSSetState(m_rs); + + // + + memset(&sd, 0, sizeof(sd)); + + sd.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR; + sd.AddressU = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressV = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.MaxLOD = FLT_MAX; + sd.MaxAnisotropy = 16; + sd.ComparisonFunc = D3D10_COMPARISON_NEVER; + + hr = m_dev->CreateSamplerState(&sd, &m_convert.ln); + + sd.Filter = D3D10_FILTER_MIN_MAG_MIP_POINT; + + hr = m_dev->CreateSamplerState(&sd, &m_convert.pt); + + // + + Reset(1, 1, true); + + // +/* + if(!m_mergefx.Create(this)) + { + return false; + } +*/ + // + + return true; +} + +bool GSDevice10::Reset(int w, int h, bool fs) +{ + if(!__super::Reset(w, h, fs)) + return false; + + DXGI_SWAP_CHAIN_DESC scd; + memset(&scd, 0, sizeof(scd)); + m_swapchain->GetDesc(&scd); + m_swapchain->ResizeBuffers(scd.BufferCount, w, h, scd.BufferDesc.Format, 0); + + CComPtr backbuffer; + m_swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (void**)&backbuffer); + m_backbuffer = Texture(backbuffer); + + return true; +} + +void GSDevice10::Present(const CRect& r) +{ + CRect cr; + + GetClientRect(m_hWnd, &cr); + + if(m_backbuffer.GetWidth() != cr.Width() || m_backbuffer.GetHeight() != cr.Height()) + { + Reset(cr.Width(), cr.Height(), false); + } + + float color[4] = {0, 0, 0, 0}; + + m_dev->ClearRenderTargetView(m_backbuffer, color); + + if(m_current) + { + StretchRect(m_current, m_backbuffer, GSVector4(r)); + } + + m_swapchain->Present(m_vsync ? 1 : 0, 0); +} + +void GSDevice10::BeginScene() +{ +} + +void GSDevice10::EndScene() +{ + PSSetShaderResources(NULL, NULL); + + // not clearing the rt/ds gives a little fps boost in complex games (5-10%) + + // OMSetRenderTargets(NULL, NULL); +} + +void GSDevice10::Draw(LPCTSTR str) +{ + /* + BOOL fs; + CComPtr target; + + m_swapchain->GetFullscreenState(&fs, &target); + + if(fs) + { + BeginScene(); + + OMSetRenderTargets(m_backbuffer, NULL); + + CRect r(0, 0, m_backbuffer.GetWidth(), m_backbuffer.GetHeight()); + + D3DCOLOR c = D3DCOLOR_ARGB(255, 0, 255, 0); + + if(m_font->DrawText(NULL, str, -1, &r, DT_CALCRECT|DT_LEFT|DT_WORDBREAK, c)) + { + m_font->DrawText(NULL, str, -1, &r, DT_LEFT|DT_WORDBREAK, c); + } + + EndScene(); + } + */ +} + +bool GSDevice10::CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format) +{ + dst = Texture(); + + if(format == 0) + { + format = DXGI_FORMAT_R8G8B8A8_UNORM; + } + + if(format != DXGI_FORMAT_R8G8B8A8_UNORM && format != DXGI_FORMAT_R16_UINT) + { + ASSERT(0); + + return false; + } + + Texture rt; + + if(CreateRenderTarget(rt, w, h, format)) + { + GSVector4 dr(0, 0, w, h); + + StretchRect(src, sr, rt, dr, m_convert.ps[format == DXGI_FORMAT_R16_UINT ? 1 : 0], NULL); + + if(CreateOffscreen(dst, w, h, format)) + { + m_dev->CopyResource(dst, rt); + } + } + + Recycle(rt); + + return !!dst; +} + +void GSDevice10::ClearRenderTarget(Texture& t, const GSVector4& c) +{ + m_dev->ClearRenderTargetView(t, c.v); +} + +void GSDevice10::ClearRenderTarget(Texture& t, DWORD c) +{ + GSVector4 color = GSVector4(c) * (1.0f / 255); + + m_dev->ClearRenderTargetView(t, color.v); +} + +void GSDevice10::ClearDepth(Texture& t, float c) +{ + m_dev->ClearDepthStencilView(t, D3D10_CLEAR_DEPTH, c, 0); +} + +void GSDevice10::ClearStencil(Texture& t, BYTE c) +{ + m_dev->ClearDepthStencilView(t, D3D10_CLEAR_STENCIL, 0, c); +} + +bool GSDevice10::Create(int type, Texture& t, int w, int h, int format) +{ + HRESULT hr; + + D3D10_TEXTURE2D_DESC desc; + + memset(&desc, 0, sizeof(desc)); + + desc.Width = w; + desc.Height = h; + desc.Format = (DXGI_FORMAT)format; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.Usage = D3D10_USAGE_DEFAULT; + + switch(type) + { + case GSTexture::RenderTarget: + desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE; + break; + case GSTexture::DepthStencil: + desc.BindFlags = D3D10_BIND_DEPTH_STENCIL; + break; + case GSTexture::Texture: + desc.BindFlags = D3D10_BIND_SHADER_RESOURCE; + break; + case GSTexture::Offscreen: + desc.Usage = D3D10_USAGE_STAGING; + desc.CPUAccessFlags |= D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE; + break; + } + + CComPtr texture; + + hr = m_dev->CreateTexture2D(&desc, NULL, &texture); + + if(SUCCEEDED(hr)) + { + t = Texture(texture); + + switch(type) + { + case GSTexture::RenderTarget: + ClearRenderTarget(t, 0); + break; + case GSTexture::DepthStencil: + ClearDepth(t, 0); + break; + } + + return true; + } + + return false; +} + +bool GSDevice10::CreateRenderTarget(Texture& t, int w, int h, int format) +{ + return __super::CreateRenderTarget(t, w, h, format ? format : DXGI_FORMAT_R8G8B8A8_UNORM); +} + +bool GSDevice10::CreateDepthStencil(Texture& t, int w, int h, int format) +{ + return __super::CreateDepthStencil(t, w, h, format ? format : DXGI_FORMAT_D32_FLOAT_S8X24_UINT); +} + +bool GSDevice10::CreateTexture(Texture& t, int w, int h, int format) +{ + return __super::CreateTexture(t, w, h, format ? format : DXGI_FORMAT_R8G8B8A8_UNORM); +} + +bool GSDevice10::CreateOffscreen(Texture& t, int w, int h, int format) +{ + return __super::CreateOffscreen(t, w, h, format ? format : DXGI_FORMAT_R8G8B8A8_UNORM); +} + +void GSDevice10::DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c) +{ + ClearRenderTarget(dt, c); + + if(st[1] && !slbg) + { + StretchRect(st[1], sr[1], dt, dr[1], m_merge.ps[0], NULL, true); + } + + if(st[0]) + { + m_dev->UpdateSubresource(m_merge.cb, 0, NULL, &c, 0, 0); + + StretchRect(st[0], sr[0], dt, dr[0], m_merge.ps[mmod ? 1 : 0], m_merge.cb, m_merge.bs, true); + } +} + +void GSDevice10::DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset) +{ + GSVector4 sr(0, 0, 1, 1); + GSVector4 dr(0.0f, yoffset, (float)dt.GetWidth(), (float)dt.GetHeight() + yoffset); + + InterlaceConstantBuffer cb; + + cb.ZrH = GSVector2(0, 1.0f / dt.GetHeight()); + cb.hH = (float)dt.GetHeight() / 2; + + m_dev->UpdateSubresource(m_interlace.cb, 0, NULL, &cb, 0, 0); + + StretchRect(st, sr, dt, dr, m_interlace.ps[shader], m_interlace.cb, linear); +} + +void GSDevice10::IASetVertexBuffer(ID3D10Buffer* vb, UINT stride) +{ + if(m_vb != vb || m_vb_stride != stride) + { + UINT offset = 0; + + m_dev->IASetVertexBuffers(0, 1, &vb, &stride, &offset); + + m_vb = vb; + m_vb_stride = stride; + } +} + +void GSDevice10::IASetInputLayout(ID3D10InputLayout* layout) +{ + if(m_layout != layout) + { + m_dev->IASetInputLayout(layout); + + m_layout = layout; + } +} + +void GSDevice10::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY topology) +{ + if(m_topology != topology) + { + m_dev->IASetPrimitiveTopology(topology); + + m_topology = topology; + } +} + +void GSDevice10::VSSetShader(ID3D10VertexShader* vs, ID3D10Buffer* vs_cb) +{ + if(m_vs != vs) + { + m_dev->VSSetShader(vs); + + m_vs = vs; + } + + if(m_vs_cb != vs_cb) + { + m_dev->VSSetConstantBuffers(0, 1, &vs_cb); + + m_vs_cb = vs_cb; + } +} + +void GSDevice10::GSSetShader(ID3D10GeometryShader* gs) +{ + if(m_gs != gs) + { + m_dev->GSSetShader(gs); + + m_gs = gs; + } +} + +void GSDevice10::PSSetShaderResources(ID3D10ShaderResourceView* srv0, ID3D10ShaderResourceView* srv1) +{ + if(m_ps_srv[0] != srv0 || m_ps_srv[1] != srv1) + { + ID3D10ShaderResourceView* srvs[] = {srv0, srv1}; + + m_dev->PSSetShaderResources(0, 2, srvs); + + m_ps_srv[0] = srv0; + m_ps_srv[1] = srv1; + } +} + +void GSDevice10::PSSetShader(ID3D10PixelShader* ps, ID3D10Buffer* ps_cb) +{ + if(m_ps != ps) + { + m_dev->PSSetShader(ps); + + m_ps = ps; + } + + if(m_ps_cb != ps_cb) + { + m_dev->PSSetConstantBuffers(0, 1, &ps_cb); + + m_ps_cb = ps_cb; + } +} + +void GSDevice10::PSSetSamplerState(ID3D10SamplerState* ss0, ID3D10SamplerState* ss1) +{ + if(m_ps_ss[0] != ss0 || m_ps_ss[1] != ss1) + { + ID3D10SamplerState* sss[] = {ss0, ss1}; + + m_dev->PSSetSamplers(0, 2, sss); + + m_ps_ss[0] = ss0; + m_ps_ss[1] = ss1; + } +} + +void GSDevice10::RSSet(int width, int height, const RECT* scissor) +{ + if(m_viewport.cx != width || m_viewport.cy != height) + { + D3D10_VIEWPORT vp; + + memset(&vp, 0, sizeof(vp)); + + vp.TopLeftX = 0; + vp.TopLeftY = 0; + vp.Width = width; + vp.Height = height; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + + m_dev->RSSetViewports(1, &vp); + + m_viewport = CSize(width, height); + } + + CRect r = scissor ? *scissor : CRect(0, 0, width, height); + + if(m_scissor != r) + { + m_dev->RSSetScissorRects(1, &r); + + m_scissor = r; + } +} + +void GSDevice10::OMSetDepthStencilState(ID3D10DepthStencilState* dss, UINT sref) +{ + if(m_dss != dss || m_sref != sref) + { + m_dev->OMSetDepthStencilState(dss, sref); + + m_dss = dss; + m_sref = sref; + } +} + +void GSDevice10::OMSetBlendState(ID3D10BlendState* bs, float bf) +{ + if(m_bs != bs || m_bf != bf) + { + float BlendFactor[] = {bf, bf, bf, 0}; + + m_dev->OMSetBlendState(bs, BlendFactor, 0xffffffff); + + m_bs = bs; + m_bf = bf; + } +} + +void GSDevice10::OMSetRenderTargets(ID3D10RenderTargetView* rtv, ID3D10DepthStencilView* dsv) +{ + if(m_rtv != rtv || m_dsv != dsv) + { + m_dev->OMSetRenderTargets(1, &rtv, dsv); + + m_rtv = rtv; + m_dsv = dsv; + } +} + +void GSDevice10::DrawPrimitive(UINT count, UINT start) +{ + m_dev->Draw(count, start); +} + +void GSDevice10::StretchRect(Texture& st, Texture& dt, const GSVector4& dr, bool linear) +{ + StretchRect(st, GSVector4(0, 0, 1, 1), dt, dr, linear); +} + +void GSDevice10::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, bool linear) +{ + StretchRect(st, sr, dt, dr, m_convert.ps[0], NULL, linear); +} + +void GSDevice10::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, ID3D10PixelShader* ps, ID3D10Buffer* ps_cb, bool linear) +{ + StretchRect(st, sr, dt, dr, ps, ps_cb, m_convert.bs, linear); +} + +void GSDevice10::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, ID3D10PixelShader* ps, ID3D10Buffer* ps_cb, ID3D10BlendState* bs, bool linear) +{ + BeginScene(); + + // om + + OMSetDepthStencilState(m_convert.dss, 0); + OMSetBlendState(bs, 0); + OMSetRenderTargets(dt, NULL); + + // ia + + float left = dr.x * 2 / dt.GetWidth() - 1.0f; + float top = 1.0f - dr.y * 2 / dt.GetHeight(); + float right = dr.z * 2 / dt.GetWidth() - 1.0f; + float bottom = 1.0f - dr.w * 2 / dt.GetHeight(); + + GSVertexPT1 vertices[] = + { + {GSVector4(left, top, 0.5f, 1.0f), GSVector2(sr.x, sr.y)}, + {GSVector4(right, top, 0.5f, 1.0f), GSVector2(sr.z, sr.y)}, + {GSVector4(left, bottom, 0.5f, 1.0f), GSVector2(sr.x, sr.w)}, + {GSVector4(right, bottom, 0.5f, 1.0f), GSVector2(sr.z, sr.w)}, + }; + + D3D10_BOX box = {0, 0, 0, sizeof(vertices), 1, 1}; + + m_dev->UpdateSubresource(m_convert.vb, 0, &box, vertices, 0, 0); + + IASetVertexBuffer(m_convert.vb, sizeof(vertices[0])); + IASetInputLayout(m_convert.il); + IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + + // vs + + VSSetShader(m_convert.vs, NULL); + + // gs + + GSSetShader(NULL); + + // ps + + PSSetShader(ps, ps_cb); + PSSetSamplerState(linear ? m_convert.ln : m_convert.pt, NULL); + PSSetShaderResources(st, NULL); + + // rs + + RSSet(dt.GetWidth(), dt.GetHeight()); + + // + + DrawPrimitive(countof(vertices)); + + // + + EndScene(); +} + +HRESULT GSDevice10::CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10VertexShader** ps, D3D10_INPUT_ELEMENT_DESC* layout, int count, ID3D10InputLayout** il) +{ + HRESULT hr; + + CComPtr shader, error; + + hr = D3DX10CompileFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(id), NULL, macro, NULL, entry, "vs_4_0", 0, 0, NULL, &shader, &error, NULL); + + if(error) + { + TRACE(_T("%s\n"), CString((LPCSTR)error->GetBufferPointer())); + } + + if(FAILED(hr)) + { + return hr; + } + + hr = m_dev->CreateVertexShader((DWORD*)shader->GetBufferPointer(), shader->GetBufferSize(), ps); + + if(FAILED(hr)) + { + return hr; + } + + hr = m_dev->CreateInputLayout(layout, count, shader->GetBufferPointer(), shader->GetBufferSize(), il); + + if(FAILED(hr)) + { + return hr; + } + + return hr; +} + +HRESULT GSDevice10::CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10GeometryShader** gs) +{ + HRESULT hr; + + CComPtr shader, error; + + hr = D3DX10CompileFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(id), NULL, macro, NULL, entry, "gs_4_0", 0, 0, NULL, &shader, &error, NULL); + + if(error) + { + TRACE(_T("%s\n"), CString((LPCSTR)error->GetBufferPointer())); + } + + if(FAILED(hr)) + { + return hr; + } + + hr = m_dev->CreateGeometryShader((DWORD*)shader->GetBufferPointer(), shader->GetBufferSize(), gs); + + if(FAILED(hr)) + { + return hr; + } + + return hr; +} + +HRESULT GSDevice10::CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10PixelShader** ps) +{ + HRESULT hr; + + CComPtr shader, error; + + hr = D3DX10CompileFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(id), NULL, macro, NULL, entry, "ps_4_0", 0, 0, NULL, &shader, &error, NULL); + + if(error) + { + TRACE(_T("%s\n"), CString((LPCSTR)error->GetBufferPointer())); + } + + if(FAILED(hr)) + { + return hr; + } + + hr = m_dev->CreatePixelShader((DWORD*)shader->GetBufferPointer(), shader->GetBufferSize(), ps); + + if(FAILED(hr)) + { + return hr; + } + + return hr; +} diff --git a/plugins/GSdx/GSDevice10.h b/plugins/GSdx/GSDevice10.h new file mode 100644 index 0000000000..b45e82f499 --- /dev/null +++ b/plugins/GSdx/GSDevice10.h @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice.h" +#include "GSTexture10.h" + +class GSDevice10 : public GSDevice +{ +public: + typedef GSTexture10 Texture; + +private: + // state cache + + ID3D10Buffer* m_vb; + UINT m_vb_count; + UINT m_vb_stride; + ID3D10InputLayout* m_layout; + D3D10_PRIMITIVE_TOPOLOGY m_topology; + ID3D10VertexShader* m_vs; + ID3D10Buffer* m_vs_cb; + ID3D10GeometryShader* m_gs; + ID3D10ShaderResourceView* m_ps_srv[2]; + ID3D10PixelShader* m_ps; + ID3D10Buffer* m_ps_cb; + ID3D10SamplerState* m_ps_ss[2]; + CSize m_viewport; + CRect m_scissor; + ID3D10DepthStencilState* m_dss; + UINT m_sref; + ID3D10BlendState* m_bs; + float m_bf; + ID3D10RenderTargetView* m_rtv; + ID3D10DepthStencilView* m_dsv; + + // + + bool Create(int type, Texture& t, int w, int h, int format); + void DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c); + void DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset = 0); + + // + + CComPtr m_dev; + CComPtr m_swapchain; + CComPtr m_font; + +public: // TODO + CComPtr m_rs; + + struct + { + CComPtr vb; + CComPtr il; + CComPtr vs; + CComPtr ps[5]; + CComPtr ln; + CComPtr pt; + CComPtr dss; + CComPtr bs; + } m_convert; + + struct + { + CComPtr ps[2]; + CComPtr cb; + CComPtr bs; + } m_merge; + + struct + { + CComPtr ps[4]; + CComPtr cb; + } m_interlace; + +public: + GSDevice10(); + virtual ~GSDevice10(); + + bool Create(HWND hWnd, bool vsync); + bool Reset(int w, int h, bool fs); + bool IsLost() {return false;} + void Present(const CRect& r); + void BeginScene(); + void EndScene(); + void Draw(LPCTSTR str); + bool CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format = 0); + + void ClearRenderTarget(Texture& t, const GSVector4& c); + void ClearRenderTarget(Texture& t, DWORD c); + void ClearDepth(Texture& t, float c); + void ClearStencil(Texture& t, BYTE c); + + bool CreateRenderTarget(Texture& t, int w, int h, int format = 0); + bool CreateDepthStencil(Texture& t, int w, int h, int format = 0); + bool CreateTexture(Texture& t, int w, int h, int format = 0); + bool CreateOffscreen(Texture& t, int w, int h, int format = 0); + + ID3D10Device* operator->() {return m_dev;} + operator ID3D10Device*() {return m_dev;} + + void IASetVertexBuffer(ID3D10Buffer* vb, UINT stride); + void IASetInputLayout(ID3D10InputLayout* layout); + void IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY topology); + void VSSetShader(ID3D10VertexShader* vs, ID3D10Buffer* vs_cb); + void GSSetShader(ID3D10GeometryShader* gs); + void PSSetShaderResources(ID3D10ShaderResourceView* srv0, ID3D10ShaderResourceView* srv1); + void PSSetShader(ID3D10PixelShader* ps, ID3D10Buffer* ps_cb); + void PSSetSamplerState(ID3D10SamplerState* ss0, ID3D10SamplerState* ss1); + void RSSet(int width, int height, const RECT* scissor = NULL); + void OMSetDepthStencilState(ID3D10DepthStencilState* dss, UINT sref); + void OMSetBlendState(ID3D10BlendState* bs, float bf); + void OMSetRenderTargets(ID3D10RenderTargetView* rtv, ID3D10DepthStencilView* dsv); + void DrawPrimitive(UINT count, UINT start = 0); + + void StretchRect(Texture& st, Texture& dt, const GSVector4& dr, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, ID3D10PixelShader* ps, ID3D10Buffer* ps_cb, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, ID3D10PixelShader* ps, ID3D10Buffer* ps_cb, ID3D10BlendState* bs, bool linear = true); + + HRESULT CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10VertexShader** vs, D3D10_INPUT_ELEMENT_DESC* layout, int count, ID3D10InputLayout** il); + HRESULT CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10GeometryShader** gs); + HRESULT CompileShader(UINT id, LPCSTR entry, D3D10_SHADER_MACRO* macro, ID3D10PixelShader** ps); + + // TODO + bool SaveToFileD32S8X24(ID3D10Texture2D* ds, LPCTSTR fn); +}; diff --git a/plugins/GSdx/GSDevice7.cpp b/plugins/GSdx/GSDevice7.cpp new file mode 100644 index 0000000000..b60cbba059 --- /dev/null +++ b/plugins/GSdx/GSDevice7.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +// dumb device implementation, only good for simple image output + +#include "stdafx.h" +#include "GSDevice7.h" + +GSDevice7::GSDevice7() +{ +} + +GSDevice7::~GSDevice7() +{ +} + +bool GSDevice7::Create(HWND hWnd, bool vsync) +{ + if(!__super::Create(hWnd, vsync)) + { + return false; + } + + if(FAILED(DirectDrawCreateEx(NULL, (VOID**)&m_dd, IID_IDirectDraw7, NULL))) + { + return false; + } + + // TODO: fullscreen + + if(FAILED(m_dd->SetCooperativeLevel(hWnd, DDSCL_NORMAL))) + { + return false; + } + + DDSURFACEDESC2 desc; + + memset(&desc, 0, sizeof(desc)); + + desc.dwSize = sizeof(desc); + desc.dwFlags = DDSD_CAPS; + desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + + if(FAILED(m_dd->CreateSurface(&desc, &m_primary, NULL))) + { + return false; + } + + CComPtr clipper; + + if(FAILED(m_dd->CreateClipper(0, &clipper, NULL)) + || FAILED(clipper->SetHWnd(0, hWnd)) + || FAILED(m_primary->SetClipper(clipper))) + { + return false; + } + + Reset(1, 1, true); + + return true; +} + +bool GSDevice7::Reset(int w, int h, bool fs) +{ + if(!__super::Reset(w, h, fs)) + return false; + + m_backbuffer = NULL; + + DDSURFACEDESC2 desc; + + memset(&desc, 0, sizeof(desc)); + + desc.dwSize = sizeof(desc); + desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; + desc.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY | DDSCAPS_3DDEVICE; + desc.dwWidth = w; + desc.dwHeight = h; + + if(FAILED(m_dd->CreateSurface(&desc, &m_backbuffer, NULL))) + { + return false; + } + + CComPtr clipper; + + if(FAILED(m_dd->CreateClipper(0, &clipper, NULL))) + { + return false; + } + + { + // ??? + + HRGN hrgn = CreateRectRgn(0, 0, w, h); + + BYTE buff[1024]; + + GetRegionData(hrgn, sizeof(buff), (RGNDATA*)buff); + + DeleteObject(hrgn); + + clipper->SetClipList((RGNDATA*)buff, 0); + + if(FAILED(m_backbuffer->SetClipper(clipper))) + { + return false; + } + } + + return true; +} + +void GSDevice7::Present(const CRect& r) +{ + HRESULT hr; + + CRect cr; + GetClientRect(m_hWnd, &cr); + + DDSURFACEDESC2 desc; + memset(&desc, 0, sizeof(desc)); + desc.dwSize = sizeof(desc); + + hr = m_backbuffer->GetSurfaceDesc(&desc); + + if(desc.dwWidth != cr.Width() || desc.dwHeight != cr.Height()) + { + Reset(cr.Width(), cr.Height(), false); + } + + DDBLTFX fx; + + memset(&fx, 0, sizeof(fx)); + + fx.dwSize = sizeof(fx); + fx.dwFillColor = 0; + + hr = m_backbuffer->Blt(NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &fx); + + CRect r2 = r; + + hr = m_backbuffer->Blt(&r2, m_merge, NULL, DDBLT_WAIT, NULL); + + r2 = cr; + + MapWindowPoints(m_hWnd, HWND_DESKTOP, (POINT*)&r2, 2); + + if(m_vsync) + { + hr = m_dd->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, NULL); + } + + hr = m_primary->Blt(&r2, m_backbuffer, &cr, DDBLT_WAIT, NULL); + + if(hr == DDERR_SURFACELOST) + { + // TODO + + HRESULT hr = m_dd->TestCooperativeLevel(); + + if(hr == DDERR_WRONGMODE) + { + } + } +} + +bool GSDevice7::Create(int type, Texture& t, int w, int h, int format) +{ + HRESULT hr; + + t = Texture(); + + DDSURFACEDESC2 desc; + + memset(&desc, 0, sizeof(desc)); + + desc.dwSize = sizeof(desc); + desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; + desc.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY; + desc.dwWidth = w; + desc.dwHeight = h; + desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat); + desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS; + desc.ddpfPixelFormat.dwRGBBitCount = 32; + desc.ddpfPixelFormat.dwRGBAlphaBitMask = 0xff000000; + desc.ddpfPixelFormat.dwRBitMask = 0x00ff0000; + desc.ddpfPixelFormat.dwGBitMask = 0x0000ff00; + desc.ddpfPixelFormat.dwBBitMask = 0x000000ff; + + CComPtr system, video; + + switch(type) + { + case GSTexture::RenderTarget: + case GSTexture::DepthStencil: + case GSTexture::Texture: + desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY; + if(FAILED(hr = m_dd->CreateSurface(&desc, &system, NULL))) return false; + desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY; + if(FAILED(hr = m_dd->CreateSurface(&desc, &video, NULL))) return false; + t = Texture(type, system, video); + break; + case GSTexture::Offscreen: + desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY; + if(FAILED(hr = m_dd->CreateSurface(&desc, &system, NULL))) return false; + t = Texture(type, system); + break; + } + + return !!t; +} + +void GSDevice7::DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c) +{ + HRESULT hr; + + hr = dt->Blt(NULL, st[0], NULL, DDBLT_WAIT, NULL); +} + +void GSDevice7::DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset) +{ +} diff --git a/plugins/GSdx/GSDevice7.h b/plugins/GSdx/GSDevice7.h new file mode 100644 index 0000000000..2afbce261f --- /dev/null +++ b/plugins/GSdx/GSDevice7.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice.h" +#include "GSTexture7.h" + +class GSDevice7 : public GSDevice +{ +public: + typedef GSTexture7 Texture; + +private: + CComPtr m_dd; + CComPtr m_primary; + CComPtr m_backbuffer; + + bool Create(int type, Texture& t, int w, int h, int format); + void DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c); + void DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset = 0); + +public: + GSDevice7(); + virtual ~GSDevice7(); + + bool Create(HWND hWnd, bool vsync); + bool Reset(int w, int h, bool fs); + bool IsLost() {return false;} + void Present(const CRect& r); + void BeginScene() {} + void EndScene() {} + void Draw(LPCTSTR str) {} + bool CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format = 0) {return false;} + + void ClearRenderTarget(Texture& t, const GSVector4& c) {} + void ClearRenderTarget(Texture& t, DWORD c) {} + void ClearDepth(Texture& t, float c) {} + void ClearStencil(Texture& t, BYTE c) {} +}; diff --git a/plugins/GSdx/GSDevice9.cpp b/plugins/GSdx/GSDevice9.cpp new file mode 100644 index 0000000000..57d9f198bf --- /dev/null +++ b/plugins/GSdx/GSDevice9.cpp @@ -0,0 +1,1062 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSDevice9.h" +#include "resource.h" + +GSDevice9::GSDevice9() + : m_vb(NULL) + , m_vb_count(0) + , m_vb_vertices(NULL) + , m_vb_stride(0) + , m_layout(NULL) + , m_topology((D3DPRIMITIVETYPE)0) + , m_vs(NULL) + , m_vs_cb(NULL) + , m_vs_cb_len(0) + , m_ps(NULL) + , m_ps_cb(NULL) + , m_ps_cb_len(0) + , m_ps_ss(NULL) + , m_scissor(0, 0, 0, 0) + , m_dss(NULL) + , m_sref(0) + , m_bs(NULL) + , m_bf(0xffffffff) + , m_rtv(NULL) + , m_dsv(NULL) +{ + memset(&m_pp, 0, sizeof(m_pp)); + memset(&m_ddcaps, 0, sizeof(m_ddcaps)); + memset(&m_d3dcaps, 0, sizeof(m_d3dcaps)); + memset(m_ps_srvs, 0, sizeof(m_ps_srvs)); +} + +GSDevice9::~GSDevice9() +{ + if(m_vs_cb) _aligned_free(m_vs_cb); + if(m_ps_cb) _aligned_free(m_ps_cb); +} + +bool GSDevice9::Create(HWND hWnd, bool vsync) +{ + if(!__super::Create(hWnd, vsync)) + { + return false; + } + + HRESULT hr; + + // dd + + CComPtr dd; + + hr = DirectDrawCreateEx(0, (void**)&dd, IID_IDirectDraw7, 0); + + if(FAILED(hr)) return false; + + memset(&m_ddcaps, 0, sizeof(m_ddcaps)); + + m_ddcaps.dwSize = sizeof(DDCAPS); + + hr = dd->GetCaps(&m_ddcaps, NULL); + + if(FAILED(hr)) return false; + + dd = NULL; + + // d3d + + m_d3d = Direct3DCreate9(D3D_SDK_VERSION); + + if(!m_d3d) return false; + + hr = m_d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8); + + if(FAILED(hr)) return false; + + hr = m_d3d->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_D24S8); + + if(FAILED(hr)) return false; + + memset(&m_d3dcaps, 0, sizeof(m_d3dcaps)); + + m_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_d3dcaps); + + bool fs = AfxGetApp()->GetProfileInt(_T("Settings"), _T("ModeWidth"), 0) > 0; + + if(!Reset(1, 1, fs)) return false; + + m_dev->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); + + // shaders + + DWORD psver = AfxGetApp()->GetProfileInt(_T("Settings"), _T("PixelShaderVersion2"), D3DPS_VERSION(2, 0)); + + if(psver > m_d3dcaps.PixelShaderVersion) + { + CString str; + + str.Format(_T("Supported pixel shader version is too low!\n\nSupported: %d.%d\nSelected: %d.%d"), + D3DSHADER_VERSION_MAJOR(m_d3dcaps.PixelShaderVersion), D3DSHADER_VERSION_MINOR(m_d3dcaps.PixelShaderVersion), + D3DSHADER_VERSION_MAJOR(psver), D3DSHADER_VERSION_MINOR(psver)); + + AfxMessageBox(str); + + return false; + } + + m_d3dcaps.PixelShaderVersion = min(psver, m_d3dcaps.PixelShaderVersion); + m_d3dcaps.VertexShaderVersion = m_d3dcaps.PixelShaderVersion & ~0x10000; + + // convert + + static const D3DVERTEXELEMENT9 il_convert[] = + { + {0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, + {0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, + D3DDECL_END() + }; + + CompileShader(IDR_CONVERT9_FX, "vs_main", NULL, &m_convert.vs, il_convert, countof(il_convert), &m_convert.il); + + for(int i = 0; i < countof(m_convert.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + CompileShader(IDR_CONVERT9_FX, main, NULL, &m_convert.ps[i]); + } + + m_convert.dss.DepthEnable = false; + m_convert.dss.StencilEnable = false; + + m_convert.bs.BlendEnable = false; + m_convert.bs.RenderTargetWriteMask = D3DCOLORWRITEENABLE_RGBA; + + m_convert.ln.FilterMin[0] = D3DTEXF_LINEAR; + m_convert.ln.FilterMag[0] = D3DTEXF_LINEAR; + m_convert.ln.FilterMin[1] = D3DTEXF_LINEAR; + m_convert.ln.FilterMag[1] = D3DTEXF_LINEAR; + m_convert.ln.AddressU = D3DTADDRESS_CLAMP; + m_convert.ln.AddressV = D3DTADDRESS_CLAMP; + + m_convert.pt.FilterMin[0] = D3DTEXF_POINT; + m_convert.pt.FilterMag[0] = D3DTEXF_POINT; + m_convert.pt.FilterMin[1] = D3DTEXF_POINT; + m_convert.pt.FilterMag[1] = D3DTEXF_POINT; + m_convert.pt.AddressU = D3DTADDRESS_CLAMP; + m_convert.pt.AddressV = D3DTADDRESS_CLAMP; + + // merge + + for(int i = 0; i < countof(m_merge.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + CompileShader(IDR_MERGE9_FX, main, NULL, &m_merge.ps[i]); + } + + m_merge.bs.BlendEnable = true; + m_merge.bs.BlendOp = D3DBLENDOP_ADD; + m_merge.bs.SrcBlend = D3DBLEND_SRCALPHA; + m_merge.bs.DestBlend = D3DBLEND_INVSRCALPHA; + m_merge.bs.BlendOpAlpha = D3DBLENDOP_ADD; + m_merge.bs.SrcBlendAlpha = D3DBLEND_ONE; + m_merge.bs.DestBlendAlpha = D3DBLEND_ZERO; + m_merge.bs.RenderTargetWriteMask = D3DCOLORWRITEENABLE_RGBA; + + // interlace + + for(int i = 0; i < countof(m_interlace.ps); i++) + { + CStringA main; + main.Format("ps_main%d", i); + CompileShader(IDR_INTERLACE9_FX, main, NULL, &m_interlace.ps[i]); + } + + // + + return true; +} + +bool GSDevice9::Reset(int w, int h, bool fs) +{ + if(!__super::Reset(w, h, fs)) + return false; + + HRESULT hr; + + if(!m_d3d) return false; + + if(m_swapchain && !fs && m_pp.Windowed) + { + m_swapchain = NULL; + + m_pp.BackBufferWidth = w; + m_pp.BackBufferHeight = h; + + hr = m_dev->CreateAdditionalSwapChain(&m_pp, &m_swapchain); + + if(FAILED(hr)) return false; + + CComPtr backbuffer; + hr = m_swapchain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); + m_backbuffer = Texture(backbuffer); + + return true; + } + + m_swapchain = NULL; + m_backbuffer = Texture(); + if(m_font) m_font->OnLostDevice(); + m_font = NULL; + + if(m_vs_cb) _aligned_free(m_vs_cb); + if(m_ps_cb) _aligned_free(m_ps_cb); + + m_vb = NULL; + m_vb_stride = 0; + m_layout = NULL; + m_vs = NULL; + m_vs_cb = NULL; + m_vs_cb_len = 0; + m_ps = NULL; + m_ps_cb = NULL; + m_ps_cb_len = 0; + m_ps_ss = NULL; + m_scissor = CRect(0, 0, 0, 0); + m_dss = NULL; + m_sref = 0; + m_bs = NULL; + m_bf = 0xffffffff; + m_rtv = NULL; + m_dsv = NULL; + + memset(&m_pp, 0, sizeof(m_pp)); + + m_pp.Windowed = TRUE; + m_pp.hDeviceWindow = m_hWnd; + m_pp.SwapEffect = D3DSWAPEFFECT_FLIP; + m_pp.BackBufferFormat = D3DFMT_X8R8G8B8; + m_pp.BackBufferWidth = 1; + m_pp.BackBufferHeight = 1; + m_pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + + if(m_vsync) + { + m_pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; + } + + if(!!AfxGetApp()->GetProfileInt(_T("Settings"), _T("tvout"), FALSE)) + { + m_pp.Flags |= D3DPRESENTFLAG_VIDEO; + } + + int mw = AfxGetApp()->GetProfileInt(_T("Settings"), _T("ModeWidth"), 0); + int mh = AfxGetApp()->GetProfileInt(_T("Settings"), _T("ModeHeight"), 0); + int mrr = AfxGetApp()->GetProfileInt(_T("Settings"), _T("ModeRefreshRate"), 0); + + if(fs && mw > 0 && mh > 0 && mrr >= 0) + { + m_pp.Windowed = FALSE; + m_pp.BackBufferWidth = mw; + m_pp.BackBufferHeight = mh; + // m_pp.FullScreen_RefreshRateInHz = mrr; + + ::SetWindowLong(m_hWnd, GWL_STYLE, ::GetWindowLong(m_hWnd, GWL_STYLE) & ~(WS_CAPTION|WS_THICKFRAME)); + ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); + ::SetMenu(m_hWnd, NULL); + } + + if(!m_dev) + { + UINT flags = D3DCREATE_MULTITHREADED | (m_d3dcaps.VertexProcessingCaps ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING); + + hr = m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, flags, &m_pp, &m_dev); + + if(FAILED(hr)) return false; + } + else + { + hr = m_dev->Reset(&m_pp); + + if(FAILED(hr)) + { + if(D3DERR_DEVICELOST == hr) + { + Sleep(1000); + + hr = m_dev->Reset(&m_pp); + } + + if(FAILED(hr)) return false; + } + } + + if(m_pp.Windowed) + { + m_pp.BackBufferWidth = 1; + m_pp.BackBufferHeight = 1; + + hr = m_dev->CreateAdditionalSwapChain(&m_pp, &m_swapchain); + + if(FAILED(hr)) return false; + } + + CComPtr backbuffer; + + if(m_swapchain) + { + hr = m_swapchain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); + } + else + { + hr = m_dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); + } + + m_backbuffer = Texture(backbuffer); + + D3DXFONT_DESC fd; + memset(&fd, 0, sizeof(fd)); + _tcscpy(fd.FaceName, _T("Arial")); + fd.Height = 20; + D3DXCreateFontIndirect(m_dev, &fd, &m_font); + + m_dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + m_dev->SetRenderState(D3DRS_LIGHTING, FALSE); + m_dev->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); + m_dev->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); + + return true; +} + +bool GSDevice9::IsLost() +{ + HRESULT hr = m_dev->TestCooperativeLevel(); + + return hr == D3DERR_DEVICELOST || hr == D3DERR_DEVICENOTRESET; +} + +void GSDevice9::Present(const CRect& r) +{ + CRect cr; + + GetClientRect(m_hWnd, &cr); + + if(m_backbuffer.GetWidth() != cr.Width() || m_backbuffer.GetHeight() != cr.Height()) + { + Reset(cr.Width(), cr.Height(), false); + } + + OMSetRenderTargets(m_backbuffer, NULL); + + m_dev->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); + + if(m_current) + { + StretchRect(m_current, m_backbuffer, GSVector4(r)); + } + + if(m_swapchain) + { + m_swapchain->Present(NULL, NULL, NULL, NULL, 0); + } + else + { + m_dev->Present(NULL, NULL, NULL, NULL); + } +} + +void GSDevice9::BeginScene() +{ + m_dev->BeginScene(); +} + +void GSDevice9::EndScene() +{ + m_dev->EndScene(); +} + +void GSDevice9::Draw(LPCTSTR str) +{ + /* + if(!m_pp.Windowed) + { + BeginScene(); + + OMSetRenderTargets(m_backbuffer, NULL); + + CRect r(0, 0, m_backbuffer.GetWidth(), m_backbuffer.GetHeight()); + + D3DCOLOR c = D3DCOLOR_ARGB(255, 0, 255, 0); + + if(m_font->DrawText(NULL, str, -1, &r, DT_CALCRECT|DT_LEFT|DT_WORDBREAK, c)) + { + m_font->DrawText(NULL, str, -1, &r, DT_LEFT|DT_WORDBREAK, c); + } + + EndScene(); + } + */ +} + +bool GSDevice9::CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format) +{ + dst = Texture(); + + if(format == 0) + { + format = D3DFMT_A8R8G8B8; + } + + if(format != D3DFMT_A8R8G8B8) + { + ASSERT(0); + + return false; + } + + Texture rt; + + if(CreateRenderTarget(rt, w, h, format)) + { + GSVector4 dr(0, 0, w, h); + + StretchRect(src, sr, rt, dr, m_convert.ps[1], NULL, 0); + + if(CreateOffscreen(dst, w, h, format)) + { + m_dev->GetRenderTargetData(rt, dst); + } + } + + Recycle(rt); + + return !!dst; +} + +void GSDevice9::ClearRenderTarget(Texture& t, const GSVector4& c) +{ + ClearRenderTarget(t, D3DCOLOR_RGBA((BYTE)(c.r * 255 + 0.5f), (BYTE)(c.g * 255 + 0.5f), (BYTE)(c.b * 255 + 0.5f), (BYTE)(c.a * 255 + 0.5f))); +} + +void GSDevice9::ClearRenderTarget(Texture& t, DWORD c) +{ + CComPtr surface; + m_dev->GetRenderTarget(0, &surface); + m_dev->SetRenderTarget(0, t); + m_dev->Clear(0, NULL, D3DCLEAR_TARGET, c, 0, 0); + m_dev->SetRenderTarget(0, surface); +} + +void GSDevice9::ClearDepth(Texture& t, float c) +{ + CComPtr surface; + m_dev->GetDepthStencilSurface(&surface); + m_dev->SetDepthStencilSurface(t); + m_dev->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, c, 0); + m_dev->SetDepthStencilSurface(surface); +} + +void GSDevice9::ClearStencil(Texture& t, BYTE c) +{ + CComPtr surface; + m_dev->GetDepthStencilSurface(&surface); + m_dev->SetDepthStencilSurface(t); + m_dev->Clear(0, NULL, D3DCLEAR_STENCIL, 0, 0, c); + m_dev->SetDepthStencilSurface(surface); +} + +bool GSDevice9::Create(int type, Texture& t, int w, int h, int format) +{ + HRESULT hr; + + CComPtr texture; + CComPtr surface; + + switch(type) + { + case GSTexture::RenderTarget: + hr = m_dev->CreateTexture(w, h, 1, D3DUSAGE_RENDERTARGET, (D3DFORMAT)format, D3DPOOL_DEFAULT, &texture, NULL); + break; + case GSTexture::DepthStencil: + hr = m_dev->CreateDepthStencilSurface(w, h, (D3DFORMAT)format, D3DMULTISAMPLE_NONE, 0, FALSE, &surface, NULL); + break; + case GSTexture::Texture: + hr = m_dev->CreateTexture(w, h, 1, 0, (D3DFORMAT)format, D3DPOOL_MANAGED, &texture, NULL); + break; + case GSTexture::Offscreen: + hr = m_dev->CreateOffscreenPlainSurface(w, h, (D3DFORMAT)format, D3DPOOL_SYSTEMMEM, &surface, NULL); + break; + } + + if(surface) + { + t = Texture(surface); + } + + if(texture) + { + t = Texture(texture); + } + + if(t) + { + switch(type) + { + case GSTexture::RenderTarget: + ClearRenderTarget(t, 0); + break; + case GSTexture::DepthStencil: + ClearDepth(t, 0); + break; + } + + return t; + } + + return false; +} + +bool GSDevice9::CreateRenderTarget(Texture& t, int w, int h, int format) +{ + return __super::CreateRenderTarget(t, w, h, format ? format : D3DFMT_A8R8G8B8); +} + +bool GSDevice9::CreateDepthStencil(Texture& t, int w, int h, int format) +{ + return __super::CreateDepthStencil(t, w, h, format ? format : D3DFMT_D24S8); +} + +bool GSDevice9::CreateTexture(Texture& t, int w, int h, int format) +{ + return __super::CreateTexture(t, w, h, format ? format : D3DFMT_A8R8G8B8); +} + +bool GSDevice9::CreateOffscreen(Texture& t, int w, int h, int format) +{ + return __super::CreateOffscreen(t, w, h, format ? format : D3DFMT_A8R8G8B8); +} + +void GSDevice9::DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c) +{ + ClearRenderTarget(dt, c); + + if(st[1] && !slbg) + { + StretchRect(st[1], sr[1], dt, dr[1], m_merge.ps[0], NULL, true); + } + + if(st[0]) + { + MergeConstantBuffer cb; + + cb.BGColor = c; + + StretchRect(st[0], sr[0], dt, dr[0], m_merge.ps[mmod ? 1 : 0], (const float*)&cb, 1, &m_merge.bs, true); + } +} + +void GSDevice9::DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset) +{ + GSVector4 sr(0, 0, 1, 1); + GSVector4 dr(0.0f, yoffset, (float)dt.GetWidth(), (float)dt.GetHeight() + yoffset); + + InterlaceConstantBuffer cb; + + cb.ZrH = GSVector2(0, 1.0f / dt.GetHeight()); + cb.hH = (float)dt.GetHeight() / 2; + + StretchRect(st, sr, dt, dr, m_interlace.ps[shader], (const float*)&cb, 1, linear); +} +/* +void GSDevice9::IASetVertexBuffer(IDirect3DVertexBuffer9* vb, UINT count, const void* vertices, UINT stride) +{ + void* data = NULL; + + if(SUCCEEDED(vb->Lock(0, count * stride, &data, D3DLOCK_DISCARD))) + { + memcpy(data, vertices, count * stride); + + vb->Unlock(); + } + + if(m_vb != vb || m_vb_stride != stride) + { + m_dev->SetStreamSource(0, vb, 0, stride); + + m_vb = vb; + m_vb_stride = stride; + } +} +*/ +void GSDevice9::IASetVertexBuffer(UINT count, const void* vertices, UINT stride) +{ + m_vb_count = count; + m_vb_vertices = vertices; + m_vb_stride = stride; +} + +void GSDevice9::IASetInputLayout(IDirect3DVertexDeclaration9* layout) +{ + // TODO: get rid of all SetFVF before enabling this + + // if(m_layout != layout) + { + m_dev->SetVertexDeclaration(layout); + + // m_layout = layout; + } +} + +void GSDevice9::IASetPrimitiveTopology(D3DPRIMITIVETYPE topology) +{ + m_topology = topology; +} + +void GSDevice9::VSSetShader(IDirect3DVertexShader9* vs, const float* vs_cb, int vs_cb_len) +{ + if(m_vs != vs) + { + m_dev->SetVertexShader(vs); + + m_vs = vs; + } + + if(vs_cb && vs_cb_len > 0) + { + int size = vs_cb_len * sizeof(float) * 4; + + if(m_vs_cb_len != vs_cb_len || m_vs_cb == NULL || memcmp(m_vs_cb, vs_cb, size)) + { + if(m_vs_cb == NULL || m_vs_cb_len < vs_cb_len) + { + if(m_vs_cb) _aligned_free(m_vs_cb); + + m_vs_cb = (float*)_aligned_malloc(size, 16); + } + + memcpy(m_vs_cb, vs_cb, size); + + m_dev->SetVertexShaderConstantF(0, vs_cb, vs_cb_len); + + m_vs_cb_len = vs_cb_len; + } + } +} + +void GSDevice9::PSSetShaderResources(IDirect3DTexture9* srv0, IDirect3DTexture9* srv1) +{ + if(m_ps_srvs[0] != srv0) + { + m_dev->SetTexture(0, srv0); + + m_ps_srvs[0] = srv0; + } + + if(m_ps_srvs[1] != srv1) + { + m_dev->SetTexture(1, srv1); + + m_ps_srvs[1] = srv1; + } +} + +void GSDevice9::PSSetShader(IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len) +{ + if(m_ps != ps) + { + m_dev->SetPixelShader(ps); + + m_ps = ps; + } + + if(ps_cb && ps_cb_len > 0) + { + int size = ps_cb_len * sizeof(float) * 4; + + if(m_ps_cb_len != ps_cb_len || m_ps_cb == NULL || memcmp(m_ps_cb, ps_cb, size)) + { + if(m_ps_cb == NULL || m_ps_cb_len < ps_cb_len) + { + if(m_ps_cb) _aligned_free(m_ps_cb); + + m_ps_cb = (float*)_aligned_malloc(size, 16); + } + + memcpy(m_ps_cb, ps_cb, size); + + m_dev->SetPixelShaderConstantF(0, ps_cb, ps_cb_len); + + m_ps_cb_len = ps_cb_len; + } + } +} + +void GSDevice9::PSSetSamplerState(Direct3DSamplerState9* ss) +{ + if(ss && m_ps_ss != ss) + { + m_dev->SetSamplerState(0, D3DSAMP_ADDRESSU, ss->AddressU); + m_dev->SetSamplerState(0, D3DSAMP_ADDRESSV, ss->AddressV); + m_dev->SetSamplerState(1, D3DSAMP_ADDRESSU, ss->AddressU); + m_dev->SetSamplerState(1, D3DSAMP_ADDRESSV, ss->AddressV); + m_dev->SetSamplerState(2, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); + m_dev->SetSamplerState(2, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); + m_dev->SetSamplerState(3, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); + m_dev->SetSamplerState(3, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); + m_dev->SetSamplerState(0, D3DSAMP_MINFILTER, ss->FilterMin[0]); + m_dev->SetSamplerState(0, D3DSAMP_MAGFILTER, ss->FilterMag[0]); + m_dev->SetSamplerState(1, D3DSAMP_MINFILTER, ss->FilterMin[1]); + m_dev->SetSamplerState(1, D3DSAMP_MAGFILTER, ss->FilterMag[1]); + m_dev->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_POINT); + m_dev->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_POINT); + m_dev->SetSamplerState(3, D3DSAMP_MINFILTER, D3DTEXF_POINT); + m_dev->SetSamplerState(3, D3DSAMP_MAGFILTER, D3DTEXF_POINT); + + m_ps_ss = ss; + } +} + +void GSDevice9::RSSet(int width, int height, const RECT* scissor) +{ + CRect r = scissor ? *scissor : CRect(0, 0, width, height); + + if(m_scissor != r) + { + m_dev->SetScissorRect(&r); + + m_scissor = r; + } +} + +void GSDevice9::OMSetDepthStencilState(Direct3DDepthStencilState9* dss, UINT sref) +{ + if(m_dss != dss || m_sref != sref) + { + m_dev->SetRenderState(D3DRS_ZENABLE, dss->DepthEnable); + m_dev->SetRenderState(D3DRS_ZWRITEENABLE, dss->DepthWriteMask); + + if(dss->DepthEnable) + { + m_dev->SetRenderState(D3DRS_ZFUNC, dss->DepthFunc); + } + + m_dev->SetRenderState(D3DRS_STENCILENABLE, dss->StencilEnable); + + if(dss->StencilEnable) + { + m_dev->SetRenderState(D3DRS_STENCILMASK, dss->StencilReadMask); + m_dev->SetRenderState(D3DRS_STENCILWRITEMASK, dss->StencilWriteMask); + m_dev->SetRenderState(D3DRS_STENCILFUNC, dss->StencilFunc); + m_dev->SetRenderState(D3DRS_STENCILPASS, dss->StencilPassOp); + m_dev->SetRenderState(D3DRS_STENCILFAIL, dss->StencilFailOp); + m_dev->SetRenderState(D3DRS_STENCILZFAIL, dss->StencilDepthFailOp); + m_dev->SetRenderState(D3DRS_STENCILREF, sref); + } + + m_dss = dss; + m_sref = sref; + } +} + +void GSDevice9::OMSetBlendState(Direct3DBlendState9* bs, DWORD bf) +{ + if(m_bs != bs || m_bf != bf) + { + m_dev->SetRenderState(D3DRS_ALPHABLENDENABLE, bs->BlendEnable); + + if(bs->BlendEnable) + { + m_dev->SetRenderState(D3DRS_BLENDOP, bs->BlendOp); + m_dev->SetRenderState(D3DRS_SRCBLEND, bs->SrcBlend); + m_dev->SetRenderState(D3DRS_DESTBLEND, bs->DestBlend); + m_dev->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); + m_dev->SetRenderState(D3DRS_BLENDOPALPHA, bs->BlendOpAlpha); + m_dev->SetRenderState(D3DRS_SRCBLENDALPHA, bs->SrcBlendAlpha); + m_dev->SetRenderState(D3DRS_DESTBLENDALPHA, bs->DestBlendAlpha); + m_dev->SetRenderState(D3DRS_BLENDFACTOR, bf); + } + + m_dev->SetRenderState(D3DRS_COLORWRITEENABLE, bs->RenderTargetWriteMask); + + m_bs = bs; + m_bf = bf; + } +} + +void GSDevice9::OMSetRenderTargets(IDirect3DSurface9* rtv, IDirect3DSurface9* dsv) +{ + if(m_rtv != rtv) + { + m_dev->SetRenderTarget(0, rtv); + + m_rtv = rtv; + } + + if(m_dsv != dsv) + { + m_dev->SetDepthStencilSurface(dsv); + + m_dsv = dsv; + } +} + +void GSDevice9::DrawPrimitive() +{ + int prims = 0; + + switch(m_topology) + { + case D3DPT_TRIANGLELIST: + prims = m_vb_count / 3; + break; + case D3DPT_LINELIST: + prims = m_vb_count / 2; + break; + case D3DPT_POINTLIST: + prims = m_vb_count; + break; + case D3DPT_TRIANGLESTRIP: + case D3DPT_TRIANGLEFAN: + prims = m_vb_count - 2; + break; + case D3DPT_LINESTRIP: + prims = m_vb_count - 1; + break; + } + + m_dev->DrawPrimitiveUP(m_topology, prims, m_vb_vertices, m_vb_stride); +} + +void GSDevice9::StretchRect(Texture& st, Texture& dt, const GSVector4& dr, bool linear) +{ + StretchRect(st, GSVector4(0, 0, 1, 1), dt, dr, linear); +} + +void GSDevice9::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, bool linear) +{ + StretchRect(st, sr, dt, dr, m_convert.ps[0], NULL, 0, linear); +} + +void GSDevice9::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, bool linear) +{ + StretchRect(st, sr, dt, dr, ps, ps_cb, ps_cb_len, &m_convert.bs, linear); +} + +void GSDevice9::StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, Direct3DBlendState9* bs, bool linear) +{ + BeginScene(); + + // om + + OMSetDepthStencilState(&m_convert.dss, 0); + OMSetBlendState(bs, 0); + OMSetRenderTargets(dt, NULL); + + // ia + + float left = dr.x * 2 / dt.GetWidth() - 1.0f; + float top = 1.0f - dr.y * 2 / dt.GetHeight(); + float right = dr.z * 2 / dt.GetWidth() - 1.0f; + float bottom = 1.0f - dr.w * 2 / dt.GetHeight(); + + GSVertexPT1 vertices[] = + { + {GSVector4(left, top, 0.5f, 1.0f), GSVector2(sr.x, sr.y)}, + {GSVector4(right, top, 0.5f, 1.0f), GSVector2(sr.z, sr.y)}, + {GSVector4(left, bottom, 0.5f, 1.0f), GSVector2(sr.x, sr.w)}, + {GSVector4(right, bottom, 0.5f, 1.0f), GSVector2(sr.z, sr.w)}, + }; + + for(int i = 0; i < countof(vertices); i++) + { + vertices[i].p.x -= 1.0f / dt.GetWidth(); + vertices[i].p.y += 1.0f / dt.GetHeight(); + } + + IASetVertexBuffer(4, vertices); + IASetInputLayout(m_convert.il); + IASetPrimitiveTopology(D3DPT_TRIANGLESTRIP); + + // vs + + VSSetShader(m_convert.vs, NULL, 0); + + // ps + + PSSetShader(ps, ps_cb, ps_cb_len); + PSSetSamplerState(linear ? &m_convert.ln : &m_convert.pt); + PSSetShaderResources(st, NULL); + + // rs + + RSSet(dt.GetWidth(), dt.GetHeight()); + + // + + DrawPrimitive(); + + // + + EndScene(); +} + +// FIXME: D3DXCompileShaderFromResource of d3dx9 v37 (march 2008) calls GetFullPathName on id for some reason and then crashes + +static HRESULT LoadShader(UINT id, LPCSTR& data, DWORD& size) +{ + CComPtr shader, error; + + HRSRC hRes = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(id), RT_RCDATA); + + if(!hRes) return E_FAIL; + + size = SizeofResource(AfxGetResourceHandle(), hRes); + + if(size == 0) return E_FAIL; + + HGLOBAL hResData = LoadResource(AfxGetResourceHandle(), hRes); + + if(!hResData) return E_FAIL; + + data = (LPCSTR)LockResource(hResData); + + return S_OK; +} + +HRESULT GSDevice9::CompileShader(UINT id, LPCSTR entry, const D3DXMACRO* macro, IDirect3DVertexShader9** vs, const D3DVERTEXELEMENT9* layout, int count, IDirect3DVertexDeclaration9** il) +{ + LPCSTR target; + + if(m_d3dcaps.VertexShaderVersion >= D3DVS_VERSION(3, 0)) + { + target = "vs_3_0"; + } + else if(m_d3dcaps.VertexShaderVersion >= D3DVS_VERSION(2, 0)) + { + target = "vs_2_0"; + } + else + { + return E_FAIL; + } + + HRESULT hr; + + CComPtr shader, error; + + // FIXME: hr = D3DXCompileShaderFromResource(AfxGetResourceHandle(), MAKEINTRESOURCE(id), macro, NULL, entry, target, 0, &shader, &error, NULL); + + LPCSTR data; + DWORD size; + + hr = LoadShader(id, data, size); + + if(FAILED(hr)) return E_FAIL; + + hr = D3DXCompileShader(data, size, macro, NULL, entry, target, 0, &shader, &error, NULL); + + if(SUCCEEDED(hr)) + { + hr = m_dev->CreateVertexShader((DWORD*)shader->GetBufferPointer(), vs); + } + else if(error) + { + LPCSTR msg = (LPCSTR)error->GetBufferPointer(); + + TRACE(_T("%s\n"), CString(msg)); + } + + ASSERT(SUCCEEDED(hr)); + + if(FAILED(hr)) + { + return hr; + } + + hr = m_dev->CreateVertexDeclaration(layout, il); + + if(FAILED(hr)) + { + return hr; + } + + return S_OK; +} + +HRESULT GSDevice9::CompileShader(UINT id, LPCSTR entry, const D3DXMACRO* macro, IDirect3DPixelShader9** ps) +{ + LPCSTR target = NULL; + UINT flags = 0; + + if(m_d3dcaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) + { + target = "ps_3_0"; + flags |= D3DXSHADER_AVOID_FLOW_CONTROL; + } + else if(m_d3dcaps.PixelShaderVersion >= D3DPS_VERSION(2, 0)) + { + target = "ps_2_0"; + } + else + { + return false; + } + + HRESULT hr; + + CComPtr shader, error; + + // FIXME: hr = D3DXCompileShaderFromResource(AfxGetResourceHandle(), MAKEINTRESOURCE(id), macro, NULL, entry, target, flags, &shader, &error, NULL); + + LPCSTR data; + DWORD size; + + hr = LoadShader(id, data, size); + + if(FAILED(hr)) return E_FAIL; + + hr = D3DXCompileShader(data, size, macro, NULL, entry, target, 0, &shader, &error, NULL); + + if(SUCCEEDED(hr)) + { + hr = m_dev->CreatePixelShader((DWORD*)shader->GetBufferPointer(), ps); + + ASSERT(SUCCEEDED(hr)); + } + else if(error) + { + LPCSTR msg = (LPCSTR)error->GetBufferPointer(); + + TRACE(_T("%s\n"), CString(msg)); + } + + ASSERT(SUCCEEDED(hr)); + + if(FAILED(hr)) + { + return hr; + } + + return S_OK; +} diff --git a/plugins/GSdx/GSDevice9.h b/plugins/GSdx/GSDevice9.h new file mode 100644 index 0000000000..0a4fe10e43 --- /dev/null +++ b/plugins/GSdx/GSDevice9.h @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice.h" +#include "GSTexture9.h" + +struct Direct3DSamplerState9 +{ + D3DTEXTUREFILTERTYPE FilterMin[2]; + D3DTEXTUREFILTERTYPE FilterMag[2]; + D3DTEXTUREADDRESS AddressU; + D3DTEXTUREADDRESS AddressV; +}; + +struct Direct3DDepthStencilState9 +{ + BOOL DepthEnable; + BOOL DepthWriteMask; + D3DCMPFUNC DepthFunc; + BOOL StencilEnable; + UINT8 StencilReadMask; + UINT8 StencilWriteMask; + D3DSTENCILOP StencilFailOp; + D3DSTENCILOP StencilDepthFailOp; + D3DSTENCILOP StencilPassOp; + D3DCMPFUNC StencilFunc; +}; + +struct Direct3DBlendState9 +{ + BOOL BlendEnable; + D3DBLEND SrcBlend; + D3DBLEND DestBlend; + D3DBLENDOP BlendOp; + D3DBLEND SrcBlendAlpha; + D3DBLEND DestBlendAlpha; + D3DBLENDOP BlendOpAlpha; + UINT8 RenderTargetWriteMask; +}; + +class GSDevice9 : public GSDevice +{ +public: + typedef GSTexture9 Texture; + +private: + // state cache + + IDirect3DVertexBuffer9* m_vb; + UINT m_vb_count; + const void* m_vb_vertices; + UINT m_vb_stride; + IDirect3DVertexDeclaration9* m_layout; + D3DPRIMITIVETYPE m_topology; + IDirect3DVertexShader9* m_vs; + float* m_vs_cb; + int m_vs_cb_len; + IDirect3DTexture9* m_ps_srvs[2]; + IDirect3DPixelShader9* m_ps; + float* m_ps_cb; + int m_ps_cb_len; + Direct3DSamplerState9* m_ps_ss; + CRect m_scissor; + Direct3DDepthStencilState9* m_dss; + UINT m_sref; + Direct3DBlendState9* m_bs; + DWORD m_bf; + IDirect3DSurface9* m_rtv; + IDirect3DSurface9* m_dsv; + + // + + bool Create(int type, Texture& t, int w, int h, int format); + void DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c); + void DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset = 0); + + // + + DDCAPS m_ddcaps; + D3DCAPS9 m_d3dcaps; + CComPtr m_d3d; + CComPtr m_dev; + CComPtr m_swapchain; + Texture m_backbuffer; + +public: // TODO + D3DPRESENT_PARAMETERS m_pp; + CComPtr m_font; + + struct + { + CComPtr vs; + CComPtr il; + CComPtr ps[5]; + Direct3DSamplerState9 ln; + Direct3DSamplerState9 pt; + Direct3DDepthStencilState9 dss; + Direct3DBlendState9 bs; + } m_convert; + + struct + { + CComPtr ps[2]; + Direct3DBlendState9 bs; + } m_merge; + + struct + { + CComPtr ps[4]; + } m_interlace; + +public: + GSDevice9(); + virtual ~GSDevice9(); + + bool Create(HWND hWnd, bool vsync); + bool Reset(int w, int h, bool fs); + bool IsLost(); + void Present(const CRect& r); + void BeginScene(); + void EndScene(); + void Draw(LPCTSTR str); + bool CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format = 0); + + void ClearRenderTarget(Texture& t, const GSVector4& c); + void ClearRenderTarget(Texture& t, DWORD c); + void ClearDepth(Texture& t, float c); + void ClearStencil(Texture& t, BYTE c); + + bool CreateRenderTarget(Texture& t, int w, int h, int format = 0); + bool CreateDepthStencil(Texture& t, int w, int h, int format = 0); + bool CreateTexture(Texture& t, int w, int h, int format = 0); + bool CreateOffscreen(Texture& t, int w, int h, int format = 0); + + IDirect3DDevice9* operator->() {return m_dev;} + operator IDirect3DDevice9*() {return m_dev;} + + // TODO: void IASetVertexBuffer(IDirect3DVertexBuffer9* vb, UINT count, const void* vertices, UINT stride); + void IASetVertexBuffer(UINT count, const void* vertices, UINT stride); + void IASetInputLayout(IDirect3DVertexDeclaration9* layout); + void IASetPrimitiveTopology(D3DPRIMITIVETYPE topology); + void VSSetShader(IDirect3DVertexShader9* vs, const float* vs_cb, int vs_cb_len); + void PSSetShaderResources(IDirect3DTexture9* srv0, IDirect3DTexture9* srv1); + void PSSetShader(IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len); + void PSSetSamplerState(Direct3DSamplerState9* ss); + void RSSet(int width, int height, const RECT* scissor = NULL); + void OMSetDepthStencilState(Direct3DDepthStencilState9* dss, UINT sref); + void OMSetBlendState(Direct3DBlendState9* bs, DWORD bf); + void OMSetRenderTargets(IDirect3DSurface9* rtv, IDirect3DSurface9* dsv); + void DrawPrimitive(); + + template void IASetVertexBuffer(UINT count, T* vertices) + { + IASetVertexBuffer(count, vertices, sizeof(T)); + } + + void StretchRect(Texture& st, Texture& dt, const GSVector4& dr, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, bool linear = true); + void StretchRect(Texture& st, const GSVector4& sr, Texture& dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, Direct3DBlendState9* bs, bool linear = true); + + HRESULT CompileShader(UINT id, LPCSTR entry, const D3DXMACRO* macro, IDirect3DVertexShader9** vs, const D3DVERTEXELEMENT9* layout, int count, IDirect3DVertexDeclaration9** il); + HRESULT CompileShader(UINT id, LPCSTR entry, const D3DXMACRO* macro, IDirect3DPixelShader9** ps); + + virtual bool IsCurrentRGBA() + { + return false; + } + + // TODO + bool SaveToFileD24S8(IDirect3DSurface9* ds, LPCTSTR fn); +}; diff --git a/plugins/GSdx/GSDeviceNull.cpp b/plugins/GSdx/GSDeviceNull.cpp new file mode 100644 index 0000000000..695293f97e --- /dev/null +++ b/plugins/GSdx/GSDeviceNull.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSDeviceNull.h" + +bool GSDeviceNull::Create(HWND hWnd, bool vsync) +{ + if(!__super::Create(hWnd, vsync)) + { + return false; + } + + Reset(1, 1, true); + + return true; +} + +bool GSDeviceNull::Reset(int w, int h, bool fs) +{ + if(!__super::Reset(w, h, fs)) + return false; + + return true; +} + +bool GSDeviceNull::Create(int type, Texture& t, int w, int h, int format) +{ + t = Texture(type, w, h, format); + + return true; +} diff --git a/plugins/GSdx/GSDeviceNull.h b/plugins/GSdx/GSDeviceNull.h new file mode 100644 index 0000000000..01bb3261d6 --- /dev/null +++ b/plugins/GSdx/GSDeviceNull.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice.h" +#include "GSTextureNull.h" + +class GSDeviceNull : public GSDevice +{ +public: + typedef GSTextureNull Texture; + +private: + bool Create(int type, Texture& t, int w, int h, int format); + void DoMerge(Texture* st, GSVector4* sr, GSVector4* dr, Texture& dt, bool slbg, bool mmod, GSVector4& c) {} + void DoInterlace(Texture& st, Texture& dt, int shader, bool linear, float yoffset = 0) {} + +public: + GSDeviceNull() {} + + bool Create(HWND hWnd, bool vsync); + bool Reset(int w, int h, bool fs); + bool IsLost() {return false;} + void Present(const CRect& r) {} + void BeginScene() {} + void EndScene() {} + void Draw(LPCTSTR str) {} + bool CopyOffscreen(Texture& src, const GSVector4& sr, Texture& dst, int w, int h, int format = 0) {return false;} + + void ClearRenderTarget(Texture& t, const GSVector4& c) {} + void ClearRenderTarget(Texture& t, DWORD c) {} + void ClearDepth(Texture& t, float c) {} + void ClearStencil(Texture& t, BYTE c) {} +}; diff --git a/plugins/GSdx/GSDirtyRect.cpp b/plugins/GSdx/GSDirtyRect.cpp new file mode 100644 index 0000000000..672bad9edb --- /dev/null +++ b/plugins/GSdx/GSDirtyRect.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSDirtyRect.h" + +GSDirtyRect::GSDirtyRect() + : m_psm(PSM_PSMCT32) + , m_rect(0, 0, 0, 0) +{ +} + +GSDirtyRect::GSDirtyRect(DWORD psm, CRect rect) +{ + m_psm = psm; + m_rect = rect; +} + +CRect GSDirtyRect::GetDirtyRect(const GIFRegTEX0& TEX0) +{ + CRect r = m_rect; + + CSize src = GSLocalMemory::m_psm[m_psm].bs; + + r.left = (r.left) & ~(src.cx-1); + r.right = (r.right + (src.cx-1) /* + 1 */) & ~(src.cx-1); + r.top = (r.top) & ~(src.cy-1); + r.bottom = (r.bottom + (src.cy-1) /* + 1 */) & ~(src.cy-1); + + if(m_psm != TEX0.PSM) + { + CSize dst = GSLocalMemory::m_psm[TEX0.PSM].bs; + + r.left = MulDiv(m_rect.left, dst.cx, src.cx); + r.right = MulDiv(m_rect.right, dst.cx, src.cx); + r.top = MulDiv(m_rect.top, dst.cy, src.cy); + r.bottom = MulDiv(m_rect.bottom, dst.cy, src.cy); + } + + return r; +} + +// + +CRect GSDirtyRectList::GetDirtyRect(const GIFRegTEX0& TEX0, CSize size) +{ + if(IsEmpty()) return CRect(0, 0, 0, 0); + CRect r(INT_MAX, INT_MAX, 0, 0); + POSITION pos = GetHeadPosition(); + while(pos) r |= GetNext(pos).GetDirtyRect(TEX0); + return r & CRect(0, 0, size.cx, size.cy); +} diff --git a/plugins/GSdx/GSDirtyRect.h b/plugins/GSdx/GSDirtyRect.h new file mode 100644 index 0000000000..0babdb494a --- /dev/null +++ b/plugins/GSdx/GSDirtyRect.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSLocalMemory.h" + +class GSDirtyRect +{ + DWORD m_psm; + CRect m_rect; + +public: + GSDirtyRect(); + GSDirtyRect(DWORD psm, CRect rect); + CRect GetDirtyRect(const GIFRegTEX0& TEX0); +}; + +class GSDirtyRectList : public CAtlList +{ +public: + GSDirtyRectList() {} + CRect GetDirtyRect(const GIFRegTEX0& TEX0, CSize size); +}; \ No newline at end of file diff --git a/plugins/GSdx/GSDrawScanline.cpp b/plugins/GSdx/GSDrawScanline.cpp new file mode 100644 index 0000000000..b62052deb5 --- /dev/null +++ b/plugins/GSdx/GSDrawScanline.cpp @@ -0,0 +1,2845 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSDrawScanline.h" +#include "GSTextureCacheSW.h" + +GSDrawScanline::GSDrawScanline(GSState* state, int id) + : m_state(state) + , m_id(id) +{ + memset(&m_env, 0, sizeof(m_env)); +} + +GSDrawScanline::~GSDrawScanline() +{ +} + +void GSDrawScanline::BeginDraw(const GSRasterizerData* data, Functions* f) +{ + GSDrawingEnvironment& env = m_state->m_env; + GSDrawingContext* context = m_state->m_context; + + const GSScanlineParam* p = (const GSScanlineParam*)data->param; + + m_env.sel = p->sel; + + m_env.vm = p->vm; + m_env.fbr = p->fbo->row; + m_env.zbr = p->zbo->row; + m_env.fbc = p->fbo->col; + m_env.zbc = p->zbo->col; + m_env.fzbr = p->fzbo->row; + m_env.fzbc = p->fzbo->col; + m_env.fm = GSVector4i(p->fm); + m_env.zm = GSVector4i(p->zm); + m_env.datm = context->TEST.DATM ? GSVector4i::x80000000() : GSVector4i::zero(); + m_env.colclamp = env.COLCLAMP.CLAMP ? GSVector4i::xffffffff() : GSVector4i::x00ff(); + m_env.fba = context->FBA.FBA ? GSVector4i::x80000000() : GSVector4i::zero(); + m_env.aref = GSVector4i((int)context->TEST.AREF); + m_env.afix = GSVector4i((int)context->ALPHA.FIX << 16); + m_env.afix2 = m_env.afix.yywwlh().sll16(7); + m_env.frb = GSVector4i((int)env.FOGCOL.ai32[0] & 0x00ff00ff); + m_env.fga = GSVector4i((int)(env.FOGCOL.ai32[0] >> 8) & 0x00ff00ff); + + if(m_env.sel.fpsm == 1) + { + m_env.fm |= GSVector4i::xff000000(); + } + else if(m_env.sel.fpsm == 2) + { + GSVector4i rb = m_env.fm & 0x00f800f8; + GSVector4i ga = m_env.fm & 0x8000f800; + + m_env.fm = (ga >> 16) | (rb >> 9) | (ga >> 6) | (rb >> 3) | GSVector4i::xffff0000(); + } + + if(m_env.sel.zpsm == 1) + { + m_env.zm |= GSVector4i::xff000000(); + } + else if(m_env.sel.zpsm == 2) + { + m_env.zm |= GSVector4i::xffff0000(); + } + + if(m_env.sel.atst == ATST_LESS) + { + m_env.sel.atst = ATST_LEQUAL; + + m_env.aref -= GSVector4i::x00000001(); + } + else if(m_env.sel.atst == ATST_GREATER) + { + m_env.sel.atst = ATST_GEQUAL; + + m_env.aref += GSVector4i::x00000001(); + } + + if(m_env.sel.tfx != TFX_NONE) + { + m_env.tex = p->tex; + m_env.clut = p->clut; + m_env.tw = p->tw; + + unsigned short tw = (unsigned short)(1 << context->TEX0.TW); + unsigned short th = (unsigned short)(1 << context->TEX0.TH); + + switch(context->CLAMP.WMS) + { + case CLAMP_REPEAT: + m_env.t.min.u16[0] = tw - 1; + m_env.t.max.u16[0] = 0; + m_env.t.mask.u32[0] = 0xffffffff; + break; + case CLAMP_CLAMP: + m_env.t.min.u16[0] = 0; + m_env.t.max.u16[0] = tw - 1; + m_env.t.mask.u32[0] = 0; + break; + case CLAMP_REGION_CLAMP: + m_env.t.min.u16[0] = min(context->CLAMP.MINU, tw - 1); + m_env.t.max.u16[0] = min(context->CLAMP.MAXU, tw - 1); + m_env.t.mask.u32[0] = 0; + break; + case CLAMP_REGION_REPEAT: + m_env.t.min.u16[0] = context->CLAMP.MINU; + m_env.t.max.u16[0] = context->CLAMP.MAXU; + m_env.t.mask.u32[0] = 0xffffffff; + break; + default: + __assume(0); + } + + switch(context->CLAMP.WMT) + { + case CLAMP_REPEAT: + m_env.t.min.u16[4] = th - 1; + m_env.t.max.u16[4] = 0; + m_env.t.mask.u32[2] = 0xffffffff; + break; + case CLAMP_CLAMP: + m_env.t.min.u16[4] = 0; + m_env.t.max.u16[4] = th - 1; + m_env.t.mask.u32[2] = 0; + break; + case CLAMP_REGION_CLAMP: + m_env.t.min.u16[4] = min(context->CLAMP.MINV, th - 1); + m_env.t.max.u16[4] = min(context->CLAMP.MAXV, th - 1); // ffx anima summon scene, when the anchor appears (th = 256, maxv > 256) + m_env.t.mask.u32[2] = 0; + break; + case CLAMP_REGION_REPEAT: + m_env.t.min.u16[4] = context->CLAMP.MINV; + m_env.t.max.u16[4] = context->CLAMP.MAXV; + m_env.t.mask.u32[2] = 0xffffffff; + break; + default: + __assume(0); + } + + m_env.t.min = m_env.t.min.xxxxlh(); + m_env.t.max = m_env.t.max.xxxxlh(); + m_env.t.mask = m_env.t.mask.xxzz(); + } + + // + + f->sl = m_ds.Lookup(m_env.sel); + + // + + if(m_env.sel.IsSolidRect()) + { + f->sr = (DrawSolidRectPtr)&GSDrawScanline::DrawSolidRect; + } + + // + + DWORD sel = 0; + + if(data->primclass != GS_POINT_CLASS) + { + sel |= (m_env.sel.ztst ? 1 : 0) << 0; + sel |= m_env.sel.fge << 1; + sel |= (m_env.sel.tfx != TFX_NONE ? 1 : 0) << 2; + sel |= m_env.sel.fst << 3; + sel |= m_env.sel.iip << 4; + } + + f->sp = m_sp.Lookup(sel); +} + +void GSDrawScanline::EndDraw(const GSRasterizerStats& stats) +{ + m_ds.UpdateStats(stats, m_state->m_perfmon.GetFrame()); +} + +template +void GSDrawScanline::SetupPrim(const GSVertexSW* vertices, const GSVertexSW& dscan) +{ + // p + + GSVector4 p = dscan.p; + + GSVector4 dz = p.zzzz(); + GSVector4 df = p.wwww(); + + if(zbe) + { + m_env.d4.z = dz * 4.0f; + } + + if(fge) + { + m_env.d4.f = GSVector4i(df * 4.0f).xxzzlh(); + } + + for(int i = 0; i < 4; i++) + { + GSVector4 v = m_shift[i]; + + if(zbe) + { + m_env.d[i].z = dz * v; + } + + if(fge) + { + m_env.d[i].f = GSVector4i(df * v).xxzzlh(); + } + } + + if(iip == 0) // should be sprite == 1, but close enough + { + GSVector4 p = vertices[0].p; + + if(zbe) + { + GSVector4 z = p.zzzz(); + + m_env.p.z = (GSVector4i(z * 0.5f) << 1) | (GSVector4i(z) & GSVector4i::x00000001()); + } + + if(fge) + { + m_env.p.f = GSVector4i(p).zzzzh().zzzz(); + } + } + + // t + + if(tme) + { + GSVector4 t = dscan.t; + + if(fst) + { + m_env.d4.st = GSVector4i(t * 4.0f); + + GSVector4 ds = t.xxxx(); + GSVector4 dt = t.yyyy(); + + for(int i = 0; i < 4; i++) + { + GSVector4 v = m_shift[i]; + + m_env.d[i].si = GSVector4i(ds * v); + m_env.d[i].ti = GSVector4i(dt * v); + } + } + else + { + m_env.d4.stq = t * 4.0f; + + GSVector4 ds = t.xxxx(); + GSVector4 dt = t.yyyy(); + GSVector4 dq = t.zzzz(); + + for(int i = 0; i < 4; i++) + { + GSVector4 v = m_shift[i]; + + m_env.d[i].s = ds * v; + m_env.d[i].t = dt * v; + m_env.d[i].q = dq * v; + } + } + } + + // c + + if(iip) + { + GSVector4 c = dscan.c; + + m_env.d4.c = GSVector4i(c * 4.0f).xzyw().ps32(); + + GSVector4 dr = c.xxxx(); + GSVector4 dg = c.yyyy(); + GSVector4 db = c.zzzz(); + GSVector4 da = c.wwww(); + + for(int i = 0; i < 4; i++) + { + GSVector4 v = m_shift[i]; + + GSVector4i rg = GSVector4i(dr * v).ps32(GSVector4i(dg * v)); + GSVector4i ba = GSVector4i(db * v).ps32(GSVector4i(da * v)); + + m_env.d[i].rb = rg.upl16(ba); + m_env.d[i].ga = rg.uph16(ba); + } + } + else + { + GSVector4i rgba = GSVector4i(vertices[0].c); + + GSVector4i rbga = rgba.upl16(rgba.zwxy()); + + if(tme == 0) + { + rbga = rbga.srl16(7); + + DWORD abe = m_env.sel.abe & 0x3f; // a, b, c + + DWORD abea = m_env.sel.abea; + DWORD abeb = m_env.sel.abeb; + DWORD abec = m_env.sel.abec; + DWORD abed = m_env.sel.abed; + + if(fge == 0 && abe != 0x3f && !(abe & 0x15) && abea != abeb) // 0x15 = 010101b => a, b, c != 1 + { + GSVector4i c[4]; + + c[0] = rbga; + c[1] = rgba.zzzzh().zzzz(); + c[2] = GSVector4i::zero(); + c[3] = m_env.afix2; + + GSVector4i cc = GSVector4i::lerp16<1>(c[abea], c[abeb], c[abec + 1]); + + if(abed == 0) + { + cc = cc.add16(c[0]); + } + + m_env.c2.rb = cc.xxxx(); + m_env.c2.ga = cc.zzzz().mix16(c[1].srl16(7)); + } + } + + m_env.c.rb = rbga.xxxx(); + m_env.c.ga = rbga.zzzz(); + } +} + +GSVector4i GSDrawScanline::Wrap(const GSVector4i& t) +{ + GSVector4i clamp = t.sat_i16(m_env.t.min, m_env.t.max); + GSVector4i repeat = (t & m_env.t.min) | m_env.t.max; + + return clamp.blend8(repeat, m_env.t.mask); +} + +void GSDrawScanline::SampleTexture(DWORD ltf, DWORD tlu, const GSVector4i& u, const GSVector4i& v, GSVector4i* c) +{ + const void* RESTRICT tex = m_env.tex; + const DWORD* RESTRICT clut = m_env.clut; + const DWORD tw = m_env.tw; + + GSVector4i uv = u.sra32(16).ps32(v.sra32(16)); + + GSVector4i c00, c01, c10, c11; + + if(ltf) + { + GSVector4i uf = u.xxzzlh().srl16(1); + GSVector4i vf = v.xxzzlh().srl16(1); + + GSVector4i uv0 = Wrap(uv); + GSVector4i uv1 = Wrap(uv.add16(GSVector4i::x0001())); + + GSVector4i y0 = uv0.uph16() << tw; + GSVector4i y1 = uv1.uph16() << tw; + GSVector4i x0 = uv0.upl16(); + GSVector4i x1 = uv1.upl16(); + + GSVector4i addr00 = y0 + x0; + GSVector4i addr01 = y0 + x1; + GSVector4i addr10 = y1 + x0; + GSVector4i addr11 = y1 + x1; + + if(tlu) + { + c00 = addr00.gather32_32((const BYTE*)tex, clut); + c01 = addr01.gather32_32((const BYTE*)tex, clut); + c10 = addr10.gather32_32((const BYTE*)tex, clut); + c11 = addr11.gather32_32((const BYTE*)tex, clut); + } + else + { + c00 = addr00.gather32_32((const DWORD*)tex); + c01 = addr01.gather32_32((const DWORD*)tex); + c10 = addr10.gather32_32((const DWORD*)tex); + c11 = addr11.gather32_32((const DWORD*)tex); + } + + GSVector4i mask = GSVector4i::x00ff(); + + GSVector4i rb00 = c00 & mask; + GSVector4i rb01 = c01 & mask; + GSVector4i rb10 = c10 & mask; + GSVector4i rb11 = c11 & mask; + + rb00 = rb00.lerp16<0>(rb01, uf); + rb10 = rb10.lerp16<0>(rb11, uf); + rb00 = rb00.lerp16<0>(rb10, vf); + + c[0] = rb00; + + GSVector4i ga00 = (c00 >> 8) & mask; + GSVector4i ga01 = (c01 >> 8) & mask; + GSVector4i ga10 = (c10 >> 8) & mask; + GSVector4i ga11 = (c11 >> 8) & mask; + + ga00 = ga00.lerp16<0>(ga01, uf); + ga10 = ga10.lerp16<0>(ga11, uf); + ga00 = ga00.lerp16<0>(ga10, vf); + + c[1] = ga00; + } + else + { + GSVector4i uv0 = Wrap(uv); + + GSVector4i addr00 = (uv0.uph16() << tw) + uv0.upl16(); + + if(tlu) + { + c00 = addr00.gather32_32((const BYTE*)tex, clut); + } + else + { + c00 = addr00.gather32_32((const DWORD*)tex); + } + + GSVector4i mask = GSVector4i::x00ff(); + + c[0] = c00 & mask; + c[1] = (c00 >> 8) & mask; + } +} + +void GSDrawScanline::ColorTFX(DWORD iip, DWORD tfx, const GSVector4i& rbf, const GSVector4i& gaf, GSVector4i& rbt, GSVector4i& gat) +{ + GSVector4i rb = iip ? rbf : m_env.c.rb; + GSVector4i ga = iip ? gaf : m_env.c.ga; + + GSVector4i af; + + switch(tfx) + { + case TFX_MODULATE: + rbt = rbt.modulate16<1>(rb).clamp8(); + break; + case TFX_DECAL: + break; + case TFX_HIGHLIGHT: + case TFX_HIGHLIGHT2: + af = ga.yywwlh().srl16(7); + rbt = rbt.modulate16<1>(rb).add16(af).clamp8(); + gat = gat.modulate16<1>(ga).add16(af).clamp8().mix16(gat); + break; + case TFX_NONE: + rbt = iip ? rb.srl16(7) : rb; + break; + default: + __assume(0); + } +} + +void GSDrawScanline::AlphaTFX(DWORD iip, DWORD tfx, DWORD tcc, const GSVector4i& gaf, GSVector4i& gat) +{ + GSVector4i ga = iip ? gaf : m_env.c.ga; + + switch(tfx) + { + case TFX_MODULATE: + gat = gat.modulate16<1>(ga).clamp8(); // mul16hrs rounds and breaks fogging in resident evil 4 (only modulate16<0> uses mul16hrs, but watch out) + if(!tcc) gat = gat.mix16(ga.srl16(7)); + break; + case TFX_DECAL: + break; + case TFX_HIGHLIGHT: + gat = gat.mix16(!tcc ? ga.srl16(7) : gat.addus8(ga.srl16(7))); + break; + case TFX_HIGHLIGHT2: + if(!tcc) gat = gat.mix16(ga.srl16(7)); + break; + case TFX_NONE: + gat = iip ? ga.srl16(7) : ga; + break; + default: + __assume(0); + } +} + +void GSDrawScanline::Fog(DWORD fge, const GSVector4i& f, GSVector4i& rb, GSVector4i& ga) +{ + if(fge) + { + rb = m_env.frb.lerp16<0>(rb, f); + ga = m_env.fga.lerp16<0>(ga, f).mix16(ga); + } +} + +bool GSDrawScanline::TestZ(DWORD zpsm, DWORD ztst, const GSVector4i& zs, const GSVector4i& zd, GSVector4i& test) +{ + if(ztst > 1) + { + GSVector4i o = GSVector4i::x80000000(zs); + + GSVector4i zso = zs - o; + GSVector4i zdo; + + switch(zpsm) + { + case 0: zdo = zd - o; break; + case 1: zdo = (zd & GSVector4i::x00ffffff(zs)) - o; break; + case 2: zdo = (zd & GSVector4i::x0000ffff(zs)) - o; break; + } + + switch(ztst) + { + case ZTST_GEQUAL: test |= zso < zdo; break; + case ZTST_GREATER: test |= zso <= zdo; break; + default: __assume(0); + } + + if(test.alltrue()) + { + return false; + } + } + + return true; +} + +bool GSDrawScanline::TestAlpha(DWORD atst, DWORD afail, const GSVector4i& ga, GSVector4i& fm, GSVector4i& zm, GSVector4i& test) +{ + if(atst != ATST_ALWAYS) + { + GSVector4i t; + + switch(atst) + { + case ATST_NEVER: t = GSVector4i::xffffffff(); break; + case ATST_ALWAYS: t = GSVector4i::zero(); break; + case ATST_LESS: + case ATST_LEQUAL: t = (ga >> 16) > m_env.aref; break; + case ATST_EQUAL: t = (ga >> 16) != m_env.aref; break; + case ATST_GEQUAL: + case ATST_GREATER: t = (ga >> 16) < m_env.aref; break; + case ATST_NOTEQUAL: t = (ga >> 16) == m_env.aref; break; + default: __assume(0); + } + + switch(afail) + { + case AFAIL_KEEP: + test |= t; + if(test.alltrue()) return false; + break; + case AFAIL_FB_ONLY: + zm |= t; + break; + case AFAIL_ZB_ONLY: + fm |= t; + break; + case AFAIL_RGB_ONLY: + fm |= t & GSVector4i::xff000000(); + zm |= t; + break; + default: + __assume(0); + } + } + + return true; +} + +bool GSDrawScanline::TestDestAlpha(DWORD fpsm, DWORD date, const GSVector4i& fd, GSVector4i& test) +{ + if(date) + { + switch(fpsm) + { + case 0: + test |= (fd ^ m_env.datm).sra32(31); + if(test.alltrue()) return false; + case 1: + break; + case 2: + test |= ((fd << 16) ^ m_env.datm).sra32(31); + if(test.alltrue()) return false; + case 3: + break; + default: + __assume(0); + } + } + + return true; +} + +void GSDrawScanline::ReadPixel(int psm, int addr, GSVector4i& c) const +{ + WORD* vm16 = (WORD*)m_env.vm; + + if(psm != 3) + { + c = GSVector4i::load(&vm16[addr], &vm16[addr + 8]); + } +} + +void GSDrawScanline::WritePixel(int psm, WORD* RESTRICT vm16, DWORD c) +{ + DWORD* RESTRICT vm32 = (DWORD*)vm16; + + switch(psm) + { + case 0: *vm32 = c; break; + case 1: *vm32 = (*vm32 & 0xff000000) | (c & 0x00ffffff); break; + case 2: *vm16 = (WORD)c; break; + } +} + +void GSDrawScanline::WriteFrame(int fpsm, int rfb, GSVector4i* c, const GSVector4i& fd, const GSVector4i& fm, int addr, int fzm) +{ + WORD* RESTRICT vm16 = (WORD*)m_env.vm; + + c[0] &= m_env.colclamp; + c[1] &= m_env.colclamp; + + GSVector4i fs = c[0].upl16(c[1]).pu16(c[0].uph16(c[1])); + + if(fpsm != 1) + { + fs |= m_env.fba; + } + + if(fpsm == 2) + { + GSVector4i rb = fs & 0x00f800f8; + GSVector4i ga = fs & 0x8000f800; + + fs = (ga >> 16) | (rb >> 9) | (ga >> 6) | (rb >> 3); + } + + if(rfb) + { + fs = fs.blend(fd, fm); + + if(fpsm < 2) + { + if(fzm & 0x03) GSVector4i::storel(&vm16[addr + 0], fs); + if(fzm & 0x0c) GSVector4i::storeh(&vm16[addr + 8], fs); + + return; + } + } + + if(fzm & 0x01) WritePixel(fpsm, &vm16[addr + 0], fs.extract32<0>()); + if(fzm & 0x02) WritePixel(fpsm, &vm16[addr + 2], fs.extract32<1>()); + if(fzm & 0x04) WritePixel(fpsm, &vm16[addr + 8], fs.extract32<2>()); + if(fzm & 0x08) WritePixel(fpsm, &vm16[addr + 10], fs.extract32<3>()); +} + +void GSDrawScanline::WriteZBuf(int zpsm, int ztst, const GSVector4i& z, const GSVector4i& zd, const GSVector4i& zm, int addr, int fzm) +{ + if(ztst == 0) return; + + WORD* RESTRICT vm16 = (WORD*)m_env.vm; + + GSVector4i zs = z; + + if(ztst > 1) + { + if(zpsm < 2) + { + zs = zs.blend8(zd, zm); + + if(fzm & 0x30) GSVector4i::storel(&vm16[addr + 0], zs); + if(fzm & 0xc0) GSVector4i::storeh(&vm16[addr + 8], zs); + + return; + } + } + + if(fzm & 0x10) WritePixel(zpsm, &vm16[addr + 0], zs.extract32<0>()); + if(fzm & 0x20) WritePixel(zpsm, &vm16[addr + 2], zs.extract32<1>()); + if(fzm & 0x40) WritePixel(zpsm, &vm16[addr + 8], zs.extract32<2>()); + if(fzm & 0x80) WritePixel(zpsm, &vm16[addr + 10], zs.extract32<3>()); +} + +template +void GSDrawScanline::DrawScanline(int top, int left, int right, const GSVertexSW& v) +{ + int skip = left & 3; + + left -= skip; + + int steps = right - left - 4; + + GSVector4i test = m_test[skip] | m_test[7 + (steps & (steps >> 31))]; + + // + + GSVector2i fza_base; + GSVector2i* fza_offset; + + GSVector4 z, s, t, q; + GSVector4i si, ti, f, rb, ga; + + // fza + + fza_base = m_env.fzbr[top]; + fza_offset = &m_env.fzbc[left >> 2]; + + // v.p + + GSVector4 vp = v.p; + + z = vp.zzzz() + m_env.d[skip].z; + f = GSVector4i(vp).zzzzh().zzzz().add16(m_env.d[skip].f); + + // v.t + + GSVector4 vt = v.t; + + if(m_env.sel.fst) + { + GSVector4i vti(vt); + + si = vti.xxxx() + m_env.d[skip].si; + ti = vti.yyyy() + m_env.d[skip].ti; + } + else + { + s = vt.xxxx() + m_env.d[skip].s; + t = vt.yyyy() + m_env.d[skip].t; + q = vt.zzzz() + m_env.d[skip].q; + } + + // v.c + + if(iip) + { + GSVector4i vc = GSVector4i(v.c); + + vc = vc.upl16(vc.zwxy()); + + rb = vc.xxxx().add16(m_env.d[skip].rb); + ga = vc.zzzz().add16(m_env.d[skip].ga); + } + + // + + while(1) + { + do + { + int fa = fza_base.x + fza_offset->x; + int za = fza_base.y + fza_offset->y; + + GSVector4i zs = (GSVector4i(z * 0.5f) << 1) | (GSVector4i(z) & GSVector4i::x00000001()); + GSVector4i zd; + + if(ztst > 1) + { + ReadPixel(zpsm, za, zd); + + if(!TestZ(zpsm, ztst, zs, zd, test)) + { + continue; + } + } + + GSVector4i c[6]; + + if(m_env.sel.tfx != TFX_NONE) + { + GSVector4i u, v; + + if(m_env.sel.fst) + { + u = si; + v = ti; + } + else + { + GSVector4 w = q.rcp(); + + u = GSVector4i(s * w); + v = GSVector4i(t * w); + + if(m_env.sel.ltf) + { + u -= 0x8000; + v -= 0x8000; + } + } + + SampleTexture(m_env.sel.ltf, m_env.sel.tlu, u, v, c); + } + + AlphaTFX(iip, m_env.sel.tfx, m_env.sel.tcc, ga, c[1]); + + GSVector4i fm = m_env.fm; + GSVector4i zm = m_env.zm; + + if(!TestAlpha(m_env.sel.atst, m_env.sel.afail, c[1], fm, zm, test)) + { + continue; + } + + ColorTFX(iip, m_env.sel.tfx, rb, ga, c[0], c[1]); + + Fog(m_env.sel.fge, f, c[0], c[1]); + + GSVector4i fd; + + if(m_env.sel.rfb) + { + ReadPixel(fpsm, fa, fd); + + if(!TestDestAlpha(fpsm, m_env.sel.date, fd, test)) + { + continue; + } + } + + fm |= test; + zm |= test; + + int fzm = ~(fm == GSVector4i::xffffffff()).ps32(zm == GSVector4i::xffffffff()).ps32().mask(); + + WriteZBuf(zpsm, ztst, zs, zd, zm, za, fzm); + + if(m_env.sel.abe != 255) + { + GSVector4i mask = GSVector4i::x00ff(fd); + + switch(fpsm) + { + case 0: + c[2] = fd & mask; + c[3] = (fd >> 8) & mask; + break; + case 1: + c[2] = fd & mask; + c[3] = (fd >> 8) & mask; + c[3] = c[3].mix16(GSVector4i(0x00800000)); + break; + case 2: + c[2] = ((fd & 0x7c00) << 9) | ((fd & 0x001f) << 3); + c[3] = ((fd & 0x8000) << 8) | ((fd & 0x03e0) >> 2); + break; + } + + c[4] = GSVector4::zero(); + c[5] = m_env.afix; + + DWORD abea = m_env.sel.abea; + DWORD abeb = m_env.sel.abeb; + DWORD abec = m_env.sel.abec; + DWORD abed = m_env.sel.abed; + + GSVector4i a = c[abec * 2 + 1].yywwlh().sll16(7); + + GSVector4i rb = GSVector4i::lerp16<1>(c[abea * 2 + 0], c[abeb * 2 + 0], a, c[abed * 2 + 0]); + GSVector4i ga = GSVector4i::lerp16<1>(c[abea * 2 + 1], c[abeb * 2 + 1], a, c[abed * 2 + 1]); + + if(m_env.sel.pabe) + { + mask = (c[1] << 8).sra32(31); + + rb = c[0].blend8(rb, mask); + ga = c[1].blend8(ga, mask); + } + + c[0] = rb; + c[1] = ga.mix16(c[1]); + } + + WriteFrame(fpsm, m_env.sel.rfb, c, fd, fm, fa, fzm); + } + while(0); + + if(steps <= 0) break; + + steps -= 4; + + test = m_test[7 + (steps & (steps >> 31))]; + + fza_offset++; + + z += m_env.d4.z; + f = f.add16(m_env.d4.f); + + if(m_env.sel.fst) + { + GSVector4i st = m_env.d4.st; + + si += st.xxxx(); + ti += st.yyyy(); + } + else + { + GSVector4 stq = m_env.d4.stq; + + s += stq.xxxx(); + t += stq.yyyy(); + q += stq.zzzz(); + } + + if(iip) + { + GSVector4i c = m_env.d4.c; + + rb = rb.add16(c.xxxx()); + ga = ga.add16(c.yyyy()); + } + } +} + +template +void GSDrawScanline::DrawScanlineEx(int top, int left, int right, const GSVertexSW& v) +{ + const DWORD fpsm = (sel >> 0) & 3; + const DWORD zpsm = (sel >> 2) & 3; + const DWORD ztst = (sel >> 4) & 3; + const DWORD atst = (sel >> 6) & 7; + const DWORD afail = (sel >> 9) & 3; + const DWORD iip = (sel >> 11) & 1; + const DWORD tfx = (sel >> 12) & 7; + const DWORD tcc = (sel >> 15) & 1; + const DWORD fst = (sel >> 16) & 1; + const DWORD ltf = (sel >> 17) & 1; + const DWORD tlu = (sel >> 18) & 1; + const DWORD fge = (sel >> 19) & 1; + const DWORD date = (sel >> 20) & 1; + const DWORD abe = (sel >> 21) & 255; + const DWORD abea = (sel >> 21) & 3; + const DWORD abeb = (sel >> 23) & 3; + const DWORD abec = (sel >> 25) & 3; + const DWORD abed = (sel >> 27) & 3; + const DWORD pabe = (sel >> 29) & 1; + const DWORD rfb = (sel >> 30) & 1; + const DWORD sprite = (sel >> 31) & 1; + + // + + int skip = left & 3; + + left -= skip; + + int steps = right - left - 4; + + GSVector4i test = m_test[skip] | m_test[7 + (steps & (steps >> 31))]; + + // + + GSVector2i fza_base; + GSVector2i* fza_offset; + + GSVector4 z, s, t, q; + GSVector4i zi, si, ti, f, rb, ga; + + // fza + + fza_base = m_env.fzbr[top]; + fza_offset = &m_env.fzbc[left >> 2]; + + // v.p + + GSVector4 vp = v.p; + + if(sprite) + { + zi = m_env.p.z; + f = m_env.p.f; + } + else + { + z = vp.zzzz() + m_env.d[skip].z; + f = GSVector4i(vp).zzzzh().zzzz().add16(m_env.d[skip].f); + } + + // v.t + + GSVector4 vt = v.t; + + if(fst) + { + GSVector4i vti(vt); + + si = vti.xxxx(); + ti = vti.yyyy(); + + si += m_env.d[skip].si; + if(!sprite) ti += m_env.d[skip].ti; + } + else + { + s = vt.xxxx() + m_env.d[skip].s; + t = vt.yyyy() + m_env.d[skip].t; + q = vt.zzzz() + m_env.d[skip].q; + } + + // v.c + + if(iip) + { + GSVector4i vc = GSVector4i(v.c); + + vc = vc.upl16(vc.zwxy()); + + rb = vc.xxxx().add16(m_env.d[skip].rb); + ga = vc.zzzz().add16(m_env.d[skip].ga); + } + + // + + while(1) + { + do + { + int fa = fza_base.x + fza_offset->x; + int za = fza_base.y + fza_offset->y; + + GSVector4i zs = sprite ? zi : (GSVector4i(z * 0.5f) << 1) | (GSVector4i(z) & GSVector4i::x00000001()); + GSVector4i zd; + + if(ztst > 1) + { + ReadPixel(zpsm, za, zd); + + if(!TestZ(zpsm, ztst, zs, zd, test)) + { + continue; + } + } + + GSVector4i c[6]; + + if(tfx != TFX_NONE) + { + GSVector4i u, v; + + if(fst) + { + u = si; + v = ti; + } + else + { + GSVector4 w = q.rcp(); + + u = GSVector4i(s * w); + v = GSVector4i(t * w); + + if(ltf) + { + u -= 0x8000; + v -= 0x8000; + } + } + + SampleTexture(ltf, tlu, u, v, c); + } + + AlphaTFX(iip, tfx, tcc, ga, c[1]); + + GSVector4i fm = m_env.fm; + GSVector4i zm = m_env.zm; + + if(!TestAlpha(atst, afail, c[1], fm, zm, test)) + { + continue; + } + + ColorTFX(iip, tfx, rb, ga, c[0], c[1]); + + Fog(fge, f, c[0], c[1]); + + GSVector4i fd; + + if(rfb) + { + ReadPixel(fpsm, fa, fd); + + if(!TestDestAlpha(fpsm, date, fd, test)) + { + continue; + } + } + + fm |= test; + zm |= test; + + int fzm = ~(fm == GSVector4i::xffffffff()).ps32(zm == GSVector4i::xffffffff()).ps32().mask(); + + WriteZBuf(zpsm, ztst, zs, zd, zm, za, fzm); + + if(abe != 255) + { + GSVector4i mask = GSVector4i::x00ff(fd); + + switch(fpsm) + { + case 0: + case 1: + c[2] = fd & mask; + c[3] = (fd >> 8) & mask; + break; + case 2: + c[2] = ((fd & 0x7c00) << 9) | ((fd & 0x001f) << 3); + c[3] = ((fd & 0x8000) << 8) | ((fd & 0x03e0) >> 2); + break; + } + + c[4] = GSVector4::zero(); + c[5] = GSVector4::zero(); + + GSVector4i rb, ga; + + if(tfx == TFX_NONE && fge == 0 && abea != 1 && abeb != 1 && abec != 1 && abea != abeb) + { + c[0] = m_env.c2.rb; + c[1] = m_env.c2.ga; + + rb = c[0]; + ga = c[1]; + + if(abed == 1) + { + rb = rb.add16(c[2]); + ga = ga.add16(c[3]); + } + } + else + { + if(abea != abeb) + { + rb = c[abea * 2 + 0]; + ga = c[abea * 2 + 1]; + + if(abeb < 2) + { + rb = rb.sub16(c[abeb * 2 + 0]); + ga = ga.sub16(c[abeb * 2 + 1]); + } + + if(!(fpsm == 1 && abec == 1)) + { + GSVector4i a = abec < 2 ? c[abec * 2 + 1].yywwlh().sll16(7) : m_env.afix2; + + rb = rb.modulate16<1>(a); + ga = ga.modulate16<1>(a); + } + + if(abed < 2) + { + rb = rb.add16(c[abed * 2 + 0]); + ga = ga.add16(c[abed * 2 + 1]); + } + } + else + { + rb = c[abed * 2 + 0]; + ga = c[abed * 2 + 1]; + } + } + + if(pabe) + { + mask = (c[1] << 8).sra32(31); + + rb = c[0].blend8(rb, mask); + ga = c[1].blend8(ga, mask); + } + + c[0] = rb; + c[1] = ga.mix16(c[1]); + } + + WriteFrame(fpsm, rfb, c, fd, fm, fa, fzm); + } + while(0); + + if(steps <= 0) break; + + steps -= 4; + + test = m_test[7 + (steps & (steps >> 31))]; + + fza_offset++; + + if(!sprite) + { + z += m_env.d4.z; + f = f.add16(m_env.d4.f); + } + + if(fst) + { + GSVector4i st = m_env.d4.st; + + si += st.xxxx(); + if(!sprite) ti += st.yyyy(); + } + else + { + GSVector4 stq = m_env.d4.stq; + + s += stq.xxxx(); + t += stq.yyyy(); + q += stq.zzzz(); + } + + if(iip) + { + GSVector4i c = m_env.d4.c; + + rb = rb.add16(c.xxxx()); + ga = ga.add16(c.yyyy()); + } + } +} + +void GSDrawScanline::DrawSolidRect(const GSVector4i& r, const GSVertexSW& v) +{ +/* +static FILE* s_fp = NULL; +if(!s_fp) s_fp = fopen("c:\\log2.txt", "w"); +__int64 start = __rdtsc(); +int size = (r.z - r.x) * (r.w - r.y); +*/ + ASSERT(r.y >= 0); + ASSERT(r.w >= 0); + + // FIXME: sometimes the frame and z buffer may overlap, the outcome is undefined + + DWORD m; + + m = m_env.zm.u32[0]; + + if(m != 0xffffffff) + { + DWORD z = (DWORD)(float)v.p.z; + + if(m_env.sel.zpsm != 2) + { + if(m == 0) + { + DrawSolidRectT(m_env.zbr, m_env.zbc[0], r, z, m); + } + else + { + DrawSolidRectT(m_env.zbr, m_env.zbc[0], r, z, m); + } + } + else + { + if(m == 0) + { + DrawSolidRectT(m_env.zbr, m_env.zbc[0], r, z, m); + } + else + { + DrawSolidRectT(m_env.zbr, m_env.zbc[0], r, z, m); + } + } + } + + m = m_env.fm.u32[0]; + + if(m != 0xffffffff) + { + DWORD c = (GSVector4i(v.c) >> 7).rgba32(); + + if(m_state->m_context->FBA.FBA) + { + c |= 0x80000000; + } + + if(m_env.sel.fpsm != 2) + { + if(m == 0) + { + DrawSolidRectT(m_env.fbr, m_env.fbc[0], r, c, m); + } + else + { + DrawSolidRectT(m_env.fbr, m_env.fbc[0], r, c, m); + } + } + else + { + c = ((c & 0xf8) >> 3) | ((c & 0xf800) >> 6) | ((c & 0xf80000) >> 9) | ((c & 0x80000000) >> 16); + + if(m == 0) + { + DrawSolidRectT(m_env.fbr, m_env.fbc[0], r, c, m); + } + else + { + DrawSolidRectT(m_env.fbr, m_env.fbc[0], r, c, m); + } + } + } +/* +__int64 stop = __rdtsc(); +fprintf(s_fp, "%I64d => %I64d = %I64d (%d,%d - %d,%d) %d\n", start, stop, stop - start, r.x, r.y, r.z, r.w, size); +*/ +} + +template +void GSDrawScanline::DrawSolidRectT(const GSVector4i* row, int* col, const GSVector4i& r, DWORD c, DWORD m) +{ + if(m == 0xffffffff) return; + + GSVector4i color((int)c); + GSVector4i mask((int)m); + + if(sizeof(T) == sizeof(WORD)) + { + color = color.xxzzlh(); + mask = mask.xxzzlh(); + } + + color = color.andnot(mask); + + GSVector4i bm(8 * 4 / sizeof(T) - 1, 8 - 1); + GSVector4i br = (r + bm).andnot(bm.xyxy()); + + FillRect(row, col, GSVector4i(r.x, r.y, r.z, br.y), c, m); + FillRect(row, col, GSVector4i(r.x, br.w, r.z, r.w), c, m); + + if(r.x < br.x || br.z < r.z) + { + FillRect(row, col, GSVector4i(r.x, br.y, br.x, br.w), c, m); + FillRect(row, col, GSVector4i(br.z, br.y, r.z, br.w), c, m); + } + + FillBlock(row, col, br, color, mask); +} + +template +void GSDrawScanline::FillRect(const GSVector4i* row, int* col, const GSVector4i& r, DWORD c, DWORD m) +{ + if(r.x >= r.z) return; + + for(int y = r.y; y < r.w; y++) + { + DWORD base = row[y].x; + + for(int x = r.x; x < r.z; x++) + { + T* p = &((T*)m_env.vm)[base + col[x]]; + + *p = (T)(!masked ? c : (c | (*p & m))); + } + } +} + +template +void GSDrawScanline::FillBlock(const GSVector4i* row, int* col, const GSVector4i& r, const GSVector4i& c, const GSVector4i& m) +{ + if(r.x >= r.z) return; + + for(int y = r.y; y < r.w; y += 8) + { + DWORD base = row[y].x; + + for(int x = r.x; x < r.z; x += 8 * 4 / sizeof(T)) + { + GSVector4i* p = (GSVector4i*)&((T*)m_env.vm)[base + col[x]]; + + for(int i = 0; i < 16; i += 4) + { + p[i + 0] = !masked ? c : (c | (p[i + 0] & m)); + p[i + 1] = !masked ? c : (c | (p[i + 1] & m)); + p[i + 2] = !masked ? c : (c | (p[i + 2] & m)); + p[i + 3] = !masked ? c : (c | (p[i + 3] & m)); + } + } + } +} + +// + +GSDrawScanline::GSDrawScanlineMap::GSDrawScanlineMap() +{ + // w00t :P + + #define InitDS_IIP(fpsm, zpsm, ztst, iip) \ + m_default[fpsm][zpsm][ztst][iip] = (DrawScanlinePtr)&GSDrawScanline::DrawScanline; \ + + #define InitDS_ZTST(fpsm, zpsm, ztst) \ + InitDS_IIP(fpsm, zpsm, ztst, 0) \ + InitDS_IIP(fpsm, zpsm, ztst, 1) \ + + #define InitDS(fpsm, zpsm) \ + InitDS_ZTST(fpsm, zpsm, 0) \ + InitDS_ZTST(fpsm, zpsm, 1) \ + InitDS_ZTST(fpsm, zpsm, 2) \ + InitDS_ZTST(fpsm, zpsm, 3) \ + + InitDS(0, 0); + InitDS(0, 1); + InitDS(0, 2); + InitDS(0, 3); + InitDS(1, 0); + InitDS(1, 1); + InitDS(1, 2); + InitDS(1, 3); + InitDS(2, 0); + InitDS(2, 1); + InitDS(2, 2); + InitDS(2, 3); + InitDS(3, 0); + InitDS(3, 1); + InitDS(3, 2); + + #define InitDS_Sel(sel) \ + SetAt(sel, (DrawScanlinePtr)&GSDrawScanline::DrawScanlineEx<##sel##>); + + #ifdef FAST_DRAWSCANLINE + + // bios + + InitDS_Sel(0x1fe04850); // 8.99% + InitDS_Sel(0x1fe28870); // 26.46% + InitDS_Sel(0x1fe38050); // 12.95% + InitDS_Sel(0x1fe38060); // 8.59% + InitDS_Sel(0x48428050); // 8.86% + InitDS_Sel(0x48428060); // 6.30% + InitDS_Sel(0x48804860); // 28.07% + InitDS_Sel(0x49028060); // 6.31% + InitDS_Sel(0x4902904c); // 5.46% + InitDS_Sel(0x4b02804c); // 5.11% + InitDS_Sel(0x4c804050); // 9.08% + InitDS_Sel(0x4d038864); // 114.89% + InitDS_Sel(0x9fe39064); // 14.72% + InitDS_Sel(0xc8804050); // 8.58% + InitDS_Sel(0xc9004050); // 8.69% + InitDS_Sel(0xc9039050); // 17.17% + InitDS_Sel(0xcc804050); // 8.16% + InitDS_Sel(0xcd019050); // 85.40% + + // ffx + + InitDS_Sel(0x11020865); // 9.81% + InitDS_Sel(0x1fe68875); // 9.90% + InitDS_Sel(0x1fe69075); // 15.13% + InitDS_Sel(0x1fe84075); // 5.16% + InitDS_Sel(0x1fee8075); // 5.98% + InitDS_Sel(0x1fee8875); // 43.94% + InitDS_Sel(0x1fee8876); // 22.75% + InitDS_Sel(0x1fee8975); // 5.70% + InitDS_Sel(0x48404865); // 20.31% + InitDS_Sel(0x48468865); // 8.18% + InitDS_Sel(0x48478065); // 17.47% + InitDS_Sel(0x48820965); // 7.89% + InitDS_Sel(0x48830875); // 5.84% + InitDS_Sel(0x48868865); // 5.44% + InitDS_Sel(0x48868965); // 9.13% + InitDS_Sel(0x48878165); // 28.27% + InitDS_Sel(0x488f89f5); // 20.19% + InitDS_Sel(0x488f89f6); // 27.92% + InitDS_Sel(0x49068065); // 7.68% + InitDS_Sel(0x49068865); // 20.15% + InitDS_Sel(0x49068965); // 8.65% + InitDS_Sel(0x49078065); // 14.00% + InitDS_Sel(0x49078165); // 5.63% + InitDS_Sel(0xc883004d); // 22.42% + InitDS_Sel(0xc887814d); // 9.20% + InitDS_Sel(0xc8878165); // 71.18% + InitDS_Sel(0xc8879065); // 26.05% + InitDS_Sel(0xc887914d); // 8.40% + InitDS_Sel(0xc88791e5); // 82.67% + InitDS_Sel(0xc9078065); // 8.00% + InitDS_Sel(0xcc819055); // 14.54% + InitDS_Sel(0xcc839065); // 7.41% + InitDS_Sel(0xd5204055); // 34.58% + InitDS_Sel(0x48804855); // 11.77% + InitDS_Sel(0x48804865); // 12.94% + InitDS_Sel(0x488e8965); // 5.04% + InitDS_Sel(0x49004875); // 11.36% + InitDS_Sel(0x9100404d); // 8.54% + InitDS_Sel(0x9fe78075); // 18.54% + InitDS_Sel(0x9fe78155); // 13.08% + InitDS_Sel(0xcd078075); // 9.47% + + // ffx-2 + + InitDS_Sel(0x110a8965); // 17.40% + InitDS_Sel(0x1fe30069); // 17.38% + InitDS_Sel(0x1fe5884d); // 11.56% + InitDS_Sel(0x48468965); // 79.89% + InitDS_Sel(0x4881884d); // 5.61% + InitDS_Sel(0x488781f5); // 5.84% + InitDS_Sel(0x4890404c); // 108.44% + InitDS_Sel(0x4893084c); // 24.74% + InitDS_Sel(0x49004859); // 48.72% + InitDS_Sel(0x49004865); // 13.08% + InitDS_Sel(0x49004869); // 22.94% + InitDS_Sel(0x4900494d); // 470.30% + InitDS_Sel(0x4907814d); // 14.15% + InitDS_Sel(0x49078865); // 21.56% + InitDS_Sel(0x49078965); // 11.37% + InitDS_Sel(0x490e8165); // 15.52% + InitDS_Sel(0xc8478165); // 10.44% + InitDS_Sel(0xc8804055); // 36.58% + InitDS_Sel(0xc881004d); // 13.53% + InitDS_Sel(0xc8830055); // 23.97% + InitDS_Sel(0xc885004d); // 14.32% + InitDS_Sel(0xc893004c); // 136.83% + InitDS_Sel(0xc895004c); // 16.48% + InitDS_Sel(0xc9004055); // 13.92% + InitDS_Sel(0xc9004059); // 15.87% + InitDS_Sel(0xc9004065); // 18.61% + InitDS_Sel(0xc9059155); // 13.56% + InitDS_Sel(0xc907814d); // 15.06% + InitDS_Sel(0xc9078165); // 12.21% + InitDS_Sel(0xcc804055); // 16.51% + InitDS_Sel(0xcc850055); // 17.01% + InitDS_Sel(0xc88581cd); // 7.32% + InitDS_Sel(0xc88581e5); // 5.41% + + // ffxii + + InitDS_Sel(0x1fe6804c); // 9.05% + InitDS_Sel(0x1fe68064); // 5.11% + InitDS_Sel(0x1fe6884c); // 14.58% + InitDS_Sel(0x1fee8864); // 88.41% + InitDS_Sel(0x1fee8964); // 33.72% + InitDS_Sel(0x48404064); // 30.72% + InitDS_Sel(0x4847004c); // 17.41% + InitDS_Sel(0x48828864); // 6.06% + InitDS_Sel(0x4883004c); // 20.10% + InitDS_Sel(0x4883084c); // 12.37% + InitDS_Sel(0x4886804c); // 5.10% + InitDS_Sel(0x4887084c); // 226.61% + InitDS_Sel(0x48878064); // 7.39% + InitDS_Sel(0x488e8b64); // 16.26% + InitDS_Sel(0x48904064); // 29.47% + InitDS_Sel(0x49004064); // 9.31% + InitDS_Sel(0x49078064); // 28.77% + InitDS_Sel(0x5fe0404c); // 70.30% + InitDS_Sel(0x9fe3904c); // 10.16% + InitDS_Sel(0xc887004c); // 18.63% + InitDS_Sel(0xc8878064); // 19.71% + InitDS_Sel(0xc887904c); // 13.03% + InitDS_Sel(0xc9278064); // 39.54% + + // kingdom hearts + + InitDS_Sel(0x4840404c); // 15.00% + InitDS_Sel(0x48830874); // 14.41% + InitDS_Sel(0x48868154); // 7.22% + InitDS_Sel(0x4886884c); // 28.13% + InitDS_Sel(0x4886904c); // 16.66% + InitDS_Sel(0x490e8974); // 59.46% + InitDS_Sel(0xc8818054); // 13.73% + InitDS_Sel(0xc8858054); // 11.63% + InitDS_Sel(0xc9004054); // 14.66% + + // kingdom hearts 2 + + InitDS_Sel(0x48804060); // 19.75% + InitDS_Sel(0x488a8964); // 20.35% + InitDS_Sel(0x9fe39054); // 16.04% + InitDS_Sel(0xc8810054); // 28.27% + InitDS_Sel(0xcc83004d); // 53.63% + InitDS_Sel(0xcd03004d); // 20.55% + + // persona 3 + + InitDS_Sel(0x484e8068); // 29.91% + InitDS_Sel(0x4881804c); // 18.16% + InitDS_Sel(0x4881904c); // 24.90% + InitDS_Sel(0x490e8068); // 5.82% + InitDS_Sel(0x4b07904c); // 59.21% + InitDS_Sel(0x4d47834c); // 29.42% + InitDS_Sel(0x4d47934c); // 27.37% + InitDS_Sel(0xca43004c); // 17.88% + InitDS_Sel(0xcb07934c); // 11.38% + InitDS_Sel(0xcd47804c); // 106.54% + InitDS_Sel(0xcd47834c); // 104.37% + + // persona 4 + + InitDS_Sel(0x1fe04058); // 23.41% + InitDS_Sel(0x4840484c); // 24.66% + InitDS_Sel(0x4881834c); // 21.87% + InitDS_Sel(0x4881934c); // 21.68% + InitDS_Sel(0x48828368); // 9.84% + InitDS_Sel(0x48868f68); // 29.84% + InitDS_Sel(0x48879168); // 5.74% + InitDS_Sel(0x49068868); // 18.43% + InitDS_Sel(0x49078068); // 63.62% + InitDS_Sel(0x49079068); // 57.82% + InitDS_Sel(0x490e8868); // 22.88% + InitDS_Sel(0x4a47804c); // 68.03% + InitDS_Sel(0x4a47904c); // 20.99% + InitDS_Sel(0x4a80404c); // 190.06% + InitDS_Sel(0x4a83004c); // 25.28% + InitDS_Sel(0x4a87804c); // 26.31% + InitDS_Sel(0x4a878068); // 29.36% + InitDS_Sel(0x4a878868); // 23.13% + InitDS_Sel(0x4a879068); // 11.16% + InitDS_Sel(0x4b00404c); // 111.77% + InitDS_Sel(0x4b07804c); // 23.00% + InitDS_Sel(0x4b07884c); // 21.15% + InitDS_Sel(0x5fe04058); // 37.01% + InitDS_Sel(0x5fe04858); // 87.23% + + // sfex3 + + InitDS_Sel(0x1fe04868); // 8.37% + InitDS_Sel(0x1fe6b068); // 16.21% + InitDS_Sel(0x1fe6b868); // 6.50% + InitDS_Sel(0x41268068); // 8.16% + InitDS_Sel(0x41269068); // 9.51% + InitDS_Sel(0x4886b068); // 20.50% + InitDS_Sel(0x4886b868); // 35.05% + InitDS_Sel(0x49079078); // 6.90% + InitDS_Sel(0x4c868068); // 5.48% + InitDS_Sel(0x4c868868); // 6.05% + InitDS_Sel(0x9fe1004e); // 6.98% + InitDS_Sel(0x9fe3004e); // 13.33% + InitDS_Sel(0xc8859058); // 10.74% + InitDS_Sel(0xcc804058); // 8.15% + InitDS_Sel(0xcd404058); // 5.04% + + // gt4 + + InitDS_Sel(0x1fee8164); // 7.38% + InitDS_Sel(0x488e8f64); // 7.09% + InitDS_Sel(0x488e9764); // 31.70% + InitDS_Sel(0x4b1a8864); // 5.45% + InitDS_Sel(0x5fe3904e); // 8.54% + + // katamary damacy + + InitDS_Sel(0x488e89e4); // 10.50% + InitDS_Sel(0x488e91d4); // 18.22% + InitDS_Sel(0xc88181cc); // 7.69% + InitDS_Sel(0xc8904054); // 12.39% + + // grandia 3 + + InitDS_Sel(0x1fe0404e); // 7.99% + InitDS_Sel(0x48868360); // 5.62% + InitDS_Sel(0x48868860); // 8.40% + InitDS_Sel(0x48869360); // 7.19% + InitDS_Sel(0x4887884c); // 5.93% + InitDS_Sel(0x488a8060); // 12.82% + InitDS_Sel(0x488e8360); // 26.69% + InitDS_Sel(0x488e8b60); // 32.33% + InitDS_Sel(0x488e8f60); // 15.38% + InitDS_Sel(0x488e9060); // 7.52% + InitDS_Sel(0x50368060); // 9.24% + InitDS_Sel(0xc8878070); // 37.81% + InitDS_Sel(0xcc81804c); // 46.79% + InitDS_Sel(0xcc839060); // 55.67% + + // rumble roses + + InitDS_Sel(0x1fe78064); // 26.70% + InitDS_Sel(0x1fe79064); // 9.93% + InitDS_Sel(0x4880484c); // 6.38% + InitDS_Sel(0x48838164); // 11.01% + InitDS_Sel(0x4887b864); // 35.30% + InitDS_Sel(0x488e8164); // 5.23% + InitDS_Sel(0x4900484c); // 8.86% + InitDS_Sel(0x49078864); // 7.98% + InitDS_Sel(0x490e8964); // 8.26% + InitDS_Sel(0x9fe3004d); // 8.92% + InitDS_Sel(0xc8838164); // 14.00% + InitDS_Sel(0xc8878164); // 15.96% + InitDS_Sel(0xcc830064); // 35.39% + + // dmc + + InitDS_Sel(0x4423904c); // 7.89% + InitDS_Sel(0x4427904c); // 17.05% + InitDS_Sel(0x45204078); // 9.08% + InitDS_Sel(0x4c87914c); // 40.48% + InitDS_Sel(0x54204078); // 9.09% + InitDS_Sel(0x9fe39058); // 7.17% + InitDS_Sel(0x9fe78068); // 9.28% + InitDS_Sel(0xc427904c); // 8.30% + InitDS_Sel(0xc520404c); // 8.11% + InitDS_Sel(0xc8804078); // 6.11% + InitDS_Sel(0xc8810068); // 7.80% + InitDS_Sel(0xc8830068); // 10.05% + InitDS_Sel(0xcc43804c); // 17.32% + InitDS_Sel(0xd420404c); // 8.03% + + // xenosaga 2 + + InitDS_Sel(0x1fee804c); // 15.39% + InitDS_Sel(0x49079064); // 31.08% + InitDS_Sel(0x51229064); // 8.86% + InitDS_Sel(0xc8804074); // 16.71% + InitDS_Sel(0xc9079054); // 17.35% + InitDS_Sel(0xcc804054); // 14.57% + InitDS_Sel(0xcc839054); // 24.04% + InitDS_Sel(0xcd004054); // 14.54% + + // nfs mw + + InitDS_Sel(0x1fe68068); // 18.10% + InitDS_Sel(0x1fe6806a); // 6.75% + InitDS_Sel(0x1fe68164); // 5.78% + InitDS_Sel(0x1fe68868); // 117.99% + InitDS_Sel(0x1fe68964); // 48.41% + InitDS_Sel(0x4883804e); // 7.16% + InitDS_Sel(0x48868868); // 46.01% + InitDS_Sel(0x4b004064); // 22.82% + InitDS_Sel(0x4b004068); // 33.12% + InitDS_Sel(0x4b004864); // 21.73% + InitDS_Sel(0x4b004868); // 37.86% + InitDS_Sel(0x4b028064); // 20.96% + InitDS_Sel(0x4b028068); // 30.53% + InitDS_Sel(0x4b028864); // 24.64% + InitDS_Sel(0x4b028868); // 31.04% + InitDS_Sel(0x4b038064); // 13.05% + InitDS_Sel(0xc805904c); // 17.04% + InitDS_Sel(0xc9078064); // 13.78% + InitDS_Sel(0xc927904c); // 18.78% + InitDS_Sel(0xcc83004c); // 5.32% + InitDS_Sel(0xcc83804c); // 19.33% + InitDS_Sel(0xcc83804e); // 5.85% + InitDS_Sel(0xcd03914c); // 12.00% + InitDS_Sel(0xd127904c); // 18.81% + InitDS_Sel(0xdfe19064); // 27.63% + + // berserk + + InitDS_Sel(0x48804064); // 41.22% + InitDS_Sel(0x48878864); // 41.61% + InitDS_Sel(0x488e8064); // 32.85% + InitDS_Sel(0x488e8964); // 33.35% + InitDS_Sel(0x49004874); // 10.91% + InitDS_Sel(0x4c8e8864); // 11.27% + InitDS_Sel(0x4c8fb864); // 5.61% + InitDS_Sel(0xc8804064); // 14.54% + InitDS_Sel(0xc8830064); // 24.60% + InitDS_Sel(0xcd03004c); // 31.16% + InitDS_Sel(0xdfe3004c); // 7.40% + InitDS_Sel(0xdfe3904c); // 18.00% + + // castlevania + + InitDS_Sel(0x1fe78868); // 19.07% + InitDS_Sel(0x48878868); // 85.08% + InitDS_Sel(0x4d00407a); // 17.23% + InitDS_Sel(0x9fe1004c); // 9.10% + InitDS_Sel(0x9fe3904e); // 16.38% + InitDS_Sel(0x9fe5904c); // 11.74% + InitDS_Sel(0xc8804068); // 13.17% + InitDS_Sel(0xc881004e); // 17.78% + InitDS_Sel(0xca80404c); // 8.43% + InitDS_Sel(0xdfe3804c); // 17.00% + + // okami + + InitDS_Sel(0x48804058); // 10.29% + InitDS_Sel(0x48878058); // 11.71% + InitDS_Sel(0x48878168); // 250.79% + InitDS_Sel(0x488e8068); // 20.22% + InitDS_Sel(0x488e8868); // 32.58% + InitDS_Sel(0x488e8968); // 29.40% + InitDS_Sel(0x49078168); // 13.87% + InitDS_Sel(0x9fe18058); // 10.94% + InitDS_Sel(0xc5218058); // 26.94% + InitDS_Sel(0xc881804c); // 11.98% + InitDS_Sel(0xc8839158); // 22.61% + InitDS_Sel(0xc8878158); // 8.98% + InitDS_Sel(0xc8878168); // 9.05% + InitDS_Sel(0xc8879168); // 6.53% + InitDS_Sel(0xc9078168); // 15.66% + InitDS_Sel(0xca83804c); // 16.89% + InitDS_Sel(0xcc43904c); // 77.10% + InitDS_Sel(0xdfe59068); // 64.26% + + // bully + + InitDS_Sel(0x110e8864); // 65.16% + InitDS_Sel(0x110e8964); // 50.07% + InitDS_Sel(0x4d068064); // 23.14% + InitDS_Sel(0x4d068364); // 21.42% + InitDS_Sel(0x4d068864); // 22.79% + InitDS_Sel(0x9fe04077); // 8.61% + InitDS_Sel(0xc901004c); // 22.07% + InitDS_Sel(0xca83904c); // 51.08% + InitDS_Sel(0xd480404d); // 15.61% + InitDS_Sel(0xd501904e); // 37.30% + + // culdcept + + InitDS_Sel(0x1fe04866); // 15.93% + InitDS_Sel(0x1fe2a9e6); // 55.93% + InitDS_Sel(0x9fe391e6); // 31.07% + InitDS_Sel(0x9fe3a1e6); // 9.26% + InitDS_Sel(0x9fe591e6); // 9.73% + InitDS_Sel(0xc88181e6); // 9.89% + InitDS_Sel(0x1fe2a1e6); // 15.71% + InitDS_Sel(0x49004866); // 5.05% + InitDS_Sel(0x4d02a1e6); // 15.31% + InitDS_Sel(0x9fe191e6); // 5.64% + InitDS_Sel(0x9fe59066); // 20.56% + InitDS_Sel(0x9fe991e6); // 19.59% + InitDS_Sel(0xcd0381e6); // 5.84% + + // suikoden 5 + + InitDS_Sel(0x00428868); // 14.32% + InitDS_Sel(0x40428868); // 20.87% + InitDS_Sel(0x4846804c); // 27.56% + InitDS_Sel(0x48819368); // 26.24% + InitDS_Sel(0x48828b68); // 29.80% + InitDS_Sel(0x48829368); // 22.30% + InitDS_Sel(0x48858368); // 8.44% + InitDS_Sel(0x48858b68); // 6.10% + InitDS_Sel(0x48859068); // 22.77% + InitDS_Sel(0x48859368); // 7.35% + InitDS_Sel(0x48869068); // 30.96% + InitDS_Sel(0x48878b68); // 5.18% + InitDS_Sel(0x48879368); // 10.31% + InitDS_Sel(0x488a8b68); // 11.73% + InitDS_Sel(0x49028868); // 14.35% + InitDS_Sel(0x4906804c); // 30.53% + InitDS_Sel(0x4d068868); // 33.72% + InitDS_Sel(0x4d0e8868); // 34.68% + + // dq8 + + InitDS_Sel(0x48830064); // 8.11% + InitDS_Sel(0x48869164); // 12.03% + InitDS_Sel(0x490a8164); // 5.03% + InitDS_Sel(0x490e904c); // 19.05% + InitDS_Sel(0x490f904c); // 15.81% + InitDS_Sel(0x9103b04c); // 5.05% + InitDS_Sel(0xc840404c); // 15.86% + InitDS_Sel(0xc883914c); // 5.84% + InitDS_Sel(0xc885804c); // 22.07% + InitDS_Sel(0xc8859054); // 9.61% + InitDS_Sel(0xc8c3804c); // 36.23% + InitDS_Sel(0xdfe3904e); // 5.49% + + // resident evil 4 + + InitDS_Sel(0x1fe04057); // 6.33% + InitDS_Sel(0x48868064); // 7.84% + InitDS_Sel(0x4886814c); // 5.42% + InitDS_Sel(0x48868164); // 12.98% + InitDS_Sel(0x48868864); // 39.04% + InitDS_Sel(0x48868964); // 10.15% + InitDS_Sel(0x48878164); // 64.35% + InitDS_Sel(0x4b068064); // 6.74% + InitDS_Sel(0x9fe18064); // 11.81% + InitDS_Sel(0xc880404c); // 8.39% + InitDS_Sel(0xc883814c); // 13.71% + InitDS_Sel(0xc885904c); // 11.74% + InitDS_Sel(0xc887804c); // 25.43% + InitDS_Sel(0xc887814c); // 17.68% + InitDS_Sel(0xc903904c); // 15.89% + InitDS_Sel(0xc907814c); // 10.74% + InitDS_Sel(0xcc879064); // 25.97% + InitDS_Sel(0xcd004064); // 70.25% + InitDS_Sel(0xcd03904c); // 23.53% + InitDS_Sel(0xcd07814c); // 8.41% + InitDS_Sel(0xd483904c); // 6.04% + InitDS_Sel(0xdfe1904e); // 19.81% + InitDS_Sel(0xdfe5904c); // 12.52% + + // tomoyo after + + InitDS_Sel(0x48404868); // 30.60% + InitDS_Sel(0x9fe38059); // 21.23% + InitDS_Sel(0x9fe39059); // 20.70% + InitDS_Sel(0xc8478068); // 8.06% + InitDS_Sel(0xc8818068); // 26.07% + InitDS_Sel(0xc9058068); // 15.90% + InitDS_Sel(0xca858068); // 14.66% + + // .hack redemption + + InitDS_Sel(0x1fe04864); // 5.01% + InitDS_Sel(0x48404074); // 6.97% + InitDS_Sel(0x48469064); // 20.80% + InitDS_Sel(0x48478064); // 8.87% + InitDS_Sel(0x48804864); // 5.94% + InitDS_Sel(0x48869364); // 22.39% + InitDS_Sel(0x488e9064); // 23.49% + InitDS_Sel(0x49004074); // 6.95% + InitDS_Sel(0x49004864); // 7.26% + InitDS_Sel(0xc123004c); // 17.86% + InitDS_Sel(0xc8478064); // 8.89% + InitDS_Sel(0xc8804054); // 13.68% + InitDS_Sel(0xc903004c); // 16.45% + InitDS_Sel(0xcc41804c); // 11.98% + InitDS_Sel(0xdfe1004c); // 11.81% + + // wild arms 4 + + InitDS_Sel(0x9fe19050); // 6.28% + InitDS_Sel(0x9fe58064); // 11.45% + InitDS_Sel(0x9fe59064); // 10.01% + InitDS_Sel(0xc8404064); // 22.23% + InitDS_Sel(0xccc0404c); // 10.08% + InitDS_Sel(0xccc04064); // 5.64% + InitDS_Sel(0xcd07804c); // 39.49% + InitDS_Sel(0xcd078164); // 19.54% + InitDS_Sel(0xcd45804c); // 5.23% + InitDS_Sel(0xdfe19054); // 17.70% + + // wild arms 5 + + InitDS_Sel(0x4885884c); // 6.64% + InitDS_Sel(0x4887904c); // 25.66% + InitDS_Sel(0x488e8764); // 25.45% + InitDS_Sel(0x48c68864); // 6.46% + InitDS_Sel(0x9fe3804c); // 7.00% + InitDS_Sel(0xc845804c); // 14.03% + InitDS_Sel(0xdfe39054); // 19.69% + + // shadow of the colossus + + InitDS_Sel(0x4883804c); // 184.84% + InitDS_Sel(0x48868b64); // 12.88% + InitDS_Sel(0x48869064); // 27.18% + InitDS_Sel(0x48878364); // 23.80% + InitDS_Sel(0x48879064); // 27.06% + InitDS_Sel(0x48879364); // 16.89% + InitDS_Sel(0x488e8864); // 148.99% + InitDS_Sel(0x488e9364); // 5.28% + InitDS_Sel(0x490e8064); // 66.50% + InitDS_Sel(0x490e8864); // 8.82% + InitDS_Sel(0x490f8064); // 8.23% + InitDS_Sel(0x4d004064); // 88.17% + InitDS_Sel(0x9fe04064); // 9.69% + InitDS_Sel(0x9fe1004d); // 10.39% + InitDS_Sel(0x9fe3904d); // 15.56% + InitDS_Sel(0xc883804c); // 45.16% + InitDS_Sel(0xc883904c); // 5.41% + InitDS_Sel(0xc8938064); // 45.66% + InitDS_Sel(0xc8939064); // 15.27% + InitDS_Sel(0xc900404c); // 8.73% + InitDS_Sel(0xc9004064); // 51.37% + InitDS_Sel(0xc903804c); // 35.17% + InitDS_Sel(0xca83004c); // 17.37% + InitDS_Sel(0xcc030064); // 21.25% + InitDS_Sel(0xcc80404c); // 9.19% + + // tales of rebirth + + InitDS_Sel(0x48404054); // 10.33% + InitDS_Sel(0x48878054); // 75.33% + InitDS_Sel(0x48878b64); // 10.17% + InitDS_Sel(0x4c838854); // 15.15% + InitDS_Sel(0xc8478054); // 13.29% + InitDS_Sel(0xc88b9054); // 7.08% + InitDS_Sel(0xcc838064); // 16.57% + + // digital devil saga + + InitDS_Sel(0x48804050); // 6.18% + InitDS_Sel(0x48868870); // 5.80% + InitDS_Sel(0x488e8860); // 22.08% + InitDS_Sel(0x4907884c); // 28.15% + InitDS_Sel(0x9fe39070); // 5.89% + + // dbzbt2 + + InitDS_Sel(0x4906884c); // 17.04% + InitDS_Sel(0x4c904064); // 28.94% + InitDS_Sel(0xc8878074); // 26.80% + InitDS_Sel(0xc9078054); // 22.93% + + // dbzbt3 + + InitDS_Sel(0x489081e4); // 11.25% + InitDS_Sel(0x48968864); // 16.90% + InitDS_Sel(0x4c469064); // 14.83% + InitDS_Sel(0x4c80404c); // 6.99% + InitDS_Sel(0x4c869064); // 15.61% + InitDS_Sel(0xc88391cc); // 12.45% + InitDS_Sel(0xc885904e); // 16.45% + InitDS_Sel(0xc8859074); // 15.19% + InitDS_Sel(0xc905904c); // 27.52% + InitDS_Sel(0xc917804c); // 5.66% + InitDS_Sel(0xca40404c); // 10.86% + InitDS_Sel(0xcc45904e); // 33.93% + InitDS_Sel(0xcc80404e); // 12.83% + InitDS_Sel(0xcc8391cc); // 25.49% + + // dbz iw + + InitDS_Sel(0x1fe48864); // 11.22% + InitDS_Sel(0x1fe49064); // 5.09% + InitDS_Sel(0x1fec8864); // 26.53% + InitDS_Sel(0x1fec8964); // 6.54% + InitDS_Sel(0x48808064); // 7.90% + InitDS_Sel(0x48848064); // 5.32% + InitDS_Sel(0x48848864); // 18.43% + InitDS_Sel(0x48858064); // 20.14% + InitDS_Sel(0x48859064); // 13.97% + InitDS_Sel(0x49058064); // 9.97% + InitDS_Sel(0x49084064); // 9.60% + InitDS_Sel(0x9fe19064); // 17.67% + InitDS_Sel(0xc881004c); // 16.40% + InitDS_Sel(0xc8858064); // 38.16% + InitDS_Sel(0xc8859064); // 26.38% + InitDS_Sel(0xc88d8064); // 7.05% + InitDS_Sel(0xc88d9064); // 15.38% + InitDS_Sel(0xc9058064); // 12.95% + InitDS_Sel(0xc9084064); // 14.20% + InitDS_Sel(0xc90d8064); // 21.35% + InitDS_Sel(0xcd404054); // 18.85% + + // disgaea 2 + + InitDS_Sel(0x1fe04064); // 5.51% + InitDS_Sel(0x1fe69074); // 8.82% + InitDS_Sel(0x48878964); // 65.93% + InitDS_Sel(0xc8879164); // 5.09% + + // gradius 5 + + InitDS_Sel(0x48868968); // 40.64% + InitDS_Sel(0x48878968); // 7.97% + InitDS_Sel(0x49078968); // 7.80% + InitDS_Sel(0x490e884c); // 37.26% + InitDS_Sel(0x5fe68068); // 32.99% + InitDS_Sel(0x5fe68968); // 15.59% + InitDS_Sel(0x5fee8168); // 5.25% + InitDS_Sel(0x5fee8868); // 36.64% + InitDS_Sel(0x5fee8968); // 16.28% + InitDS_Sel(0x5ffe8868); // 6.67% + InitDS_Sel(0xdfe3814c); // 24.29% + + // tales of abyss + + InitDS_Sel(0x1fe39368); // 7.47% + InitDS_Sel(0x4885804c); // 14.27% + InitDS_Sel(0x48868068); // 11.14% + InitDS_Sel(0x4886934c); // 6.41% + InitDS_Sel(0x4887834c); // 8.90% + InitDS_Sel(0x488e8368); // 13.35% + InitDS_Sel(0x48cf89e8); // 39.25% + InitDS_Sel(0x4903834c); // 21.04% + InitDS_Sel(0x490c8b68); // 15.04% + InitDS_Sel(0x490e8b68); // 8.05% + InitDS_Sel(0x490f89e8); // 39.57% + InitDS_Sel(0x4d03914c); // 18.97% + InitDS_Sel(0xc121004c); // 26.59% + InitDS_Sel(0xc887934c); // 5.73% + InitDS_Sel(0xdfe59078); // 21.43% + + // Gundam Seed Destiny OMNI VS ZAFT II PLUS + + InitDS_Sel(0x1fe68075); // 27.61% + InitDS_Sel(0x4880484d); // 13.50% + InitDS_Sel(0x48878075); // 8.16% + InitDS_Sel(0x48878375); // 8.66% + InitDS_Sel(0x4887884d); // 17.82% + InitDS_Sel(0x48878b75); // 53.12% + InitDS_Sel(0x488e8075); // 42.24% + InitDS_Sel(0x488e8375); // 35.32% + InitDS_Sel(0x488e8875); // 25.59% + InitDS_Sel(0x488e8b75); // 51.44% + InitDS_Sel(0x488e9075); // 16.57% + InitDS_Sel(0x49068075); // 35.78% + InitDS_Sel(0x4906884d); // 6.37% + InitDS_Sel(0x490e8375); // 31.56% + InitDS_Sel(0x490e8875); // 35.20% + InitDS_Sel(0x490e8b75); // 38.85% + InitDS_Sel(0x9fe19075); // 20.98% + InitDS_Sel(0xc8878075); // 14.30% + + // nba 2k8 + + InitDS_Sel(0x1fe04056); // 15.57% + InitDS_Sel(0x1fe38966); // 28.88% + InitDS_Sel(0x1fe39156); // 25.28% + InitDS_Sel(0x1fe60866); // 5.67% + InitDS_Sel(0x1fe68866); // 5.75% + InitDS_Sel(0x48838166); // 10.93% + InitDS_Sel(0x48868066); // 7.59% + InitDS_Sel(0x48868166); // 10.44% + InitDS_Sel(0x48868866); // 42.03% + InitDS_Sel(0x48868966); // 30.06% + InitDS_Sel(0x48869166); // 6.52% + InitDS_Sel(0x48879066); // 10.60% + InitDS_Sel(0x49028966); // 7.28% + InitDS_Sel(0x49068066); // 31.37% + InitDS_Sel(0x49068966); // 11.65% + InitDS_Sel(0x49068976); // 45.50% + InitDS_Sel(0x5fe68866); // 22.26% + InitDS_Sel(0x9fe79066); // 28.38% + InitDS_Sel(0xc8879166); // 6.42% + InitDS_Sel(0xdfe79066); // 30.98% + + // onimusha 3 + + InitDS_Sel(0x1fee004c); // 5.11% + InitDS_Sel(0x1fee0868); // 42.66% + InitDS_Sel(0x1fee8968); // 7.76% + InitDS_Sel(0x48878068); // 24.27% + InitDS_Sel(0x48c28368); // 5.97% + InitDS_Sel(0x4903884c); // 28.26% + InitDS_Sel(0x49068068); // 9.53% + InitDS_Sel(0x4d05884c); // 6.59% + InitDS_Sel(0x5fe04078); // 29.53% + InitDS_Sel(0x9fe18068); // 5.38% + InitDS_Sel(0xc8839168); // 6.59% + InitDS_Sel(0xcc81904c); // 5.21% + InitDS_Sel(0xcc878168); // 7.18% + InitDS_Sel(0xcd004068); // 28.32% + InitDS_Sel(0xcd03804c); // 7.20% + InitDS_Sel(0xd425904c); // 5.69% + InitDS_Sel(0xdfe78368); // 6.71% + InitDS_Sel(0xdfe7904c); // 8.43% + InitDS_Sel(0xdfe79368); // 9.36% + + // resident evil code veronica + + InitDS_Sel(0x1fee8168); // 9.31% + InitDS_Sel(0x1fee8868); // 6.75% + InitDS_Sel(0x48804068); // 12.40% + InitDS_Sel(0x48804868); // 41.21% + InitDS_Sel(0x48804b68); // 7.16% + InitDS_Sel(0x9fe39068); // 17.58% + InitDS_Sel(0x9fe79068); // 25.03% + InitDS_Sel(0x9fe79168); // 25.89% + InitDS_Sel(0xc8878068); // 29.01% + InitDS_Sel(0xc8878368); // 11.44% + InitDS_Sel(0xc8879368); // 6.59% + InitDS_Sel(0xcc819058); // 23.03% + + // armored core 3 + + InitDS_Sel(0x1fe84074); // 6.66% + InitDS_Sel(0x1fee0874); // 59.28% + InitDS_Sel(0x48404854); // 7.27% + InitDS_Sel(0x48878074); // 10.32% + InitDS_Sel(0x48878874); // 16.96% + InitDS_Sel(0x488e8074); // 25.40% + InitDS_Sel(0x490e8074); // 79.82% + InitDS_Sel(0x4c4e8074); // 23.05% + InitDS_Sel(0x4d0b0864); // 5.94% + InitDS_Sel(0x4d0e8074); // 8.44% + InitDS_Sel(0xc8404054); // 9.47% + InitDS_Sel(0xc8850054); // 11.54% + InitDS_Sel(0xc88581d4); // 6.72% + InitDS_Sel(0xc88791d4); // 6.83% + InitDS_Sel(0xc9059054); // 13.98% + InitDS_Sel(0xc9078074); // 9.60% + + // aerial planet + + InitDS_Sel(0x4886894c); // 18.07% + InitDS_Sel(0x488e814c); // 16.96% + InitDS_Sel(0x4c868074); // 46.13% + InitDS_Sel(0x4c868874); // 7.71% + InitDS_Sel(0x4c868934); // 19.26% + InitDS_Sel(0x4c8e8074); // 12.50% + InitDS_Sel(0x4c8e8874); // 27.69% + InitDS_Sel(0x4cc0404c); // 15.74% + InitDS_Sel(0xc820404c); // 16.32% + InitDS_Sel(0xc8478164); // 11.12% + InitDS_Sel(0xc847914c); // 7.70% + InitDS_Sel(0xc887914c); // 8.85% + + // one piece grand battle 3 + + InitDS_Sel(0x48804054); // 12.28% + InitDS_Sel(0x48878854); // 12.62% + InitDS_Sel(0x49068174); // 5.54% + InitDS_Sel(0x49068874); // 28.97% + InitDS_Sel(0x49068964); // 16.11% + InitDS_Sel(0x49078174); // 11.17% + InitDS_Sel(0x9fe1904e); // 10.23% + InitDS_Sel(0xc8839054); // 21.50% + InitDS_Sel(0xc8878054); // 6.54% + InitDS_Sel(0xc8879054); // 10.18% + InitDS_Sel(0xc9078174); // 8.49% + InitDS_Sel(0xcac0404c); // 9.30% + InitDS_Sel(0xcc41904c); // 12.33% + InitDS_Sel(0xcc4190cc); // 7.21% + InitDS_Sel(0xd321914c); // 7.10% + + // one piece grand adventure + + InitDS_Sel(0x1fe0404c); // 5.18% + InitDS_Sel(0x48829164); // 12.79% + InitDS_Sel(0x48849164); // 11.67% + InitDS_Sel(0x48869154); // 5.38% + InitDS_Sel(0x48878154); // 5.01% + InitDS_Sel(0x48879154); // 5.57% + InitDS_Sel(0x49078964); // 151.46% + InitDS_Sel(0xc421814c); // 7.73% + InitDS_Sel(0xc843b04c); // 21.62% + + // shadow hearts + + InitDS_Sel(0x1fe6904c); // 5.97% + InitDS_Sel(0x48868078); // 9.90% + InitDS_Sel(0x48868778); // 9.59% + InitDS_Sel(0x49004058); // 7.20% + InitDS_Sel(0x49030058); // 40.18% + InitDS_Sel(0x4c870878); // 6.19% + InitDS_Sel(0x9fe3004c); // 20.51% + InitDS_Sel(0xc8804058); // 9.57% + InitDS_Sel(0xc881904e); // 5.29% + InitDS_Sel(0xc8819168); // 13.94% + InitDS_Sel(0xc8839058); // 12.53% + InitDS_Sel(0xc8878058); // 13.80% + InitDS_Sel(0xc8879058); // 9.68% + InitDS_Sel(0xc9039058); // 12.08% + InitDS_Sel(0xc9078068); // 29.18% + + // the punisher + + InitDS_Sel(0x48420864); // 5.88% + InitDS_Sel(0x48468864); // 8.39% + InitDS_Sel(0x48868764); // 16.69% + InitDS_Sel(0x4886bf64); // 14.77% + InitDS_Sel(0x49068064); // 7.90% + InitDS_Sel(0x4906904c); // 14.02% + InitDS_Sel(0x4d068f64); // 12.33% + InitDS_Sel(0x5fe68f64); // 62.24% + InitDS_Sel(0xc880474c); // 20.85% + InitDS_Sel(0xc883974c); // 6.07% + InitDS_Sel(0xc887874c); // 8.47% + InitDS_Sel(0xc887974c); // 10.36% + InitDS_Sel(0xdfe3974c); // 21.42% + + // guitar hero + + InitDS_Sel(0x1fe0407b); // 6.67% + InitDS_Sel(0x1fe6887a); // 9.08% + InitDS_Sel(0x4880484e); // 34.74% + InitDS_Sel(0x4886807a); // 6.73% + InitDS_Sel(0x4886887a); // 47.23% + InitDS_Sel(0x4886907a); // 5.53% + InitDS_Sel(0x4886956a); // 8.71% + InitDS_Sel(0x4887854e); // 6.67% + InitDS_Sel(0x4887887a); // 31.33% + InitDS_Sel(0x4887954e); // 20.26% + InitDS_Sel(0x488a917a); // 33.17% + InitDS_Sel(0x488e887a); // 80.36% + InitDS_Sel(0x488e8d7a); // 5.16% + InitDS_Sel(0x4906806a); // 23.53% + InitDS_Sel(0x4906886a); // 5.02% + InitDS_Sel(0x4d06a06a); // 22.00% + InitDS_Sel(0x4d06a86a); // 15.44% + InitDS_Sel(0x4d0ea06a); // 5.08% + InitDS_Sel(0x9503204e); // 22.47% + InitDS_Sel(0x9fe3906a); // 12.46% + InitDS_Sel(0xc887855a); // 5.09% + InitDS_Sel(0xc887857a); // 27.54% + InitDS_Sel(0xcd03204e); // 70.62% + InitDS_Sel(0xcd07806a); // 8.94% + + // ico + + InitDS_Sel(0x1fe28060); // 16.14% + InitDS_Sel(0x1fe68860); // 47.91% + InitDS_Sel(0x48868060); // 10.32% + InitDS_Sel(0x48868b60); // 94.89% + InitDS_Sel(0x49068b60); // 10.26% + InitDS_Sel(0x4c468b60); // 41.67% + InitDS_Sel(0x4c478860); // 11.90% + InitDS_Sel(0x4d004060); // 102.46% + InitDS_Sel(0x4d028060); // 17.75% + InitDS_Sel(0x4d068360); // 16.19% + InitDS_Sel(0x4d068860); // 18.08% + InitDS_Sel(0x4d068b60); // 223.13% + InitDS_Sel(0x4d078360); // 5.55% + InitDS_Sel(0x9fe04060); // 7.09% + InitDS_Sel(0x9fe380cc); // 16.22% + InitDS_Sel(0x9fe3a04c); // 47.14% + InitDS_Sel(0xc8859060); // 13.85% + InitDS_Sel(0xc893814c); // 40.77% + InitDS_Sel(0xc9004060); // 6.15% + InitDS_Sel(0xcd078060); // 5.49% + InitDS_Sel(0xcd078360); // 9.27% + + // kuon + + InitDS_Sel(0x1fee0865); // 19.11% + InitDS_Sel(0x48860065); // 26.44% + InitDS_Sel(0x48860865); // 24.38% + InitDS_Sel(0x48868365); // 18.01% + InitDS_Sel(0x48868b65); // 31.33% + InitDS_Sel(0x488e0865); // 39.97% + InitDS_Sel(0x488e0b65); // 20.78% + InitDS_Sel(0x488e8b65); // 12.26% + InitDS_Sel(0x4c429065); // 25.89% + InitDS_Sel(0x4d068365); // 9.17% + InitDS_Sel(0xc847004d); // 13.35% + InitDS_Sel(0xc887004d); // 17.53% + InitDS_Sel(0xc8870065); // 28.03% + InitDS_Sel(0xc907004d); // 18.64% + InitDS_Sel(0xc9070065); // 5.21% + + // hxh + + InitDS_Sel(0x1fe04876); // 7.16% + InitDS_Sel(0x1fee8076); // 9.68% + InitDS_Sel(0x1fee8976); // 17.11% + InitDS_Sel(0x9fe04076); // 5.37% + InitDS_Sel(0x9fe79176); // 6.77% + InitDS_Sel(0xc8804076); // 6.64% + InitDS_Sel(0xc8838176); // 6.95% + InitDS_Sel(0xc8839176); // 6.85% + InitDS_Sel(0xc8878076); // 5.29% + + // grandia extreme + + InitDS_Sel(0x1fe3884c); // 27.03% + InitDS_Sel(0x45269070); // 5.74% + InitDS_Sel(0x452e9070); // 7.25% + InitDS_Sel(0x48868070); // 13.25% + InitDS_Sel(0x48869070); // 24.24% + InitDS_Sel(0x48878370); // 22.45% + InitDS_Sel(0x48879070); // 21.66% + InitDS_Sel(0x48879370); // 13.17% + InitDS_Sel(0x4888404c); // 12.23% + InitDS_Sel(0x48884050); // 15.68% + InitDS_Sel(0x488e8870); // 44.66% + InitDS_Sel(0x488e8b70); // 45.62% + InitDS_Sel(0x488e9370); // 14.64% + InitDS_Sel(0x9fe3934c); // 16.61% + InitDS_Sel(0xcc81934c); // 62.20% + + // enthusia + + InitDS_Sel(0x1fe04854); // 23.33% + InitDS_Sel(0x1fe60064); // 5.72% + InitDS_Sel(0x1fe60068); // 5.03% + InitDS_Sel(0x1fee0064); // 7.51% + InitDS_Sel(0x1fee0864); // 15.47% + InitDS_Sel(0x48860f64); // 9.63% + InitDS_Sel(0x48868f64); // 29.41% + InitDS_Sel(0x488e0f64); // 11.80% + InitDS_Sel(0x49068864); // 23.63% + InitDS_Sel(0x4b020864); // 13.40% + InitDS_Sel(0x4b060064); // 11.39% + InitDS_Sel(0x4b060864); // 8.38% + InitDS_Sel(0x4b068864); // 14.03% + InitDS_Sel(0x9fe79064); // 13.49% + InitDS_Sel(0xcd40404c); // 11.10% + + // ys 1/2 eternal story + + // bloody roar + + InitDS_Sel(0x48810068); // 25.54% + InitDS_Sel(0x48848068); // 68.60% + InitDS_Sel(0x488789e8); // 10.83% + InitDS_Sel(0x488791e8); // 11.18% + InitDS_Sel(0x49004068); // 9.51% + InitDS_Sel(0x49004868); // 6.95% + InitDS_Sel(0x49018368); // 15.01% + InitDS_Sel(0x49019368); // 13.90% + InitDS_Sel(0x4b068068); // 13.98% + InitDS_Sel(0x4b068868); // 14.29% + InitDS_Sel(0x4c469068); // 9.71% + + // ferrari f355 challenge + + InitDS_Sel(0x489e8064); // 9.07% + InitDS_Sel(0x489e8b64); // 8.01% + InitDS_Sel(0x5fe04064); // 10.07% + InitDS_Sel(0x5fe04068); // 13.01% + InitDS_Sel(0x5fe04868); // 6.33% + InitDS_Sel(0x5fe60064); // 10.58% + InitDS_Sel(0x5fee0064); // 14.14% + InitDS_Sel(0x5fee0864); // 21.09% + InitDS_Sel(0x5feeb864); // 7.78% + InitDS_Sel(0x5ff60064); // 16.67% + InitDS_Sel(0x5ffe0064); // 17.43% + InitDS_Sel(0x5ffe0864); // 23.98% + InitDS_Sel(0xc8858168); // 54.96% + InitDS_Sel(0xc890404c); // 6.30% + InitDS_Sel(0xdfef0064); // 8.68% + + // king of fighters xi + + InitDS_Sel(0x488589e8); // 110.21% + InitDS_Sel(0x488591e8); // 55.38% + InitDS_Sel(0xf4819050); // 12.12% + + // mana khemia + + InitDS_Sel(0x488e8369); // 96.11% + InitDS_Sel(0xc880404d); // 12.30% + InitDS_Sel(0xc881804d); // 16.48% + InitDS_Sel(0xc8819059); // 22.73% + InitDS_Sel(0xc885904d); // 12.36% + InitDS_Sel(0xc907804d); // 59.32% + InitDS_Sel(0xc90f8369); // 7.80% + + // ar tonelico 2 + + InitDS_Sel(0x484f8369); // 7.55% + InitDS_Sel(0x488e8069); // 22.13% + InitDS_Sel(0x488e9069); // 33.42% + InitDS_Sel(0x488e9369); // 103.56% + InitDS_Sel(0x488f8369); // 23.74% + InitDS_Sel(0x490f8369); // 29.15% + InitDS_Sel(0xc885804d); // 5.80% + InitDS_Sel(0xc887804d); // 7.94% + InitDS_Sel(0xc88f9369); // 13.74% + InitDS_Sel(0xc905904d); // 6.23% + + // rouge galaxy + + InitDS_Sel(0x1fe0484c); // 6.12% + InitDS_Sel(0x484e8164); // 53.09% + InitDS_Sel(0x48879054); // 47.81% + InitDS_Sel(0x488b0964); // 95.23% + InitDS_Sel(0x490e8164); // 24.34% + InitDS_Sel(0x490e9164); // 5.24% + InitDS_Sel(0xc8858154); // 18.49% + InitDS_Sel(0xdff0404c); // 7.47% + + // mobile suit gundam seed battle assault 3 + + InitDS_Sel(0x49004854); // 7.22% + InitDS_Sel(0x4c804054); // 12.92% + InitDS_Sel(0x5fee8074); // 14.87% + InitDS_Sel(0x5fee8874); // 58.09% + InitDS_Sel(0x5fee9174); // 5.15% + InitDS_Sel(0xc88390cc); // 21.42% + InitDS_Sel(0xc88791cc); // 6.20% + InitDS_Sel(0xc90781cc); // 12.73% + InitDS_Sel(0xcc81004d); // 11.15% + + // hajime no ippo all stars + + InitDS_Sel(0x48848868); // 7.31% + InitDS_Sel(0x48848b68); // 6.25% + InitDS_Sel(0x48858868); // 6.71% + InitDS_Sel(0x488c8b68); // 16.70% + + // hajime no ippo 2 + + InitDS_Sel(0x1fe04068); // 11.95% + InitDS_Sel(0x4881004c); // 20.37% + InitDS_Sel(0x48839368); // 31.79% + InitDS_Sel(0x48858068); // 55.12% + InitDS_Sel(0x48868b68); // 23.49% + InitDS_Sel(0x48878368); // 6.54% + InitDS_Sel(0x48879068); // 29.98% + InitDS_Sel(0x488c8068); // 10.13% + InitDS_Sel(0x488c8868); // 16.20% + InitDS_Sel(0x488e8b68); // 38.97% + InitDS_Sel(0x488e9068); // 7.20% + InitDS_Sel(0x488e9368); // 13.42% + InitDS_Sel(0x49028b68); // 48.16% + InitDS_Sel(0x4b0e8068); // 5.51% + InitDS_Sel(0x4b0e8868); // 14.85% + InitDS_Sel(0x4d0e8068); // 7.65% + InitDS_Sel(0xc8859068); // 21.31% + + // virtual tennis 2 + + InitDS_Sel(0x488049e5); // 5.52% + InitDS_Sel(0x48868075); // 28.20% + InitDS_Sel(0x48868875); // 20.93% + InitDS_Sel(0x4c8781f5); // 11.54% + InitDS_Sel(0x9540404d); // 6.25% + InitDS_Sel(0xc8859065); // 11.57% + + // crash wrath of cortex + + InitDS_Sel(0x1fe20864); // 15.87% + InitDS_Sel(0x48828764); // 9.14% + InitDS_Sel(0x48828f64); // 18.80% + InitDS_Sel(0x48838364); // 12.07% + InitDS_Sel(0x48838f64); // 5.84% + InitDS_Sel(0x49028f64); // 16.48% + InitDS_Sel(0x49038f64); // 34.44% + InitDS_Sel(0x9fe78064); // 11.75% + InitDS_Sel(0xc8818364); // 9.12% + InitDS_Sel(0xc9030064); // 100.90% + InitDS_Sel(0xca838364); // 8.86% + InitDS_Sel(0xcd05834c); // 8.90% + + // sbam 2 + + // remember 11 + + // prince of tennis + + InitDS_Sel(0x4888484c); // 5.28% + InitDS_Sel(0x488d8164); // 30.73% + InitDS_Sel(0x488d9164); // 23.46% + InitDS_Sel(0xc885914c); // 5.01% + InitDS_Sel(0xc8859164); // 7.79% + InitDS_Sel(0xc88d8164); // 5.33% + InitDS_Sel(0xc88d9164); // 26.18% + InitDS_Sel(0xc8958164); // 10.14% + InitDS_Sel(0xc89d814c); // 7.09% + InitDS_Sel(0xcd458064); // 13.67% + + // ar tonelico + + InitDS_Sel(0x48868369); // 17.29% + InitDS_Sel(0x48869369); // 16.05% + InitDS_Sel(0x488f9369); // 6.07% + InitDS_Sel(0x49078b69); // 54.61% + InitDS_Sel(0x490f8069); // 54.79% + InitDS_Sel(0x490f9369); // 5.20% + InitDS_Sel(0xc8804069); // 16.59% + InitDS_Sel(0xc8878069); // 32.09% + InitDS_Sel(0xc8878369); // 14.18% + InitDS_Sel(0xc8879069); // 31.83% + InitDS_Sel(0xc8879369); // 12.02% + InitDS_Sel(0xc9038069); // 245.53% + + // dbz sagas + + InitDS_Sel(0x48828964); // 38.72% + InitDS_Sel(0x488e9164); // 10.98% + InitDS_Sel(0x54229164); // 6.16% + + // tourist trophy + + InitDS_Sel(0x1fe84064); // 14.21% + InitDS_Sel(0x1fee8064); // 23.50% + InitDS_Sel(0x1fee9064); // 13.72% + InitDS_Sel(0x488a8064); // 10.03% + InitDS_Sel(0x488e8065); // 16.63% + InitDS_Sel(0x488e9065); // 16.00% + InitDS_Sel(0x5fe3804c); // 8.34% + InitDS_Sel(0x5fe3904c); // 7.65% + InitDS_Sel(0x5fee8164); // 7.66% + InitDS_Sel(0x9fe1904c); // 9.19% + InitDS_Sel(0x9fe1904d); // 6.10% + InitDS_Sel(0x9fe3804d); // 9.23% + InitDS_Sel(0x9fe5904d); // 7.20% + InitDS_Sel(0x9fe7804d); // 5.30% + InitDS_Sel(0x9fe7904d); // 5.67% + InitDS_Sel(0xc88181d4); // 11.57% + InitDS_Sel(0xc881904d); // 7.00% + InitDS_Sel(0xc88191d4); // 12.53% + InitDS_Sel(0xc887904d); // 5.13% + InitDS_Sel(0xcb03804c); // 21.06% + InitDS_Sel(0xcc81904d); // 13.48% + InitDS_Sel(0xcc83804d); // 10.28% + InitDS_Sel(0xcc83904c); // 34.42% + InitDS_Sel(0xcc83904d); // 10.24% + InitDS_Sel(0xcc87904d); // 33.01% + InitDS_Sel(0xcd05804c); // 17.24% + InitDS_Sel(0xd520404c); // 9.81% + InitDS_Sel(0xdfe1904c); // 7.52% + InitDS_Sel(0xdfe5804c); // 7.14% + + // svr2k8 + + InitDS_Sel(0x1fe79066); // 13.17% + InitDS_Sel(0x4880404c); // 7.49% + InitDS_Sel(0x4887804c); // 5.40% + InitDS_Sel(0x4887814c); // 9.08% + InitDS_Sel(0x488a0064); // 5.41% + InitDS_Sel(0x4900404c); // 12.53% + InitDS_Sel(0x490e8364); // 30.80% + InitDS_Sel(0x4c97b874); // 15.16% + InitDS_Sel(0xc887834c); // 7.67% + + // tokyo bus guide + + InitDS_Sel(0x1fe04070); // 13.22% + InitDS_Sel(0x1fe04870); // 13.56% + InitDS_Sel(0x1fee8170); // 10.70% + InitDS_Sel(0x1fee8970); // 60.13% + InitDS_Sel(0x1fee89f0); // 51.01% + InitDS_Sel(0x48804870); // 12.85% + InitDS_Sel(0x488a8850); // 21.74% + InitDS_Sel(0xc8804070); // 13.67% + InitDS_Sel(0xc8879070); // 35.52% + InitDS_Sel(0xc88791f0); // 18.61% + + // 12riven + + // xenosaga + + InitDS_Sel(0x1fe38054); // 67.22% + InitDS_Sel(0x1fe38074); // 25.86% + InitDS_Sel(0x48478164); // 32.61% + InitDS_Sel(0x48804964); // 7.12% + InitDS_Sel(0x49078164); // 25.05% + InitDS_Sel(0x4c818134); // 10.06% + InitDS_Sel(0x4d069064); // 98.01% + InitDS_Sel(0x4d084864); // 13.35% + InitDS_Sel(0x5fe04074); // 61.58% + InitDS_Sel(0x5fe68864); // 39.80% + InitDS_Sel(0x5fee8864); // 45.95% + InitDS_Sel(0x9fe04074); // 9.60% + InitDS_Sel(0x9fe1804c); // 9.55% + InitDS_Sel(0x9fe19074); // 16.03% + InitDS_Sel(0xc8819054); // 35.54% + InitDS_Sel(0xc8830074); // 47.20% + InitDS_Sel(0xc8858164); // 7.84% + InitDS_Sel(0xc9078164); // 154.58% + InitDS_Sel(0xcc819074); // 19.87% + InitDS_Sel(0xcd00404c); // 31.81% + InitDS_Sel(0xd5204064); // 14.68% + InitDS_Sel(0xdfe04074); // 14.26% + + // mgs3s1 + + InitDS_Sel(0x482e8864); // 18.14% + InitDS_Sel(0x484e8864); // 10.00% + InitDS_Sel(0x48868364); // 94.90% + InitDS_Sel(0x488e8364); // 15.82% + InitDS_Sel(0x488f8064); // 5.31% + InitDS_Sel(0x49268864); // 6.73% + InitDS_Sel(0x4b0e8864); // 8.35% + InitDS_Sel(0x4b0e8b64); // 6.23% + InitDS_Sel(0x9fe1804d); // 9.13% + InitDS_Sel(0x9fe7904c); // 11.17% + InitDS_Sel(0xc8804364); // 5.24% + InitDS_Sel(0xc8819364); // 19.10% + InitDS_Sel(0xc883004c); // 17.25% + InitDS_Sel(0xc8878364); // 12.09% + InitDS_Sel(0xc8879064); // 22.53% + InitDS_Sel(0xcc830074); // 54.93% + InitDS_Sel(0xcd079364); // 5.24% + InitDS_Sel(0xcd404074); // 9.03% + + // god of war + + // gta sa + + InitDS_Sel(0x1fe84864); // 6.23% + InitDS_Sel(0x48810064); // 14.67% + InitDS_Sel(0x49004054); // 16.40% + InitDS_Sel(0x4d07804c); // 15.09% + InitDS_Sel(0x4d078864); // 9.54% + InitDS_Sel(0x4d0c8864); // 8.71% + + // haunting ground + + InitDS_Sel(0x1fe68864); // 38.74% + InitDS_Sel(0x1fe689e4); // 44.79% + InitDS_Sel(0x488689e4); // 5.03% + InitDS_Sel(0xc843904c); // 16.66% + InitDS_Sel(0xc88791e4); // 22.86% + InitDS_Sel(0xcc23804c); // 16.80% + InitDS_Sel(0xcd01904c); // 82.27% + InitDS_Sel(0xdfe04064); // 14.73% + InitDS_Sel(0xdff1904e); // 14.38% + + // odin sphere + + InitDS_Sel(0x4880404d); // 7.35% + InitDS_Sel(0x4885804d); // 6.02% + InitDS_Sel(0x4885904c); // 11.56% + InitDS_Sel(0x488f904c); // 6.59% + InitDS_Sel(0x488f904d); // 6.69% + InitDS_Sel(0x4907804d); // 5.71% + InitDS_Sel(0x4907884d); // 9.45% + InitDS_Sel(0xc881904c); // 5.36% + + // kingdom hearts re:com + + InitDS_Sel(0x1fe04078); // 9.88% + InitDS_Sel(0x1fee884c); // 17.61% + InitDS_Sel(0x4880494c); // 6.46% + InitDS_Sel(0x48868974); // 11.43% + InitDS_Sel(0x488a8164); // 6.28% + InitDS_Sel(0x488e804c); // 6.41% + InitDS_Sel(0x488e874c); // 6.02% + InitDS_Sel(0x4906814c); // 33.35% + InitDS_Sel(0x4906894c); // 44.97% + InitDS_Sel(0x49068974); // 15.85% + InitDS_Sel(0x4907894c); // 16.25% + InitDS_Sel(0x49078974); // 6.45% + InitDS_Sel(0x9fe10054); // 14.10% + InitDS_Sel(0x9fe3814d); // 14.12% + InitDS_Sel(0x9fe3914d); // 17.86% + InitDS_Sel(0xc885814c); // 7.69% + InitDS_Sel(0xc907804c); // 8.56% + InitDS_Sel(0xcd404064); // 9.28% + InitDS_Sel(0xd120404c); // 9.47% + + // hyper street fighter 2 anniversary edition + + // star ocean 3 + + InitDS_Sel(0x49068164); // 57.66% + InitDS_Sel(0x9fe30064); // 24.77% + InitDS_Sel(0x9fe38065); // 28.18% + InitDS_Sel(0xc8839164); // 37.72% + InitDS_Sel(0xc920404c); // 11.35% + + // aura for laura + + InitDS_Sel(0x1fe38070); // 31.10% + InitDS_Sel(0x1fe3904c); // 19.62% + InitDS_Sel(0x1fe39050); // 5.19% + InitDS_Sel(0x1fe68070); // 19.79% + InitDS_Sel(0x1fe78070); // 112.75% + InitDS_Sel(0x1fe79070); // 65.97% + InitDS_Sel(0x1fefb04c); // 17.85% + InitDS_Sel(0x9fe38050); // 11.45% + InitDS_Sel(0x9fe39050); // 11.01% + InitDS_Sel(0x9fe78050); // 12.31% + InitDS_Sel(0xc523804c); // 106.80% + InitDS_Sel(0xc8839050); // 14.31% + InitDS_Sel(0xc9038050); // 6.72% + InitDS_Sel(0xc9058050); // 291.44% + InitDS_Sel(0xcc818050); // 49.07% + InitDS_Sel(0xd5204050); // 7.22% + + #endif +} + +IDrawScanline::DrawScanlinePtr GSDrawScanline::GSDrawScanlineMap::GetDefaultFunction(DWORD dw) +{ + GSScanlineSelector sel; + + sel.dw = dw; + + return m_default[sel.fpsm][sel.zpsm][sel.ztst][sel.iip]; +} + +void GSDrawScanline::GSDrawScanlineMap::PrintStats() +{ + __super::PrintStats(); + + if(FILE* fp = fopen("c:\\1.txt", "w")) + { + POSITION pos = m_map_active.GetHeadPosition(); + + while(pos) + { + DWORD dw; + ActivePtr* p; + + m_map_active.GetNextAssoc(pos, dw, p); + + if(m_map.Lookup(dw)) + { + continue; + } + + GSScanlineSelector sel; + + sel.dw = dw; + + if(p->frames > 30 && !sel.IsSolidRect()) + { + int tpf = (int)((p->ticks / p->frames) * 10000 / (3000000000 / 60)); // 3 GHz, 60 fps + + if(tpf >= 500) + { + _ftprintf(fp, _T("InitDS_Sel(0x%08x); // %6.2f%%\n"), sel.dw, (float)tpf / 100); + } + } + } + + fclose(fp); + } +} + +// + +GSDrawScanline::GSSetupPrimMap::GSSetupPrimMap() +{ + #define InitSP_IIP(zbe, fge, tme, fst, iip) \ + m_default[zbe][fge][tme][fst][iip] = (SetupPrimPtr)&GSDrawScanline::SetupPrim; \ + + #define InitSP_FST(zbe, fge, tme, fst) \ + InitSP_IIP(zbe, fge, tme, fst, 0) \ + InitSP_IIP(zbe, fge, tme, fst, 1) \ + + #define InitSP_TME(zbe, fge, tme) \ + InitSP_FST(zbe, fge, tme, 0) \ + InitSP_FST(zbe, fge, tme, 1) \ + + #define InitSP_FGE(zbe, fge) \ + InitSP_TME(zbe, fge, 0) \ + InitSP_TME(zbe, fge, 1) \ + + #define InitSP_ZBE(zbe) \ + InitSP_FGE(zbe, 0) \ + InitSP_FGE(zbe, 1) \ + + InitSP_ZBE(0); + InitSP_ZBE(1); +} + +IDrawScanline::SetupPrimPtr GSDrawScanline::GSSetupPrimMap::GetDefaultFunction(DWORD dw) +{ + DWORD zbe = (dw >> 0) & 1; + DWORD fge = (dw >> 1) & 1; + DWORD tme = (dw >> 2) & 1; + DWORD fst = (dw >> 3) & 1; + DWORD iip = (dw >> 4) & 1; + + return m_default[zbe][fge][tme][fst][iip]; +} + +// + +const GSVector4 GSDrawScanline::m_shift[4] = +{ + GSVector4(0.0f, 1.0f, 2.0f, 3.0f), + GSVector4(-1.0f, 0.0f, 1.0f, 2.0f), + GSVector4(-2.0f, -1.0f, 0.0f, 1.0f), + GSVector4(-3.0f, -2.0f, -1.0f, 0.0f), +}; + +const GSVector4i GSDrawScanline::m_test[8] = +{ + GSVector4i::zero(), + GSVector4i(0xffffffff, 0x00000000, 0x00000000, 0x00000000), + GSVector4i(0xffffffff, 0xffffffff, 0x00000000, 0x00000000), + GSVector4i(0xffffffff, 0xffffffff, 0xffffffff, 0x00000000), + GSVector4i(0x00000000, 0xffffffff, 0xffffffff, 0xffffffff), + GSVector4i(0x00000000, 0x00000000, 0xffffffff, 0xffffffff), + GSVector4i(0x00000000, 0x00000000, 0x00000000, 0xffffffff), + GSVector4i::zero(), +}; diff --git a/plugins/GSdx/GSDrawScanline.h b/plugins/GSdx/GSDrawScanline.h new file mode 100644 index 0000000000..8d63a29191 --- /dev/null +++ b/plugins/GSdx/GSDrawScanline.h @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSState.h" +#include "GSRasterizer.h" +#include "GSAlignedClass.h" + +union GSScanlineSelector +{ + struct + { + DWORD fpsm:2; // 0 + DWORD zpsm:2; // 2 + DWORD ztst:2; // 4 (0: off, 1: write, 2: test (ge), 3: test (g)) + DWORD atst:3; // 6 + DWORD afail:2; // 9 + DWORD iip:1; // 11 + DWORD tfx:3; // 12 + DWORD tcc:1; // 15 + DWORD fst:1; // 16 + DWORD ltf:1; // 17 + DWORD tlu:1; // 18 + DWORD fge:1; // 19 + DWORD date:1; // 20 + DWORD abea:2; // 21 + DWORD abeb:2; // 23 + DWORD abec:2; // 25 + DWORD abed:2; // 27 + DWORD pabe:1; // 29 + DWORD rfb:1; // 30 + DWORD sprite:1; // 31 + }; + + struct + { + DWORD _pad1:21; + DWORD abe:8; + DWORD _pad2:3; + }; + + DWORD dw; + + operator DWORD() {return dw;} + + bool IsSolidRect() + { + return sprite + && iip == 0 + && tfx == TFX_NONE + && abe == 255 + && ztst <= 1 + && atst <= 1 + && date == 0 + && fge == 0; + } +}; + +__declspec(align(16)) struct GSScanlineEnvironment +{ + GSScanlineSelector sel; + + void* vm; + const void* tex; + const DWORD* clut; + DWORD tw; + + GSVector4i* fbr; + GSVector4i* zbr; + int** fbc; + int** zbc; + GSVector2i* fzbr; + GSVector2i* fzbc; + + GSVector4i fm, zm; + struct {GSVector4i min, max, mask;} t; // [u] x 4 [v] x 4 + GSVector4i datm; + GSVector4i colclamp; + GSVector4i fba; + GSVector4i aref; + GSVector4i afix, afix2; + GSVector4i frb, fga; + + struct {GSVector4 z, s, t, q; GSVector4i rb, ga, f, si, ti, _pad[3];} d[4]; + struct {GSVector4 z, stq; GSVector4i c, f, st;} d4; + struct {GSVector4i rb, ga;} c; + struct {GSVector4i z, f;} p; + struct {GSVector4i rb, ga;} c2; +}; + +__declspec(align(16)) struct GSScanlineParam +{ + GSScanlineSelector sel; + + void* vm; + const void* tex; + const DWORD* clut; + DWORD tw; + + GSLocalMemory::Offset* fbo; + GSLocalMemory::Offset* zbo; + GSLocalMemory::Offset4* fzbo; + + DWORD fm, zm; +}; + +class GSDrawScanline : public GSAlignedClass<16>, public IDrawScanline +{ + GSScanlineEnvironment m_env; + + static const GSVector4 m_shift[4]; + static const GSVector4i m_test[8]; + + // + + class GSDrawScanlineMap : public GSFunctionMap + { + DrawScanlinePtr m_default[4][4][4][2]; + + public: + GSDrawScanlineMap(); + + DrawScanlinePtr GetDefaultFunction(DWORD dw); + + void PrintStats(); + }; + + GSDrawScanlineMap m_ds; + + // + + class GSSetupPrimMap : public GSFunctionMap + { + SetupPrimPtr m_default[2][2][2][2][2]; + + public: + GSSetupPrimMap(); + + SetupPrimPtr GetDefaultFunction(DWORD dw); + }; + + GSSetupPrimMap m_sp; + + // + + template + void SetupPrim(const GSVertexSW* vertices, const GSVertexSW& dscan); + + // + + __forceinline GSVector4i Wrap(const GSVector4i& t); + + __forceinline void SampleTexture(DWORD ltf, DWORD tlu, const GSVector4i& u, const GSVector4i& v, GSVector4i* c); + __forceinline void ColorTFX(DWORD iip, DWORD tfx, const GSVector4i& rbf, const GSVector4i& gaf, GSVector4i& rbt, GSVector4i& gat); + __forceinline void AlphaTFX(DWORD iip, DWORD tfx, DWORD tcc, const GSVector4i& gaf, GSVector4i& gat); + __forceinline void Fog(DWORD fge, const GSVector4i& f, GSVector4i& rb, GSVector4i& ga); + __forceinline bool TestZ(DWORD zpsm, DWORD ztst, const GSVector4i& zs, const GSVector4i& zd, GSVector4i& test); + __forceinline bool TestAlpha(DWORD atst, DWORD afail, const GSVector4i& ga, GSVector4i& fm, GSVector4i& zm, GSVector4i& test); + __forceinline bool TestDestAlpha(DWORD fpsm, DWORD date, const GSVector4i& fd, GSVector4i& test); + + __forceinline void ReadPixel(int psm, int addr, GSVector4i& c) const; + __forceinline static void WritePixel(int psm, WORD* RESTRICT vm16, DWORD c); + __forceinline void WriteFrame(int fpsm, int rfb, GSVector4i* c, const GSVector4i& fd, const GSVector4i& fm, int addr, int fzm); + __forceinline void WriteZBuf(int zpsm, int ztst, const GSVector4i& z, const GSVector4i& zd, const GSVector4i& zm, int addr, int fzm); + + template + void DrawScanline(int top, int left, int right, const GSVertexSW& v); + + template + void DrawScanlineEx(int top, int left, int right, const GSVertexSW& v); + + // + + void DrawSolidRect(const GSVector4i& r, const GSVertexSW& v); + + template + void DrawSolidRectT(const GSVector4i* row, int* col, const GSVector4i& r, DWORD c, DWORD m); + + template + __forceinline void FillRect(const GSVector4i* row, int* col, const GSVector4i& r, DWORD c, DWORD m); + + template + __forceinline void FillBlock(const GSVector4i* row, int* col, const GSVector4i& r, const GSVector4i& c, const GSVector4i& m); + +protected: + GSState* m_state; + int m_id; + +public: + GSDrawScanline(GSState* state, int id); + virtual ~GSDrawScanline(); + + // IDrawScanline + + void BeginDraw(const GSRasterizerData* data, Functions* f); + void EndDraw(const GSRasterizerStats& stats); + void PrintStats() {m_ds.PrintStats();} +}; diff --git a/plugins/GSdx/GSDrawingContext.h b/plugins/GSdx/GSDrawingContext.h new file mode 100644 index 0000000000..86cfae4855 --- /dev/null +++ b/plugins/GSdx/GSDrawingContext.h @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSLocalMemory.h" + +#pragma pack(push, 1) + +__declspec(align(16)) class GSDrawingContext +{ +public: + GIFRegXYOFFSET XYOFFSET; + GIFRegTEX0 TEX0; + GIFRegTEX1 TEX1; + GIFRegTEX2 TEX2; + GIFRegCLAMP CLAMP; + GIFRegMIPTBP1 MIPTBP1; + GIFRegMIPTBP2 MIPTBP2; + GIFRegSCISSOR SCISSOR; + GIFRegALPHA ALPHA; + GIFRegTEST TEST; + GIFRegFBA FBA; + GIFRegFRAME FRAME; + GIFRegZBUF ZBUF; + + __declspec(align(16)) struct + { + GSVector4i dx10; + GSVector4 dx9; + GSVector4 in; + GSVector4 ex; + } scissor; + + GSDrawingContext() + { + Reset(); + } + + void Reset() + { + memset(&XYOFFSET, 0, sizeof(XYOFFSET)); + memset(&TEX0, 0, sizeof(TEX0)); + memset(&TEX1, 0, sizeof(TEX1)); + memset(&TEX2, 0, sizeof(TEX2)); + memset(&CLAMP, 0, sizeof(CLAMP)); + memset(&MIPTBP1, 0, sizeof(MIPTBP1)); + memset(&MIPTBP2, 0, sizeof(MIPTBP2)); + memset(&SCISSOR, 0, sizeof(SCISSOR)); + memset(&ALPHA, 0, sizeof(ALPHA)); + memset(&TEST, 0, sizeof(TEST)); + memset(&FBA, 0, sizeof(FBA)); + memset(&FRAME, 0, sizeof(FRAME)); + memset(&ZBUF, 0, sizeof(ZBUF)); + } + + void UpdateScissor() + { + scissor.dx10 = GSVector4i( + (int)((SCISSOR.SCAX0 << 4) + XYOFFSET.OFX), + (int)((SCISSOR.SCAY0 << 4) + XYOFFSET.OFY), + (int)((SCISSOR.SCAX1 << 4) + XYOFFSET.OFX), + (int)((SCISSOR.SCAY1 << 4) + XYOFFSET.OFY)); + + scissor.dx9 = GSVector4(scissor.dx10); + + scissor.in = GSVector4( + (int)SCISSOR.SCAX0, + (int)SCISSOR.SCAY0, + (int)SCISSOR.SCAX1 + 1, + (int)SCISSOR.SCAY1 + 1); + + scissor.ex = GSVector4i( + (int)SCISSOR.SCAX0, + (int)SCISSOR.SCAY0, + (int)SCISSOR.SCAX1, + (int)SCISSOR.SCAY1); + } + + bool DepthRead() const + { + return TEST.ZTE && TEST.ZTST >= 2; + } + + bool DepthWrite() const + { + if(TEST.ATE && TEST.ATST == ATST_NEVER && TEST.AFAIL != AFAIL_ZB_ONLY) // alpha test, all pixels fail, z buffer is not updated + { + return false; + } + + return ZBUF.ZMSK == 0 && TEST.ZTE != 0; // ZTE == 0 is bug on the real hardware, write is blocked then + } +}; + +#pragma pack(pop) \ No newline at end of file diff --git a/plugins/GSdx/GSDrawingEnvironment.h b/plugins/GSdx/GSDrawingEnvironment.h new file mode 100644 index 0000000000..2ac74cdb3c --- /dev/null +++ b/plugins/GSdx/GSDrawingEnvironment.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" + +#pragma pack(push, 1) + +__declspec(align(16)) class GSDrawingEnvironment +{ +public: + GIFRegPRIM PRIM; + GIFRegPRMODE PRMODE; + GIFRegPRMODECONT PRMODECONT; + GIFRegTEXCLUT TEXCLUT; + GIFRegSCANMSK SCANMSK; + GIFRegTEXA TEXA; + GIFRegFOGCOL FOGCOL; + GIFRegDIMX DIMX; + GIFRegDTHE DTHE; + GIFRegCOLCLAMP COLCLAMP; + GIFRegPABE PABE; + GIFRegBITBLTBUF BITBLTBUF; + GIFRegTRXDIR TRXDIR; + GIFRegTRXPOS TRXPOS; + GIFRegTRXREG TRXREG; + GIFRegTRXREG TRXREG2; + GSDrawingContext CTXT[2]; + + GSDrawingEnvironment() + { + } + + void Reset() + { + memset(&PRIM, 0, sizeof(PRIM)); + memset(&PRMODE, 0, sizeof(PRMODE)); + memset(&PRMODECONT, 0, sizeof(PRMODECONT)); + memset(&TEXCLUT, 0, sizeof(TEXCLUT)); + memset(&SCANMSK, 0, sizeof(SCANMSK)); + memset(&TEXA, 0, sizeof(TEXA)); + memset(&FOGCOL, 0, sizeof(FOGCOL)); + memset(&DIMX, 0, sizeof(DIMX)); + memset(&DTHE, 0, sizeof(DTHE)); + memset(&COLCLAMP, 0, sizeof(COLCLAMP)); + memset(&PABE, 0, sizeof(PABE)); + memset(&BITBLTBUF, 0, sizeof(BITBLTBUF)); + memset(&TRXDIR, 0, sizeof(TRXDIR)); + memset(&TRXPOS, 0, sizeof(TRXPOS)); + memset(&TRXREG, 0, sizeof(TRXREG)); + memset(&TRXREG2, 0, sizeof(TRXREG2)); + + CTXT[0].Reset(); + CTXT[1].Reset(); + } +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GSDump.cpp b/plugins/GSdx/GSDump.cpp new file mode 100644 index 0000000000..050365f498 --- /dev/null +++ b/plugins/GSdx/GSDump.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSDump.h" + +GSDump::GSDump() + : m_fp(NULL) +{ +} + +GSDump::~GSDump() +{ + if(m_fp) + { + fclose(m_fp); + } +} + +void GSDump::Open(LPCTSTR fn, DWORD crc, const GSFreezeData& fd, const void* regs) +{ + m_fp = _tfopen(fn, _T("wb")); + m_vsyncs = 0; + + if(m_fp) + { + fwrite(&crc, 4, 1, m_fp); + fwrite(&fd.size, 4, 1, m_fp); + fwrite(fd.data, fd.size, 1, m_fp); + fwrite(regs, 0x2000, 1, m_fp); + } +} + +void GSDump::Transfer(int index, BYTE* mem, size_t size) +{ + if(m_fp && size > 0) + { + fputc(0, m_fp); + fputc(index, m_fp); + fwrite(&size, 4, 1, m_fp); + fwrite(mem, size, 1, m_fp); + } +} + +void GSDump::ReadFIFO(UINT32 size) +{ + if(m_fp && size > 0) + { + fputc(2, m_fp); + fwrite(&size, 4, 1, m_fp); + } +} + +void GSDump::VSync(int field, bool last, const void* regs) +{ + if(m_fp) + { + fputc(3, m_fp); + fwrite(regs, 0x2000, 1, m_fp); + + fputc(1, m_fp); + fputc(field, m_fp); + + if((++m_vsyncs & 1) == 0 && last) + { + fclose(m_fp); + m_fp = NULL; + } + } +} diff --git a/plugins/GSdx/GSDump.h b/plugins/GSdx/GSDump.h new file mode 100644 index 0000000000..da6cb74898 --- /dev/null +++ b/plugins/GSdx/GSDump.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" + +/* + +Dump file format: +- [crc/4] [state size/4] [state data/size] [PMODE/0x2000] [id/1] [data/?] .. [id/1] [data/?] + +Transfer data (id == 0) +- [0/1] [path index/1] [size/4] [data/size] + +VSync data (id == 1) +- [1/1] [field/1] + +ReadFIFO2 data (id == 2) +- [2/1] [size/?] + +Regs data (id == 3) +- [PMODE/0x2000] + +*/ + +class GSDump +{ + FILE* m_fp; + int m_vsyncs; + +public: + GSDump(); + virtual ~GSDump(); + + void Open(LPCTSTR fn, DWORD crc, const GSFreezeData& fd, const void* regs); + void ReadFIFO(UINT32 size); + void Transfer(int index, BYTE* mem, size_t size); + void VSync(int field, bool last, const void* regs); + operator bool() {return m_fp != NULL;} +}; diff --git a/plugins/GSdx/GSFunctionMap.cpp b/plugins/GSdx/GSFunctionMap.cpp new file mode 100644 index 0000000000..37cd38e946 --- /dev/null +++ b/plugins/GSdx/GSFunctionMap.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSFunctionMap.h" + diff --git a/plugins/GSdx/GSFunctionMap.h b/plugins/GSdx/GSFunctionMap.h new file mode 100644 index 0000000000..8867311ebc --- /dev/null +++ b/plugins/GSdx/GSFunctionMap.h @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" + +struct GSRasterizerStats +{ + __int64 ticks; + int prims, pixels; + + GSRasterizerStats() + { + Reset(); + } + + void Reset() + { + ticks = 0; + pixels = prims = 0; + } +}; + +template class GSFunctionMap +{ +protected: + struct ActivePtr + { + UINT64 frame, frames; + __int64 ticks, pixels; + T f; + }; + + CRBMap m_map; + CRBMap m_map_active; + ActivePtr* m_active; + + virtual T GetDefaultFunction(DWORD sel) = 0; + +public: + GSFunctionMap() + : m_active(NULL) + { + } + + virtual ~GSFunctionMap() + { + POSITION pos = m_map_active.GetHeadPosition(); + + while(pos) + { + delete m_map_active.GetNextValue(pos); + } + + m_map_active.RemoveAll(); + } + + void SetAt(DWORD sel, T f) + { + m_map.SetAt(sel, f); + } + + T Lookup(DWORD sel) + { + m_active = NULL; + + if(!m_map_active.Lookup(sel, m_active)) + { + CRBMap::CPair* pair = m_map.Lookup(sel); + + ActivePtr* p = new ActivePtr(); + + memset(p, 0, sizeof(*p)); + + p->frame = (UINT64)-1; + + p->f = pair ? pair->m_value : GetDefaultFunction(sel); + + m_map_active.SetAt(sel, p); + + m_active = p; + } + + return m_active->f; + } + + void UpdateStats(const GSRasterizerStats& stats, UINT64 frame) + { + if(m_active) + { + if(m_active->frame != frame) + { + m_active->frame = frame; + m_active->frames++; + } + + m_active->pixels += stats.pixels; + m_active->ticks += stats.ticks; + } + } + + virtual void PrintStats() + { + __int64 ttpf = 0; + + POSITION pos = m_map_active.GetHeadPosition(); + + while(pos) + { + ActivePtr* p = m_map_active.GetNextValue(pos); + + if(p->frames) + { + ttpf += p->ticks / p->frames; + } + } + + pos = m_map_active.GetHeadPosition(); + + while(pos) + { + DWORD sel; + ActivePtr* p; + + m_map_active.GetNextAssoc(pos, sel, p); + + if(p->frames > 0) + { + __int64 tpp = p->pixels > 0 ? p->ticks / p->pixels : 0; + __int64 tpf = p->frames > 0 ? p->ticks / p->frames : 0; + __int64 ppf = p->frames > 0 ? p->pixels / p->frames : 0; + + printf("[%08x]%c %6.2f%% | %5.2f%% | f %4I64d | p %10I64d | tpp %4I64d | tpf %9I64d | ppf %7I64d\n", + sel, !m_map.Lookup(sel) ? '*' : ' ', + (float)(tpf * 10000 / 50000000) / 100, + (float)(tpf * 10000 / ttpf) / 100, + p->frames, p->pixels, + tpp, tpf, ppf); + } + } + } +}; diff --git a/plugins/GSdx/GSLocalMemory.cpp b/plugins/GSdx/GSLocalMemory.cpp new file mode 100644 index 0000000000..80c66b6c7e --- /dev/null +++ b/plugins/GSdx/GSLocalMemory.cpp @@ -0,0 +1,2399 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * Special Notes: + * + * Based on Page.c from GSSoft + * Copyright (C) 2002-2004 GSsoft Team + * + */ + +#include "StdAfx.h" +#include "GSLocalMemory.h" + +#define ASSERT_BLOCK(r, w, h) \ + ASSERT((r).Width() >= w && (r).Height() >= h && !((r).left&(w-1)) && !((r).top&(h-1)) && !((r).right&(w-1)) && !((r).bottom&(h-1))); \ + +#define FOREACH_BLOCK_START(w, h, bpp) \ + DWORD bp = TEX0.TBP0; \ + DWORD bw = TEX0.TBW; \ + int offset = dstpitch * h - (r.right - r.left) * bpp / 8; \ + for(int y = r.top; y < r.bottom; y += h, dst += offset) \ + { ASSERT_BLOCK(r, w, h); \ + for(int x = r.left; x < r.right; x += w, dst += w * bpp / 8) \ + { \ + +#define FOREACH_BLOCK_END }} + +// + +DWORD GSLocalMemory::pageOffset32[32][32][64]; +DWORD GSLocalMemory::pageOffset32Z[32][32][64]; +DWORD GSLocalMemory::pageOffset16[32][64][64]; +DWORD GSLocalMemory::pageOffset16S[32][64][64]; +DWORD GSLocalMemory::pageOffset16Z[32][64][64]; +DWORD GSLocalMemory::pageOffset16SZ[32][64][64]; +DWORD GSLocalMemory::pageOffset8[32][64][128]; +DWORD GSLocalMemory::pageOffset4[32][128][128]; + +int GSLocalMemory::rowOffset32[2048]; +int GSLocalMemory::rowOffset32Z[2048]; +int GSLocalMemory::rowOffset16[2048]; +int GSLocalMemory::rowOffset16S[2048]; +int GSLocalMemory::rowOffset16Z[2048]; +int GSLocalMemory::rowOffset16SZ[2048]; +int GSLocalMemory::rowOffset8[2][2048]; +int GSLocalMemory::rowOffset4[2][2048]; + +// + +DWORD GSLocalMemory::m_xtbl[1024]; +DWORD GSLocalMemory::m_ytbl[1024]; + +// + +GSLocalMemory::psm_t GSLocalMemory::m_psm[64]; + +// + +GSLocalMemory::GSLocalMemory() + : m_clut(this) +{ + m_vm8 = (BYTE*)VirtualAlloc(NULL, m_vmsize * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + + memset(m_vm8, 0, m_vmsize); + + for(int bp = 0; bp < 32; bp++) + { + for(int y = 0; y < 32; y++) for(int x = 0; x < 64; x++) + { + pageOffset32[bp][y][x] = PixelAddressOrg32(x, y, bp, 0); + pageOffset32Z[bp][y][x] = PixelAddressOrg32Z(x, y, bp, 0); + } + + for(int y = 0; y < 64; y++) for(int x = 0; x < 64; x++) + { + pageOffset16[bp][y][x] = PixelAddressOrg16(x, y, bp, 0); + pageOffset16S[bp][y][x] = PixelAddressOrg16S(x, y, bp, 0); + pageOffset16Z[bp][y][x] = PixelAddressOrg16Z(x, y, bp, 0); + pageOffset16SZ[bp][y][x] = PixelAddressOrg16SZ(x, y, bp, 0); + } + + for(int y = 0; y < 64; y++) for(int x = 0; x < 128; x++) + { + pageOffset8[bp][y][x] = PixelAddressOrg8(x, y, bp, 0); + } + + for(int y = 0; y < 128; y++) for(int x = 0; x < 128; x++) + { + pageOffset4[bp][y][x] = PixelAddressOrg4(x, y, bp, 0); + } + } + + for(int x = 0; x < countof(rowOffset32); x++) + { + rowOffset32[x] = (int)PixelAddress32(x, 0, 0, 32) - (int)PixelAddress32(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset32Z); x++) + { + rowOffset32Z[x] = (int)PixelAddress32Z(x, 0, 0, 32) - (int)PixelAddress32Z(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset16); x++) + { + rowOffset16[x] = (int)PixelAddress16(x, 0, 0, 32) - (int)PixelAddress16(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset16S); x++) + { + rowOffset16S[x] = (int)PixelAddress16S(x, 0, 0, 32) - (int)PixelAddress16S(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset16Z); x++) + { + rowOffset16Z[x] = (int)PixelAddress16Z(x, 0, 0, 32) - (int)PixelAddress16Z(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset16SZ); x++) + { + rowOffset16SZ[x] = (int)PixelAddress16SZ(x, 0, 0, 32) - (int)PixelAddress16SZ(0, 0, 0, 32); + } + + for(int x = 0; x < countof(rowOffset8[0]); x++) + { + rowOffset8[0][x] = (int)PixelAddress8(x, 0, 0, 32) - (int)PixelAddress8(0, 0, 0, 32), + rowOffset8[1][x] = (int)PixelAddress8(x, 2, 0, 32) - (int)PixelAddress8(0, 2, 0, 32); + } + + for(int x = 0; x < countof(rowOffset4[0]); x++) + { + rowOffset4[0][x] = (int)PixelAddress4(x, 0, 0, 32) - (int)PixelAddress4(0, 0, 0, 32), + rowOffset4[1][x] = (int)PixelAddress4(x, 2, 0, 32) - (int)PixelAddress4(0, 2, 0, 32); + } + + for(int i = 0; i < countof(m_psm); i++) + { + m_psm[i].pa = &GSLocalMemory::PixelAddress32; + m_psm[i].ba = &GSLocalMemory::BlockAddress32; + m_psm[i].pga = &GSLocalMemory::PageAddress32; + m_psm[i].pgn = &GSLocalMemory::PageNumber32; + m_psm[i].rp = &GSLocalMemory::ReadPixel32; + m_psm[i].rpa = &GSLocalMemory::ReadPixel32; + m_psm[i].wp = &GSLocalMemory::WritePixel32; + m_psm[i].wpa = &GSLocalMemory::WritePixel32; + m_psm[i].rt = &GSLocalMemory::ReadTexel32; + m_psm[i].rtNP = &GSLocalMemory::ReadTexel32; + m_psm[i].rta = &GSLocalMemory::ReadTexel32; + m_psm[i].wfa = &GSLocalMemory::WritePixel32; + m_psm[i].wi = &GSLocalMemory::WriteImage; + m_psm[i].ri = &GSLocalMemory::ReadImageX; // TODO + m_psm[i].rtx = &GSLocalMemory::ReadTexture32; + m_psm[i].rtxNP = &GSLocalMemory::ReadTexture32; + m_psm[i].rtxP = &GSLocalMemory::ReadTexture32; + m_psm[i].bpp = m_psm[i].trbpp = 32; + m_psm[i].pal = 0; + m_psm[i].bs = CSize(8, 8); + m_psm[i].pgs = CSize(64, 32); + for(int j = 0; j < 8; j++) m_psm[i].rowOffset[j] = rowOffset32; + } + + m_psm[PSM_PSMCT16].pa = &GSLocalMemory::PixelAddress16; + m_psm[PSM_PSMCT16S].pa = &GSLocalMemory::PixelAddress16S; + m_psm[PSM_PSMT8].pa = &GSLocalMemory::PixelAddress8; + m_psm[PSM_PSMT4].pa = &GSLocalMemory::PixelAddress4; + m_psm[PSM_PSMZ32].pa = &GSLocalMemory::PixelAddress32Z; + m_psm[PSM_PSMZ24].pa = &GSLocalMemory::PixelAddress32Z; + m_psm[PSM_PSMZ16].pa = &GSLocalMemory::PixelAddress16Z; + m_psm[PSM_PSMZ16S].pa = &GSLocalMemory::PixelAddress16SZ; + + m_psm[PSM_PSMCT16].ba = &GSLocalMemory::BlockAddress16; + m_psm[PSM_PSMCT16S].ba = &GSLocalMemory::BlockAddress16S; + m_psm[PSM_PSMT8].ba = &GSLocalMemory::BlockAddress8; + m_psm[PSM_PSMT4].ba = &GSLocalMemory::BlockAddress4; + m_psm[PSM_PSMZ32].ba = &GSLocalMemory::BlockAddress32Z; + m_psm[PSM_PSMZ24].ba = &GSLocalMemory::BlockAddress32Z; + m_psm[PSM_PSMZ16].ba = &GSLocalMemory::BlockAddress16Z; + m_psm[PSM_PSMZ16S].ba = &GSLocalMemory::BlockAddress16SZ; + + m_psm[PSM_PSMCT16].pga = &GSLocalMemory::PageAddress16; + m_psm[PSM_PSMCT16S].pga = &GSLocalMemory::PageAddress16; + m_psm[PSM_PSMZ16].pga = &GSLocalMemory::PageAddress16; + m_psm[PSM_PSMZ16S].pga = &GSLocalMemory::PageAddress16; + m_psm[PSM_PSMT8].pga = &GSLocalMemory::PageAddress8; + m_psm[PSM_PSMT4].pga = &GSLocalMemory::PageAddress4; + + m_psm[PSM_PSMCT16].pgn = &GSLocalMemory::PageNumber16; + m_psm[PSM_PSMCT16S].pgn = &GSLocalMemory::PageNumber16; + m_psm[PSM_PSMZ16].pgn = &GSLocalMemory::PageNumber16; + m_psm[PSM_PSMZ16S].pgn = &GSLocalMemory::PageNumber16; + m_psm[PSM_PSMT8].pgn = &GSLocalMemory::PageNumber8; + m_psm[PSM_PSMT4].pgn = &GSLocalMemory::PageNumber4; + + m_psm[PSM_PSMCT24].rp = &GSLocalMemory::ReadPixel24; + m_psm[PSM_PSMCT16].rp = &GSLocalMemory::ReadPixel16; + m_psm[PSM_PSMCT16S].rp = &GSLocalMemory::ReadPixel16S; + m_psm[PSM_PSMT8].rp = &GSLocalMemory::ReadPixel8; + m_psm[PSM_PSMT4].rp = &GSLocalMemory::ReadPixel4; + m_psm[PSM_PSMT8H].rp = &GSLocalMemory::ReadPixel8H; + m_psm[PSM_PSMT4HL].rp = &GSLocalMemory::ReadPixel4HL; + m_psm[PSM_PSMT4HH].rp = &GSLocalMemory::ReadPixel4HH; + m_psm[PSM_PSMZ32].rp = &GSLocalMemory::ReadPixel32Z; + m_psm[PSM_PSMZ24].rp = &GSLocalMemory::ReadPixel24Z; + m_psm[PSM_PSMZ16].rp = &GSLocalMemory::ReadPixel16Z; + m_psm[PSM_PSMZ16S].rp = &GSLocalMemory::ReadPixel16SZ; + + m_psm[PSM_PSMCT24].rpa = &GSLocalMemory::ReadPixel24; + m_psm[PSM_PSMCT16].rpa = &GSLocalMemory::ReadPixel16; + m_psm[PSM_PSMCT16S].rpa = &GSLocalMemory::ReadPixel16; + m_psm[PSM_PSMT8].rpa = &GSLocalMemory::ReadPixel8; + m_psm[PSM_PSMT4].rpa = &GSLocalMemory::ReadPixel4; + m_psm[PSM_PSMT8H].rpa = &GSLocalMemory::ReadPixel8H; + m_psm[PSM_PSMT4HL].rpa = &GSLocalMemory::ReadPixel4HL; + m_psm[PSM_PSMT4HH].rpa = &GSLocalMemory::ReadPixel4HH; + m_psm[PSM_PSMZ32].rpa = &GSLocalMemory::ReadPixel32; + m_psm[PSM_PSMZ24].rpa = &GSLocalMemory::ReadPixel24; + m_psm[PSM_PSMZ16].rpa = &GSLocalMemory::ReadPixel16; + m_psm[PSM_PSMZ16S].rpa = &GSLocalMemory::ReadPixel16; + + m_psm[PSM_PSMCT32].wp = &GSLocalMemory::WritePixel32; + m_psm[PSM_PSMCT24].wp = &GSLocalMemory::WritePixel24; + m_psm[PSM_PSMCT16].wp = &GSLocalMemory::WritePixel16; + m_psm[PSM_PSMCT16S].wp = &GSLocalMemory::WritePixel16S; + m_psm[PSM_PSMT8].wp = &GSLocalMemory::WritePixel8; + m_psm[PSM_PSMT4].wp = &GSLocalMemory::WritePixel4; + m_psm[PSM_PSMT8H].wp = &GSLocalMemory::WritePixel8H; + m_psm[PSM_PSMT4HL].wp = &GSLocalMemory::WritePixel4HL; + m_psm[PSM_PSMT4HH].wp = &GSLocalMemory::WritePixel4HH; + m_psm[PSM_PSMZ32].wp = &GSLocalMemory::WritePixel32Z; + m_psm[PSM_PSMZ24].wp = &GSLocalMemory::WritePixel24Z; + m_psm[PSM_PSMZ16].wp = &GSLocalMemory::WritePixel16Z; + m_psm[PSM_PSMZ16S].wp = &GSLocalMemory::WritePixel16SZ; + + m_psm[PSM_PSMCT32].wpa = &GSLocalMemory::WritePixel32; + m_psm[PSM_PSMCT24].wpa = &GSLocalMemory::WritePixel24; + m_psm[PSM_PSMCT16].wpa = &GSLocalMemory::WritePixel16; + m_psm[PSM_PSMCT16S].wpa = &GSLocalMemory::WritePixel16; + m_psm[PSM_PSMT8].wpa = &GSLocalMemory::WritePixel8; + m_psm[PSM_PSMT4].wpa = &GSLocalMemory::WritePixel4; + m_psm[PSM_PSMT8H].wpa = &GSLocalMemory::WritePixel8H; + m_psm[PSM_PSMT4HL].wpa = &GSLocalMemory::WritePixel4HL; + m_psm[PSM_PSMT4HH].wpa = &GSLocalMemory::WritePixel4HH; + m_psm[PSM_PSMZ32].wpa = &GSLocalMemory::WritePixel32; + m_psm[PSM_PSMZ24].wpa = &GSLocalMemory::WritePixel24; + m_psm[PSM_PSMZ16].wpa = &GSLocalMemory::WritePixel16; + m_psm[PSM_PSMZ16S].wpa = &GSLocalMemory::WritePixel16; + + m_psm[PSM_PSMCT24].rt = &GSLocalMemory::ReadTexel24; + m_psm[PSM_PSMCT16].rt = &GSLocalMemory::ReadTexel16; + m_psm[PSM_PSMCT16S].rt = &GSLocalMemory::ReadTexel16S; + m_psm[PSM_PSMT8].rt = &GSLocalMemory::ReadTexel8; + m_psm[PSM_PSMT4].rt = &GSLocalMemory::ReadTexel4; + m_psm[PSM_PSMT8H].rt = &GSLocalMemory::ReadTexel8H; + m_psm[PSM_PSMT4HL].rt = &GSLocalMemory::ReadTexel4HL; + m_psm[PSM_PSMT4HH].rt = &GSLocalMemory::ReadTexel4HH; + m_psm[PSM_PSMZ32].rt = &GSLocalMemory::ReadTexel32Z; + m_psm[PSM_PSMZ24].rt = &GSLocalMemory::ReadTexel24Z; + m_psm[PSM_PSMZ16].rt = &GSLocalMemory::ReadTexel16Z; + m_psm[PSM_PSMZ16S].rt = &GSLocalMemory::ReadTexel16SZ; + + m_psm[PSM_PSMCT24].rta = &GSLocalMemory::ReadTexel24; + m_psm[PSM_PSMCT16].rta = &GSLocalMemory::ReadTexel16; + m_psm[PSM_PSMCT16S].rta = &GSLocalMemory::ReadTexel16; + m_psm[PSM_PSMT8].rta = &GSLocalMemory::ReadTexel8; + m_psm[PSM_PSMT4].rta = &GSLocalMemory::ReadTexel4; + m_psm[PSM_PSMT8H].rta = &GSLocalMemory::ReadTexel8H; + m_psm[PSM_PSMT4HL].rta = &GSLocalMemory::ReadTexel4HL; + m_psm[PSM_PSMT4HH].rta = &GSLocalMemory::ReadTexel4HH; + m_psm[PSM_PSMZ24].rta = &GSLocalMemory::ReadTexel24; + m_psm[PSM_PSMZ16].rta = &GSLocalMemory::ReadTexel16; + m_psm[PSM_PSMZ16S].rta = &GSLocalMemory::ReadTexel16; + + m_psm[PSM_PSMCT24].wfa = &GSLocalMemory::WritePixel24; + m_psm[PSM_PSMCT16].wfa = &GSLocalMemory::WriteFrame16; + m_psm[PSM_PSMCT16S].wfa = &GSLocalMemory::WriteFrame16; + m_psm[PSM_PSMZ24].wfa = &GSLocalMemory::WritePixel24; + m_psm[PSM_PSMZ16].wfa = &GSLocalMemory::WriteFrame16; + m_psm[PSM_PSMZ16S].wfa = &GSLocalMemory::WriteFrame16; + + m_psm[PSM_PSMCT16].rtNP = &GSLocalMemory::ReadTexel16NP; + m_psm[PSM_PSMCT16S].rtNP = &GSLocalMemory::ReadTexel16SNP; + m_psm[PSM_PSMT8].rtNP = &GSLocalMemory::ReadTexel8; + m_psm[PSM_PSMT4].rtNP = &GSLocalMemory::ReadTexel4; + m_psm[PSM_PSMT8H].rtNP = &GSLocalMemory::ReadTexel8H; + m_psm[PSM_PSMT4HL].rtNP = &GSLocalMemory::ReadTexel4HL; + m_psm[PSM_PSMT4HH].rtNP = &GSLocalMemory::ReadTexel4HH; + m_psm[PSM_PSMZ32].rtNP = &GSLocalMemory::ReadTexel32Z; + m_psm[PSM_PSMZ24].rtNP = &GSLocalMemory::ReadTexel24Z; + m_psm[PSM_PSMZ16].rtNP = &GSLocalMemory::ReadTexel16ZNP; + m_psm[PSM_PSMZ16S].rtNP = &GSLocalMemory::ReadTexel16SZNP; + + m_psm[PSM_PSMCT24].wi = &GSLocalMemory::WriteImage24; // TODO + m_psm[PSM_PSMCT16].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMCT16S].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMT8].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMT4].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMT8H].wi = &GSLocalMemory::WriteImage8H; // TODO + m_psm[PSM_PSMT4HL].wi = &GSLocalMemory::WriteImage4HL; // TODO + m_psm[PSM_PSMT4HH].wi = &GSLocalMemory::WriteImage4HH; // TODO + m_psm[PSM_PSMZ32].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMZ24].wi = &GSLocalMemory::WriteImage24Z; // TODO + m_psm[PSM_PSMZ16].wi = &GSLocalMemory::WriteImage; + m_psm[PSM_PSMZ16S].wi = &GSLocalMemory::WriteImage; + + m_psm[PSM_PSMCT24].rtx = &GSLocalMemory::ReadTexture24; + m_psm[PSM_PSMCT16].rtx = &GSLocalMemory::ReadTexture16; + m_psm[PSM_PSMCT16S].rtx = &GSLocalMemory::ReadTexture16S; + m_psm[PSM_PSMT8].rtx = &GSLocalMemory::ReadTexture8; + m_psm[PSM_PSMT4].rtx = &GSLocalMemory::ReadTexture4; + m_psm[PSM_PSMT8H].rtx = &GSLocalMemory::ReadTexture8H; + m_psm[PSM_PSMT4HL].rtx = &GSLocalMemory::ReadTexture4HL; + m_psm[PSM_PSMT4HH].rtx = &GSLocalMemory::ReadTexture4HH; + m_psm[PSM_PSMZ32].rtx = &GSLocalMemory::ReadTexture32Z; + m_psm[PSM_PSMZ24].rtx = &GSLocalMemory::ReadTexture24Z; + m_psm[PSM_PSMZ16].rtx = &GSLocalMemory::ReadTexture16Z; + m_psm[PSM_PSMZ16S].rtx = &GSLocalMemory::ReadTexture16SZ; + + m_psm[PSM_PSMCT16].rtxNP = &GSLocalMemory::ReadTexture16NP; + m_psm[PSM_PSMCT16S].rtxNP = &GSLocalMemory::ReadTexture16SNP; + m_psm[PSM_PSMT8].rtxNP = &GSLocalMemory::ReadTexture8NP; + m_psm[PSM_PSMT4].rtxNP = &GSLocalMemory::ReadTexture4NP; + m_psm[PSM_PSMT8H].rtxNP = &GSLocalMemory::ReadTexture8HNP; + m_psm[PSM_PSMT4HL].rtxNP = &GSLocalMemory::ReadTexture4HLNP; + m_psm[PSM_PSMT4HH].rtxNP = &GSLocalMemory::ReadTexture4HHNP; + m_psm[PSM_PSMZ32].rtxNP = &GSLocalMemory::ReadTexture32Z; + m_psm[PSM_PSMZ24].rtxNP = &GSLocalMemory::ReadTexture24Z; + m_psm[PSM_PSMZ16].rtxNP = &GSLocalMemory::ReadTexture16ZNP; + m_psm[PSM_PSMZ16S].rtxNP = &GSLocalMemory::ReadTexture16SZNP; + + m_psm[PSM_PSMT8].rtxP = &GSLocalMemory::ReadTexture8P; + m_psm[PSM_PSMT4].rtxP = &GSLocalMemory::ReadTexture4P; + m_psm[PSM_PSMT8H].rtxP = &GSLocalMemory::ReadTexture8HP; + m_psm[PSM_PSMT4HL].rtxP = &GSLocalMemory::ReadTexture4HLP; + m_psm[PSM_PSMT4HH].rtxP = &GSLocalMemory::ReadTexture4HHP; + + m_psm[PSM_PSMT8].pal = m_psm[PSM_PSMT8H].pal = 256; + m_psm[PSM_PSMT4].pal = m_psm[PSM_PSMT4HL].pal = m_psm[PSM_PSMT4HH].pal = 16; + + m_psm[PSM_PSMCT16].bpp = m_psm[PSM_PSMCT16S].bpp = 16; + m_psm[PSM_PSMT8].bpp = 8; + m_psm[PSM_PSMT4].bpp = 4; + m_psm[PSM_PSMZ16].bpp = m_psm[PSM_PSMZ16S].bpp = 16; + + m_psm[PSM_PSMCT24].trbpp = 24; + m_psm[PSM_PSMCT16].trbpp = m_psm[PSM_PSMCT16S].trbpp = 16; + m_psm[PSM_PSMT8].trbpp = m_psm[PSM_PSMT8H].trbpp = 8; + m_psm[PSM_PSMT4].trbpp = m_psm[PSM_PSMT4HL].trbpp = m_psm[PSM_PSMT4HH].trbpp = 4; + m_psm[PSM_PSMZ24].trbpp = 24; + m_psm[PSM_PSMZ16].trbpp = m_psm[PSM_PSMZ16S].trbpp = 16; + + m_psm[PSM_PSMCT16].bs = m_psm[PSM_PSMCT16S].bs = CSize(16, 8); + m_psm[PSM_PSMT8].bs = CSize(16, 16); + m_psm[PSM_PSMT4].bs = CSize(32, 16); + m_psm[PSM_PSMZ16].bs = m_psm[PSM_PSMZ16S].bs = CSize(16, 8); + + m_psm[PSM_PSMCT16].pgs = m_psm[PSM_PSMCT16S].pgs = CSize(64, 64); + m_psm[PSM_PSMT8].pgs = CSize(128, 64); + m_psm[PSM_PSMT4].pgs = CSize(128, 128); + m_psm[PSM_PSMZ16].pgs = m_psm[PSM_PSMZ16S].pgs = CSize(64, 64); + + for(int i = 0; i < 8; i++) m_psm[PSM_PSMCT16].rowOffset[i] = rowOffset16; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMCT16S].rowOffset[i] = rowOffset16S; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMT8].rowOffset[i] = rowOffset8[((i + 2) >> 2) & 1]; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMT4].rowOffset[i] = rowOffset4[((i + 2) >> 2) & 1]; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMZ32].rowOffset[i] = rowOffset32Z; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMZ24].rowOffset[i] = rowOffset32Z; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMZ16].rowOffset[i] = rowOffset16Z; + for(int i = 0; i < 8; i++) m_psm[PSM_PSMZ16S].rowOffset[i] = rowOffset16SZ; +} + +GSLocalMemory::~GSLocalMemory() +{ + VirtualFree(m_vm8, 0, MEM_RELEASE); + + POSITION pos = m_omap.GetHeadPosition(); + + while(pos) + { + Offset* o = m_omap.GetNextValue(pos); + + for(int i = 0; i < countof(o->col); i++) + { + _aligned_free(o->col); + } + + _aligned_free(o); + } + + m_omap.RemoveAll(); + + pos = m_o4map.GetHeadPosition(); + + while(pos) + { + _aligned_free(m_o4map.GetNextValue(pos)); + } + + m_o4map.RemoveAll(); +} + +GSLocalMemory::Offset* GSLocalMemory::GetOffset(DWORD bp, DWORD bw, DWORD psm) +{ + if(bw == 0) {ASSERT(0); return NULL;} + + ASSERT(m_psm[psm].bpp > 8); // only for 16/24/32/8h/4hh/4hl formats where all columns are the same + + DWORD hash = bp | (bw << 14) | (psm << 20); + + if(CRBMap::CPair* pair = m_omap.Lookup(hash)) + { + return pair->m_value; + } + + Offset* o = (Offset*)_aligned_malloc(sizeof(Offset), 16); + + o->hash = hash; + + pixelAddress pa = m_psm[psm].pa; + + for(int i = 0; i < 2048; i++) + { + o->row[i] = GSVector4i((int)pa(0, i, bp, bw)); + } + + int* p = (int*)_aligned_malloc(sizeof(int) * (2048 + 3) * 4, 16); + + for(int i = 0; i < 4; i++) + { + o->col[i] = &p[2048 * i + ((4 - (i & 3)) & 3)]; + + memcpy(o->col[i], m_psm[psm].rowOffset[0], sizeof(int) * 2048); + } + + m_omap.SetAt(hash, o); + + return o; +} + +GSLocalMemory::Offset4* GSLocalMemory::GetOffset4(const GIFRegFRAME& FRAME, const GIFRegZBUF& ZBUF) +{ + DWORD fbp = FRAME.Block(); + DWORD zbp = ZBUF.Block(); + DWORD fpsm = FRAME.PSM; + DWORD zpsm = ZBUF.PSM; + DWORD bw = FRAME.FBW; + + ASSERT(m_psm[fpsm].trbpp > 8 || m_psm[zpsm].trbpp > 8); + + // "(psm & 0x0f) ^ ((psm & 0xf0) >> 2)" creates 4 bit unique identifiers for render target formats (only) + + DWORD fpsm_hash = (fpsm & 0x0f) ^ ((fpsm & 0x30) >> 2); + DWORD zpsm_hash = (zpsm & 0x0f) ^ ((zpsm & 0x30) >> 2); + + DWORD hash = (FRAME.FBP << 0) | (ZBUF.ZBP << 9) | (bw << 18) | (fpsm_hash << 24) | (zpsm_hash << 28); + + if(CRBMap::CPair* pair = m_o4map.Lookup(hash)) + { + return pair->m_value; + } + + Offset4* o = (Offset4*)_aligned_malloc(sizeof(Offset4), 16); + + o->hash = hash; + + pixelAddress fpa = m_psm[fpsm].pa; + pixelAddress zpa = m_psm[zpsm].pa; + + int fs = m_psm[fpsm].bpp >> 5; + int zs = m_psm[zpsm].bpp >> 5; + + for(int i = 0; i < 2048; i++) + { + o->row[i].x = (int)fpa(0, i, fbp, bw) << fs; + o->row[i].y = (int)zpa(0, i, zbp, bw) << zs; + } + + for(int i = 0; i < 512; i++) + { + o->col[i].x = m_psm[fpsm].rowOffset[0][i * 4] << fs; + o->col[i].y = m_psm[zpsm].rowOffset[0][i * 4] << zs; + } + + m_o4map.SetAt(hash, o); + + return o; +} + +bool GSLocalMemory::FillRect(const GSVector4i& r, DWORD c, DWORD psm, DWORD bp, DWORD bw) +{ + const psm_t& tbl = m_psm[psm]; + + writePixel wp = tbl.wp; + pixelAddress ba = tbl.ba; + + int w = tbl.bs.cx; + int h = tbl.bs.cy; + int bpp = tbl.bpp; + + int shift = 0; + + switch(bpp) + { + case 32: shift = 0; break; + case 16: shift = 1; c = (c & 0xffff) * 0x00010001; break; + case 8: shift = 2; c = (c & 0xff) * 0x01010101; break; + case 4: shift = 3; c = (c & 0xf) * 0x11111111; break; + } + + CRect clip; + + clip.left = (r.x + (w - 1)) & ~(w - 1); + clip.top = (r.y + (h - 1)) & ~(h - 1); + clip.right = r.z & ~(w - 1); + clip.bottom = r.w & ~(h - 1); + + for(int y = r.y; y < clip.top; y++) + { + for(int x = r.x; x < r.z; x++) + { + (this->*wp)(x, y, c, bp, bw); + } + } + + for(int y = clip.bottom; y < r.w; y++) + { + for(int x = r.x; x < r.z; x++) + { + (this->*wp)(x, y, c, bp, bw); + } + } + + if(r.x < clip.left || clip.right < r.z) + { + for(int y = clip.top; y < clip.bottom; y += h) + { + for(int ys = y, ye = y + h; ys < ye; ys++) + { + for(int x = r.x; x < clip.left; x++) + { + (this->*wp)(x, ys, c, bp, bw); + } + + for(int x = clip.right; x < r.z; x++) + { + (this->*wp)(x, ys, c, bp, bw); + } + } + } + } + + if(psm == PSM_PSMCT24 || psm == PSM_PSMZ24) + { + #if _M_SSE >= 0x200 + + GSVector4i c128(c); + GSVector4i mask(0x00ffffff); + + for(int y = clip.top; y < clip.bottom; y += h) + { + for(int x = clip.left; x < clip.right; x += w) + { + GSVector4i* p = (GSVector4i*)&m_vm8[ba(x, y, bp, bw) << 2 >> shift]; + + for(int i = 0; i < 16; i += 4) + { + p[i + 0] = p[i + 0].blend8(c128, mask); + p[i + 1] = p[i + 1].blend8(c128, mask); + p[i + 2] = p[i + 2].blend8(c128, mask); + p[i + 3] = p[i + 3].blend8(c128, mask); + } + } + } + + #else + + c &= 0x00ffffff; + + for(int y = clip.top; y < clip.bottom; y += h) + { + for(int x = clip.left; x < clip.right; x += w) + { + DWORD* p = &m_vm32[ba(x, y, bp, bw)]; + + for(int i = 0; i < 64; i += 4) + { + p[i + 0] = (p[i + 0] & 0xff000000) | c; + p[i + 1] = (p[i + 1] & 0xff000000) | c; + p[i + 2] = (p[i + 2] & 0xff000000) | c; + p[i + 3] = (p[i + 3] & 0xff000000) | c; + } + } + } + + #endif + } + else + { + #if _M_SSE >= 0x200 + + GSVector4i c128(c); + + for(int y = clip.top; y < clip.bottom; y += h) + { + for(int x = clip.left; x < clip.right; x += w) + { + GSVector4i* p = (GSVector4i*)&m_vm8[ba(x, y, bp, bw) << 2 >> shift]; + + for(int i = 0; i < 16; i += 4) + { + p[i + 0] = c128; + p[i + 1] = c128; + p[i + 2] = c128; + p[i + 3] = c128; + } + } + } + + #else + + for(int y = clip.top; y < clip.bottom; y += h) + { + for(int x = clip.left; x < clip.right; x += w) + { + DWORD* p = (DWORD*)&m_vm8[ba(x, y, bp, bw) << 2 >> shift]; + + for(int i = 0; i < 64; i += 4) + { + p[i + 0] = c; + p[i + 1] = c; + p[i + 2] = c; + p[i + 3] = c; + } + } + } + + #endif + } + + return true; +} + +//////////////////// + +template +void GSLocalMemory::WriteImageColumn(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF) +{ + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + const int csy = bsy / 4; + + for(int offset = srcpitch * csy; h >= csy; h -= csy, y += csy, src += offset) + { + for(int x = l; x < r; x += bsx) + { + switch(psm) + { + case PSM_PSMCT32: WriteColumn32(y, (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], &src[x * 4], srcpitch); break; + case PSM_PSMCT16: WriteColumn16(y, (BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMCT16S: WriteColumn16(y, (BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMT8: WriteColumn8(y, (BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)], &src[x], srcpitch); break; + case PSM_PSMT4: WriteColumn4(y, (BYTE*)&m_vm8[BlockAddress4(x, y, bp, bw) >> 1], &src[x >> 1], srcpitch); break; + case PSM_PSMZ32: WriteColumn32(y, (BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)], &src[x * 4], srcpitch); break; + case PSM_PSMZ16: WriteColumn16(y, (BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMZ16S: WriteColumn16(y, (BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)], &src[x * 2], srcpitch); break; + // TODO + default: __assume(0); + } + } + } +} + +template +void GSLocalMemory::WriteImageBlock(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF) +{ + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + for(int offset = srcpitch * bsy; h >= bsy; h -= bsy, y += bsy, src += offset) + { + for(int x = l; x < r; x += bsx) + { + switch(psm) + { + case PSM_PSMCT32: WriteBlock32((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], &src[x * 4], srcpitch); break; + case PSM_PSMCT16: WriteBlock16((BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMCT16S: WriteBlock16((BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMT8: WriteBlock8((BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)], &src[x], srcpitch); break; + case PSM_PSMT4: WriteBlock4((BYTE*)&m_vm8[BlockAddress4(x, y, bp, bw) >> 1], &src[x >> 1], srcpitch); break; + case PSM_PSMZ32: WriteBlock32((BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)], &src[x * 4], srcpitch); break; + case PSM_PSMZ16: WriteBlock16((BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)], &src[x * 2], srcpitch); break; + case PSM_PSMZ16S: WriteBlock16((BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)], &src[x * 2], srcpitch); break; + // TODO + default: __assume(0); + } + } + } +} + +template +void GSLocalMemory::WriteImageLeftRight(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF) +{ + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + for(; h > 0; y++, h--, src += srcpitch) + { + for(int x = l; x < r; x++) + { + switch(psm) + { + case PSM_PSMCT32: WritePixel32(x, y, *(DWORD*)&src[x * 4], bp, bw); break; + case PSM_PSMCT16: WritePixel16(x, y, *(WORD*)&src[x * 2], bp, bw); break; + case PSM_PSMCT16S: WritePixel16S(x, y, *(WORD*)&src[x * 2], bp, bw); break; + case PSM_PSMT8: WritePixel8(x, y, src[x], bp, bw); break; + case PSM_PSMT4: WritePixel4(x, y, src[x >> 1] >> ((x & 1) << 2), bp, bw); break; + case PSM_PSMZ32: WritePixel32Z(x, y, *(DWORD*)&src[x * 4], bp, bw); break; + case PSM_PSMZ16: WritePixel16Z(x, y, *(WORD*)&src[x * 2], bp, bw); break; + case PSM_PSMZ16S: WritePixel16SZ(x, y, *(WORD*)&src[x * 2], bp, bw); break; + // TODO + default: __assume(0); + } + } + } +} + +template +void GSLocalMemory::WriteImageTopBottom(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF) +{ + __declspec(align(16)) BYTE buff[64]; // merge buffer for one column + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + const int csy = bsy / 4; + + // merge incomplete column + + int y2 = y & (csy - 1); + + if(y2 > 0) + { + int h2 = min(h, csy - y2); + + for(int x = l; x < r; x += bsx) + { + BYTE* dst = NULL; + + switch(psm) + { + case PSM_PSMCT32: dst = (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]; break; + case PSM_PSMCT16: dst = (BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)]; break; + case PSM_PSMCT16S: dst = (BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)]; break; + case PSM_PSMT8: dst = (BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)]; break; + case PSM_PSMT4: dst = (BYTE*)&m_vm8[BlockAddress4(x, y, bp, bw) >> 1]; break; + case PSM_PSMZ32: dst = (BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)]; break; + case PSM_PSMZ16: dst = (BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)]; break; + case PSM_PSMZ16S: dst = (BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)]; break; + // TODO + default: __assume(0); + } + + switch(psm) + { + case PSM_PSMCT32: + case PSM_PSMZ32: + ReadColumn32(y, dst, buff, 32); + memcpy(&buff[32], &src[x * 4], 32); + WriteColumn32(y, dst, buff, 32); + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + case PSM_PSMZ16: + case PSM_PSMZ16S: + ReadColumn16(y, dst, buff, 32); + memcpy(&buff[32], &src[x * 2], 32); + WriteColumn16(y, dst, buff, 32); + break; + case PSM_PSMT8: + ReadColumn8(y, dst, buff, 16); + memcpy(&buff[y2 * 16], &src[x], h2 * 16); + WriteColumn8(y, dst, buff, 16); + break; + case PSM_PSMT4: + ReadColumn4(y, dst, buff, 16); + memcpy(&buff[y2 * 16], &src[x >> 1], h2 * 16); + WriteColumn4(y, dst, buff, 16); + break; + // TODO + default: + __assume(0); + } + } + + src += srcpitch * h2; + y += h2; + h -= h2; + } + + // write whole columns + + { + int h2 = h & ~(csy - 1); + + if(h2 > 0) + { + if(((DWORD_PTR)&src[l * trbpp >> 3] & 15) == 0 && (srcpitch & 15) == 0) + { + WriteImageColumn(l, r, y, h2, src, srcpitch, BITBLTBUF); + } + else + { + WriteImageColumn(l, r, y, h2, src, srcpitch, BITBLTBUF); + } + + src += srcpitch * h2; + y += h2; + h -= h2; + } + } + + // merge incomplete column + + if(h >= 1) + { + for(int x = l; x < r; x += bsx) + { + BYTE* dst = NULL; + + switch(psm) + { + case PSM_PSMCT32: dst = (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]; break; + case PSM_PSMCT16: dst = (BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)]; break; + case PSM_PSMCT16S: dst = (BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)]; break; + case PSM_PSMT8: dst = (BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)]; break; + case PSM_PSMT4: dst = (BYTE*)&m_vm8[BlockAddress4(x, y, bp, bw) >> 1]; break; + case PSM_PSMZ32: dst = (BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)]; break; + case PSM_PSMZ16: dst = (BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)]; break; + case PSM_PSMZ16S: dst = (BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)]; break; + // TODO + default: __assume(0); + } + + switch(psm) + { + case PSM_PSMCT32: + case PSM_PSMZ32: + ReadColumn32(y, dst, buff, 32); + memcpy(&buff[0], &src[x * 4], 32); + WriteColumn32(y, dst, buff, 32); + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + case PSM_PSMZ16: + case PSM_PSMZ16S: + ReadColumn16(y, dst, buff, 32); + memcpy(&buff[0], &src[x * 2], 32); + WriteColumn16(y, dst, buff, 32); + break; + case PSM_PSMT8: + ReadColumn8(y, dst, buff, 16); + memcpy(&buff[0], &src[x], h * 16); + WriteColumn8(y, dst, buff, 16); + break; + case PSM_PSMT4: + ReadColumn4(y, dst, buff, 16); + memcpy(&buff[0], &src[x >> 1], h * 16); + WriteColumn4(y, dst, buff, 16); + break; + // TODO + default: + __assume(0); + } + } + } +} + +template +void GSLocalMemory::WriteImage(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + int l = (int)TRXPOS.DSAX; + int r = (int)TRXREG.RRW; + + // finish the incomplete row first + + if(tx != l) + { + int n = min(len, (r - tx) * trbpp >> 3); + WriteImageX(tx, ty, src, n, BITBLTBUF, TRXPOS, TRXREG); + src += n; + len -= n; + } + + int la = (l + (bsx - 1)) & ~(bsx - 1); + int ra = r & ~(bsx - 1); + int srcpitch = (r - l) * trbpp >> 3; + int h = len / srcpitch; + + // transfer width >= block width, and there is at least one full row + + if(ra - la >= bsx && h > 0) + { + BYTE* s = &src[-l * trbpp >> 3]; + + src += srcpitch * h; + len -= srcpitch * h; + + // left part + + if(l < la) + { + WriteImageLeftRight(l, la, ty, h, s, srcpitch, BITBLTBUF); + } + + // right part + + if(ra < r) + { + WriteImageLeftRight(ra, r, ty, h, s, srcpitch, BITBLTBUF); + } + + // horizontally aligned part + + if(la < ra) + { + // top part + + { + int h2 = min(h, bsy - (ty & (bsy - 1))); + + if(h2 < bsy) + { + WriteImageTopBottom(la, ra, ty, h2, s, srcpitch, BITBLTBUF); + + s += srcpitch * h2; + ty += h2; + h -= h2; + } + } + + // horizontally and vertically aligned part + + { + int h2 = h & ~(bsy - 1); + + if(h2 > 0) + { + if(((DWORD_PTR)&s[la * trbpp >> 3] & 15) == 0 && (srcpitch & 15) == 0) + { + WriteImageBlock(la, ra, ty, h2, s, srcpitch, BITBLTBUF); + } + else + { + WriteImageBlock(la, ra, ty, h2, s, srcpitch, BITBLTBUF); + } + + s += srcpitch * h2; + ty += h2; + h -= h2; + } + } + + // bottom part + + if(h > 0) + { + WriteImageTopBottom(la, ra, ty, h, s, srcpitch, BITBLTBUF); + + // s += srcpitch * h; + ty += h; + // h -= h; + } + } + } + + // the rest + + if(len > 0) + { + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } +} + + +#define IsTopLeftAligned(dsax, tx, ty, bw, bh) \ + ((((int)dsax) & ((bw)-1)) == 0 && ((tx) & ((bw)-1)) == 0 && ((int)dsax) == (tx) && ((ty) & ((bh)-1)) == 0) + +void GSLocalMemory::WriteImage24(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + int tw = TRXREG.RRW, srcpitch = (TRXREG.RRW - TRXPOS.DSAX) * 3; + int th = len / srcpitch; + + bool aligned = IsTopLeftAligned(TRXPOS.DSAX, tx, ty, 8, 8); + + if(!aligned || (tw & 7) || (th & 7) || (len % srcpitch)) + { + // TODO + + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } + else + { + th += ty; + + for(int y = ty; y < th; y += 8, src += srcpitch * 8) + { + for(int x = tx; x < tw; x += 8) + { + UnpackAndWriteBlock24(src + (x - tx) * 3, srcpitch, (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]); + } + } + + ty = th; + } +} +void GSLocalMemory::WriteImage8H(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + int tw = TRXREG.RRW, srcpitch = TRXREG.RRW - TRXPOS.DSAX; + int th = len / srcpitch; + + bool aligned = IsTopLeftAligned(TRXPOS.DSAX, tx, ty, 8, 8); + + if(!aligned || (tw & 7) || (th & 7) || (len % srcpitch)) + { + // TODO + + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } + else + { + th += ty; + + for(int y = ty; y < th; y += 8, src += srcpitch * 8) + { + for(int x = tx; x < tw; x += 8) + { + UnpackAndWriteBlock8H(src + (x - tx), srcpitch, (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]); + } + } + + ty = th; + } +} + +void GSLocalMemory::WriteImage4HL(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + int tw = TRXREG.RRW, srcpitch = (TRXREG.RRW - TRXPOS.DSAX) / 2; + int th = len / srcpitch; + + bool aligned = IsTopLeftAligned(TRXPOS.DSAX, tx, ty, 8, 8); + + if(!aligned || (tw & 7) || (th & 7) || (len % srcpitch)) + { + // TODO + + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } + else + { + th += ty; + + for(int y = ty; y < th; y += 8, src += srcpitch * 8) + { + for(int x = tx; x < tw; x += 8) + { + UnpackAndWriteBlock4HL(src + (x - tx) / 2, srcpitch, (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]); + } + } + + ty = th; + } +} + +void GSLocalMemory::WriteImage4HH(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + int tw = TRXREG.RRW, srcpitch = (TRXREG.RRW - TRXPOS.DSAX) / 2; + int th = len / srcpitch; + + bool aligned = IsTopLeftAligned(TRXPOS.DSAX, tx, ty, 8, 8); + + if(!aligned || (tw & 7) || (th & 7) || (len % srcpitch)) + { + // TODO + + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } + else + { + th += ty; + + for(int y = ty; y < th; y += 8, src += srcpitch * 8) + { + for(int x = tx; x < tw; x += 8) + { + UnpackAndWriteBlock4HH(src + (x - tx) / 2, srcpitch, (BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)]); + } + } + + ty = th; + } +} +void GSLocalMemory::WriteImage24Z(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(TRXREG.RRW == 0) return; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + int tw = TRXREG.RRW, srcpitch = (TRXREG.RRW - TRXPOS.DSAX) * 3; + int th = len / srcpitch; + + bool aligned = IsTopLeftAligned(TRXPOS.DSAX, tx, ty, 8, 8); + + if(!aligned || (tw & 7) || (th & 7) || (len % srcpitch)) + { + // TODO + + WriteImageX(tx, ty, src, len, BITBLTBUF, TRXPOS, TRXREG); + } + else + { + th += ty; + + for(int y = ty; y < th; y += 8, src += srcpitch * 8) + { + for(int x = tx; x < tw; x += 8) + { + UnpackAndWriteBlock24(src + (x - tx) * 3, srcpitch, (BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)]); + } + } + + ty = th; + } +} +void GSLocalMemory::WriteImageX(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) +{ + if(len <= 0) return; + + // if(ty >= (int)TRXREG.RRH) {ASSERT(0); return;} + + BYTE* pb = (BYTE*)src; + WORD* pw = (WORD*)src; + DWORD* pd = (DWORD*)src; + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + psm_t* psm = &m_psm[BITBLTBUF.DPSM]; + + int x = tx; + int y = ty; + int sx = (int)TRXPOS.DSAX; + int ex = (int)TRXREG.RRW; + + switch(BITBLTBUF.DPSM) + { + case PSM_PSMCT32: + case PSM_PSMZ32: + + len /= 4; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pd++) + { + WritePixel32(addr + offset[x], *pd); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMCT24: + case PSM_PSMZ24: + + len /= 3; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb += 3) + { + WritePixel24(addr + offset[x], *(DWORD*)pb); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMCT16: + case PSM_PSMCT16S: + case PSM_PSMZ16: + case PSM_PSMZ16S: + + len /= 2; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pw++) + { + WritePixel16(addr + offset[x], *pw); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT8: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb++) + { + WritePixel8(addr + offset[x], *pb); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + WritePixel4(addr + offset[x + 0], *pb & 0xf); + WritePixel4(addr + offset[x + 1], *pb >> 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT8H: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb++) + { + WritePixel8H(addr + offset[x], *pb); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4HL: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + WritePixel4HL(addr + offset[x + 0], *pb & 0xf); + WritePixel4HL(addr + offset[x + 1], *pb >> 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4HH: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + WritePixel4HH(addr + offset[x + 0], *pb & 0xf); + WritePixel4HH(addr + offset[x + 1], *pb >> 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + } + + tx = x; + ty = y; +} + +// + +void GSLocalMemory::ReadImageX(int& tx, int& ty, BYTE* dst, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) const +{ + if(len <= 0) return; + + // if(ty >= (int)TRXREG.RRH) {ASSERT(0); return;} + + BYTE* pb = (BYTE*)dst; + WORD* pw = (WORD*)dst; + DWORD* pd = (DWORD*)dst; + + DWORD bp = BITBLTBUF.SBP; + DWORD bw = BITBLTBUF.SBW; + psm_t* psm = &m_psm[BITBLTBUF.SPSM]; + + int x = tx; + int y = ty; + int sx = (int)TRXPOS.SSAX; + int ex = (int)TRXREG.RRW; + + switch(BITBLTBUF.SPSM) + { + case PSM_PSMCT32: + case PSM_PSMZ32: + + len /= 4; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pd++) + { + *pd = ReadPixel32(addr + offset[x]); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMCT24: + case PSM_PSMZ24: + + len /= 3; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb += 3) + { + DWORD dw = ReadPixel32(addr + offset[x]); + + pb[0] = ((BYTE*)&dw)[0]; + pb[1] = ((BYTE*)&dw)[1]; + pb[2] = ((BYTE*)&dw)[2]; + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMCT16: + case PSM_PSMCT16S: + case PSM_PSMZ16: + case PSM_PSMZ16S: + + len /= 2; + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pw++) + { + *pw = ReadPixel16(addr + offset[x]); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT8: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb++) + { + *pb = ReadPixel8(addr + offset[x]); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + *pb = ReadPixel4(addr + offset[x + 0]) | (ReadPixel4(addr + offset[x + 1]) << 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT8H: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x++, pb++) + { + *pb = ReadPixel8H(addr + offset[x]); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4HL: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + *pb = ReadPixel4HL(addr + offset[x + 0]) | (ReadPixel4HL(addr + offset[x + 1]) << 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + + case PSM_PSMT4HH: + + while(len > 0) + { + DWORD addr = psm->pa(0, y, bp, bw); + int* offset = psm->rowOffset[y & 7]; + + for(; len > 0 && x < ex; len--, x += 2, pb++) + { + *pb = ReadPixel4HH(addr + offset[x + 0]) | (ReadPixel4HH(addr + offset[x + 1]) << 4); + } + + if(x == ex) {x = sx; y++;} + } + + break; + } + + tx = x; + ty = y; +} + +/////////////////// + +void GSLocalMemory::ReadTexture32(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(8, 8, 32) + { + ReadBlock32((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END + +} + +void GSLocalMemory::ReadTexture24(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + if(TEXA.AEM) + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock24((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END + } + else + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock24((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture16(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + __declspec(align(16)) WORD block[16 * 8]; + + FOREACH_BLOCK_START(16, 8, 32) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock16(block, dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture16S(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + __declspec(align(16)) WORD block[16 * 8]; + + FOREACH_BLOCK_START(16, 8, 32) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock16(block, dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture8(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + FOREACH_BLOCK_START(16, 16, 32) + { + ReadAndExpandBlock8_32((BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const UINT64* pal = m_clut; + + FOREACH_BLOCK_START(32, 16, 32) + { + ReadAndExpandBlock4_32((BYTE*)&m_vm8[BlockAddress4(x, y, bp, bw) >> 1], dst, dstpitch, pal); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture8H(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock8H_32((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4HL(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock4HL_32((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4HH(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock4HH_32((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture32Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(8, 8, 32) + { + ReadBlock32((BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture24Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + if(TEXA.AEM) + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock24((BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)], dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END + } + else + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock24((BYTE*)&m_vm32[BlockAddress32Z(x, y, bp, bw)], dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture16Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + __declspec(align(16)) WORD block[16 * 8]; + + FOREACH_BLOCK_START(16, 8, 32) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock16(block, dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture16SZ(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + __declspec(align(16)) WORD block[16 * 8]; + + FOREACH_BLOCK_START(16, 8, 32) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock16(block, dst, dstpitch, TEXA); + } + FOREACH_BLOCK_END +} + +/////////////////// + +void GSLocalMemory::ReadTexture(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP) +{ + readTexture rtx = m_psm[TEX0.PSM].rtx; + readTexel rt = m_psm[TEX0.PSM].rt; + CSize bs = m_psm[TEX0.PSM].bs; + + if(r.Width() < bs.cx || r.Height() < bs.cy + || (r.left & (bs.cx-1)) || (r.top & (bs.cy-1)) + || (r.right & (bs.cx-1)) || (r.bottom & (bs.cy-1)) + || (CLAMP.WMS == 3) || (CLAMP.WMT == 3)) + { + ReadTexture(r, dst, dstpitch, TEX0, TEXA, CLAMP, rt, rtx); + } + else + { + (this->*rtx)(r, dst, dstpitch, TEX0, TEXA); + } +} + +void GSLocalMemory::ReadTextureNC(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP) +{ + readTexture rtx = m_psm[TEX0.PSM].rtx; + readTexel rt = m_psm[TEX0.PSM].rt; + CSize bs = m_psm[TEX0.PSM].bs; + + if(r.Width() < bs.cx || r.Height() < bs.cy + || (r.left & (bs.cx-1)) || (r.top & (bs.cy-1)) + || (r.right & (bs.cx-1)) || (r.bottom & (bs.cy-1))) + { + ReadTextureNC(r, dst, dstpitch, TEX0, TEXA, rt, rtx); + } + else + { + (this->*rtx)(r, dst, dstpitch, TEX0, TEXA); + } +} +/////////////////// + +void GSLocalMemory::ReadTexture16NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(16, 8, 16) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture16SNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(16, 8, 16) + { + ReadBlock16((BYTE*)&m_vm16[BlockAddress16S(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture8NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + FOREACH_BLOCK_START(16, 16, 32) + { + ReadAndExpandBlock8_32((BYTE*)&m_vm8[BlockAddress8(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } + else + { + ASSERT(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S); + + __declspec(align(16)) BYTE block[16 * 16]; + + FOREACH_BLOCK_START(16, 16, 16) + { + ReadBlock8(&m_vm8[BlockAddress8(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 16); + + ExpandBlock8_16(block, dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture4NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const UINT64* pal = m_clut; + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + FOREACH_BLOCK_START(32, 16, 32) + { + ReadAndExpandBlock4_32(&m_vm8[BlockAddress4(x, y, bp, bw) >> 1], dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } + else + { + ASSERT(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S); + + __declspec(align(16)) BYTE block[(32 / 2) * 16]; + + FOREACH_BLOCK_START(32, 16, 16) + { + ReadBlock4(&m_vm8[BlockAddress4(x, y, bp, bw)>>1], (BYTE*)block, sizeof(block) / 16); + + ExpandBlock4_16(block, dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture8HNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock8H_32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } + else + { + ASSERT(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S); + + __declspec(align(16)) DWORD block[8 * 8]; + + FOREACH_BLOCK_START(8, 8, 16) + { + ReadBlock32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock8H_16(block, dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture4HLNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock4HL_32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } + else + { + ASSERT(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S); + + __declspec(align(16)) DWORD block[8 * 8]; + + FOREACH_BLOCK_START(8, 8, 16) + { + ReadBlock32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock4HL_16(block, dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture4HHNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + const DWORD* pal = m_clut; + + if(TEX0.CPSM == PSM_PSMCT32 || TEX0.CPSM == PSM_PSMCT24) + { + FOREACH_BLOCK_START(8, 8, 32) + { + ReadAndExpandBlock4HH_32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } + else + { + ASSERT(TEX0.CPSM == PSM_PSMCT16 || TEX0.CPSM == PSM_PSMCT16S); + + __declspec(align(16)) DWORD block[8 * 8]; + + FOREACH_BLOCK_START(8, 8, 16) + { + ReadBlock32((const BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], (BYTE*)block, sizeof(block) / 8); + + ExpandBlock4HH_16(block, dst, dstpitch, pal); + } + FOREACH_BLOCK_END + } +} + +void GSLocalMemory::ReadTexture16ZNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(16, 8, 16) + { + ReadBlock16((const BYTE*)&m_vm16[BlockAddress16Z(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture16SZNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(16, 8, 16) + { + ReadBlock16((const BYTE*)&m_vm16[BlockAddress16SZ(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +/////////////////// + +void GSLocalMemory::ReadTextureNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP) +{ + readTexture rtx = m_psm[TEX0.PSM].rtxNP; + readTexel rt = m_psm[TEX0.PSM].rtNP; + CSize bs = m_psm[TEX0.PSM].bs; + + if(r.Width() < bs.cx || r.Height() < bs.cy + || (r.left & (bs.cx-1)) || (r.top & (bs.cy-1)) + || (r.right & (bs.cx-1)) || (r.bottom & (bs.cy-1)) + || (CLAMP.WMS == 3) || (CLAMP.WMT == 3)) + { + DWORD psm = TEX0.PSM; + + switch(psm) + { + case PSM_PSMT8: + case PSM_PSMT8H: + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + psm = TEX0.CPSM; + break; + } + + switch(psm) + { + default: + case PSM_PSMCT32: + case PSM_PSMCT24: + ReadTexture(r, dst, dstpitch, TEX0, TEXA, CLAMP, rt, rtx); + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + ReadTexture(r, dst, dstpitch, TEX0, TEXA, CLAMP, rt, rtx); + break; + } + } + else + { + (this->*rtx)(r, dst, dstpitch, TEX0, TEXA); + } +} + +void GSLocalMemory::ReadTextureNPNC(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP) +{ + readTexture rtx = m_psm[TEX0.PSM].rtxNP; + readTexel rt = m_psm[TEX0.PSM].rtNP; + CSize bs = m_psm[TEX0.PSM].bs; + + if(r.Width() < bs.cx || r.Height() < bs.cy + || (r.left & (bs.cx-1)) || (r.top & (bs.cy-1)) + || (r.right & (bs.cx-1)) || (r.bottom & (bs.cy-1))) + { + DWORD psm = TEX0.PSM; + + switch(psm) + { + case PSM_PSMT8: + case PSM_PSMT8H: + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + psm = TEX0.CPSM; + break; + } + + switch(psm) + { + default: + case PSM_PSMCT32: + case PSM_PSMCT24: + ReadTextureNC(r, dst, dstpitch, TEX0, TEXA, rt, rtx); + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + ReadTextureNC(r, dst, dstpitch, TEX0, TEXA, rt, rtx); + break; + } + } + else + { + (this->*rtx)(r, dst, dstpitch, TEX0, TEXA); + } +} + +// 32/8 + +void GSLocalMemory::ReadTexture8P(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(16, 16, 8) + { + ReadBlock8(&m_vm8[BlockAddress8(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4P(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(32, 16, 8) + { + ReadBlock4P(&m_vm8[BlockAddress4(x, y, bp, bw) >> 1], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture8HP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(8, 8, 8) + { + ReadBlock8HP((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4HLP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(8, 8, 8) + { + ReadBlock4HLP((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +void GSLocalMemory::ReadTexture4HHP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const +{ + FOREACH_BLOCK_START(8, 8, 8) + { + ReadBlock4HHP((BYTE*)&m_vm32[BlockAddress32(x, y, bp, bw)], dst, dstpitch); + } + FOREACH_BLOCK_END +} + +// + +template +void GSLocalMemory::ReadTexture(CRect r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP, readTexel rt, readTexture rtx) +{ + // TODO: this is a mess, make it more simple + + DWORD wms = CLAMP.WMS, wmt = CLAMP.WMT; + DWORD minu = CLAMP.MINU, maxu = CLAMP.MAXU; + DWORD minv = CLAMP.MINV, maxv = CLAMP.MAXV; + + CSize bs = m_psm[TEX0.PSM].bs; + + int bsxm = bs.cx - 1; + int bsym = bs.cy - 1; + + if(wms == 3 || wmt == 3) + { + if(wms == 3 && wmt == 3) + { + int w = minu + 1; + int h = minv + 1; + + w = (w + bsxm) & ~bsxm; + h = (h + bsym) & ~bsym; + + if(w % bs.cx == 0 && maxu % bs.cx == 0 && h % bs.cy == 0 && maxv % bs.cy == 0) + { +// printf("!!! 1 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + + T* buff = (T*)_aligned_malloc(w * h * sizeof(T), 16); + + (this->*rtx)(CRect(CPoint(maxu, maxv), CSize(w, h)), (BYTE*)buff, w * sizeof(T), TEX0, TEXA); + + dst -= r.left * sizeof(T); + +// int left = (r.left + minu) & ~minu; +// int right = r.right & ~minu; + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + { + T* src = &buff[(y & minv) * w]; + + int x = r.left; +/* + for(; x < left; x++) + { + ((T*)dst)[x] = src[x & minu]; + } + + for(; x < right; x += minu + 1) + { + memcpy(&((T*)dst)[x], src, sizeof(T) * (minu + 1)); + } +*/ + for(; x < r.right; x++) + { + ((T*)dst)[x] = src[x & minu]; + } + } + + _aligned_free(buff); + + return; + } + } + + if(wms == 2) + { + int left = r.left; + r.left = min(r.right, max(r.left, (int)minu)); + r.right = max(r.left, min(r.right, (int)maxu + 1)); + dst += (r.left - left) * sizeof(T); + } + + if(wmt == 2) + { + int top = r.top; + r.top = min(r.bottom, max(r.top, (int)minv)); + r.bottom = max(r.top, min(r.bottom, (int)maxv + 1)); + dst += (r.top - top) * dstpitch; + } + + if(wms == 3 && wmt != 3) + { + int w = ((minu + 1) + bsxm) & ~bsxm; + + if(w % bs.cx == 0 && maxu % bs.cx == 0) + { +// printf("!!! 2 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + int top = r.top & ~bsym; + int bottom = (r.bottom + bsym) & ~bsym; + + int h = bottom - top; + + T* buff = (T*)_aligned_malloc(w * h * sizeof(T), 16); + + (this->*rtx)(CRect(CPoint(maxu, top), CSize(w, h)), (BYTE*)buff, w * sizeof(T), TEX0, TEXA); + + dst -= r.left * sizeof(T); + +// int left = (r.left + minu) & ~minu; +// int right = r.right & ~minu; + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + { + T* src = &buff[(y - top) * w]; + + int x = r.left; +/* + for(; x < left; x++) + { + ((T*)dst)[x] = src[x & minu]; + } + + for(; x < right; x += minu + 1) + { + memcpy(&((T*)dst)[x], src, sizeof(T) * (minu + 1)); + } +*/ + for(; x < r.right; x++) + { + ((T*)dst)[x] = src[x & minu]; + } + } + + _aligned_free(buff); + + return; + } + } + + if(wms != 3 && wmt == 3) + { + int h = (minv + 1 + bsym) & ~bsym; + + if(h % bs.cy == 0 && maxv % bs.cy == 0) + { +// printf("!!! 3 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + int left = r.left & ~bsxm; + int right = (r.right + bsxm) & ~bsxm; + + int w = right - left; + + T* buff = (T*)_aligned_malloc(w * h * sizeof(T), 16); + + (this->*rtx)(CRect(CPoint(left, maxv), CSize(w, h)), (BYTE*)buff, w * sizeof(T), TEX0, TEXA); + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + { + T* src = &buff[(y & minv) * w + (r.left - left)]; + + memcpy(dst, src, sizeof(T) * r.Width()); + } + + _aligned_free(buff); + + return; + } + } + + switch(wms) + { + default: for(int x = r.left; x < r.right; x++) m_xtbl[x] = x; break; + case 3: for(int x = r.left; x < r.right; x++) m_xtbl[x] = (x & minu) | maxu; break; + } + + switch(wmt) + { + default: for(int y = r.top; y < r.bottom; y++) m_ytbl[y] = y; break; + case 3: for(int y = r.top; y < r.bottom; y++) m_ytbl[y] = (y & minv) | maxv; break; + } + +// printf("!!! 4 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(m_xtbl[x], m_ytbl[y], TEX0, TEXA); + } + else + { + // find a block-aligned rect that fits between r and the region clamped area (if any) + + CRect r1 = r; + CRect r2 = r; + + r1.left = (r1.left + bsxm) & ~bsxm; + r1.top = (r1.top + bsym) & ~bsym; + r1.right = r1.right & ~bsxm; + r1.bottom = r1.bottom & ~bsym; + + if(wms == 2 && minu < maxu) + { + r2.left = minu & ~bsxm; + r2.right = (maxu + bsxm) & ~bsxm; + } + + if(wmt == 2 && minv < maxv) + { + r2.top = minv & ~bsym; + r2.bottom = (maxv + bsym) & ~bsym; + } + + CRect cr = r1 & r2; + + bool aligned = ((DWORD_PTR)(dst + (cr.left - r.left) * sizeof(T)) & 0xf) == 0; + + if(cr.left >= cr.right && cr.top >= cr.bottom || !aligned) + { + // TODO: expand r to block size, read into temp buffer, copy to r (like above) + +if(!aligned) printf("unaligned memory pointer passed to ReadTexture\n"); + +// printf("!!! 5 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } + else + { +// printf("!!! 6 wms = %d, wmt = %d, %3x %3x %3x %3x, %d %d - %d %d\n", wms, wmt, minu, maxu, minv, maxv, r.left, r.top, r.right, r.bottom); + + for(int y = r.top; y < cr.top; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + + if(!cr.IsRectEmpty()) + { + (this->*rtx)(cr, dst + (cr.left - r.left) * sizeof(T), dstpitch, TEX0, TEXA); + } + + for(int y = cr.top; y < cr.bottom; y++, dst += dstpitch) + { + for(int x = r.left, i = 0; x < cr.left; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + for(int x = cr.right, i = x - r.left; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } + + for(int y = cr.bottom; y < r.bottom; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } + } +} + +template +void GSLocalMemory::ReadTextureNC(CRect r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, readTexel rt, readTexture rtx) +{ + CSize bs = m_psm[TEX0.PSM].bs; + + int bsxm = bs.cx - 1; + int bsym = bs.cy - 1; + + CRect cr; + + cr.left = (r.left + bsxm) & ~bsxm; + cr.top = (r.top + bsym) & ~bsym; + cr.right = r.right & ~bsxm; + cr.bottom = r.bottom & ~bsym; + + bool aligned = ((DWORD_PTR)(dst + (cr.left - r.left) * sizeof(T)) & 0xf) == 0; + + if(cr.left >= cr.right && cr.top >= cr.bottom || !aligned) + { + // TODO: expand r to block size, read into temp buffer, copy to r (like above) + +if(!aligned) printf("unaligned memory pointer passed to ReadTexture\n"); + + for(int y = r.top; y < r.bottom; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } + else + { + for(int y = r.top; y < cr.top; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + + if(!cr.IsRectEmpty()) + (this->*rtx)(cr, dst + (cr.left - r.left) * sizeof(T), dstpitch, TEX0, TEXA); + + for(int y = cr.top; y < cr.bottom; y++, dst += dstpitch) + { + for(int x = r.left, i = 0; x < cr.left; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + for(int x = cr.right, i = x - r.left; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } + + for(int y = cr.bottom; y < r.bottom; y++, dst += dstpitch) + for(int x = r.left, i = 0; x < r.right; x++, i++) + ((T*)dst)[i] = (T)(this->*rt)(x, y, TEX0, TEXA); + } +} + +HRESULT GSLocalMemory::SaveBMP(LPCTSTR fn, DWORD bp, DWORD bw, DWORD psm, int w, int h) +{ + int pitch = w * 4; + int size = pitch * h; + void* bits = ::_aligned_malloc(size, 16); + + GIFRegTEX0 TEX0; + + TEX0.TBP0 = bp; + TEX0.TBW = bw; + TEX0.PSM = psm; + + GIFRegTEXA TEXA; + + TEXA.AEM = 0; + TEXA.TA0 = 0; + TEXA.TA1 = 0x80; + + // (this->*m_psm[TEX0.PSM].rtx)(CRect(0, 0, w, h), bits, pitch, TEX0, TEXA); + + readPixel rp = m_psm[psm].rp; + + BYTE* p = (BYTE*)bits; + + for(int j = h-1; j >= 0; j--, p += pitch) + for(int i = 0; i < w; i++) + ((DWORD*)p)[i] = (this->*rp)(i, j, TEX0.TBP0, TEX0.TBW); + + if(FILE* fp = _tfopen(fn, _T("wb"))) + { + BITMAPINFOHEADER bih; + memset(&bih, 0, sizeof(bih)); + bih.biSize = sizeof(bih); + bih.biWidth = w; + bih.biHeight = h; + bih.biPlanes = 1; + bih.biBitCount = 32; + bih.biCompression = BI_RGB; + bih.biSizeImage = size; + + BITMAPFILEHEADER bfh; + memset(&bfh, 0, sizeof(bfh)); + bfh.bfType = 'MB'; + bfh.bfOffBits = sizeof(bfh) + sizeof(bih); + bfh.bfSize = bfh.bfOffBits + size; + bfh.bfReserved1 = bfh.bfReserved2 = 0; + + fwrite(&bfh, 1, sizeof(bfh), fp); + fwrite(&bih, 1, sizeof(bih), fp); + fwrite(bits, 1, size, fp); + + fclose(fp); + } + + ::_aligned_free(bits); + + return true; +} diff --git a/plugins/GSdx/GSLocalMemory.h b/plugins/GSdx/GSLocalMemory.h new file mode 100644 index 0000000000..6d273dcbc1 --- /dev/null +++ b/plugins/GSdx/GSLocalMemory.h @@ -0,0 +1,1009 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#pragma warning(disable: 4100) // warning C4100: 'TEXA' : unreferenced formal parameter + +#include "GS.h" +#include "GSTables.h" +#include "GSVector.h" +#include "GSBlock.h" +#include "GSClut.h" + +class GSLocalMemory : public GSBlock +{ +public: + typedef DWORD (*pixelAddress)(int x, int y, DWORD bp, DWORD bw); + typedef void (GSLocalMemory::*writePixel)(int x, int y, DWORD c, DWORD bp, DWORD bw); + typedef void (GSLocalMemory::*writeFrame)(int x, int y, DWORD c, DWORD bp, DWORD bw); + typedef DWORD (GSLocalMemory::*readPixel)(int x, int y, DWORD bp, DWORD bw) const; + typedef DWORD (GSLocalMemory::*readTexel)(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + typedef void (GSLocalMemory::*writePixelAddr)(DWORD addr, DWORD c); + typedef void (GSLocalMemory::*writeFrameAddr)(DWORD addr, DWORD c); + typedef DWORD (GSLocalMemory::*readPixelAddr)(DWORD addr) const; + typedef DWORD (GSLocalMemory::*readTexelAddr)(DWORD addr, const GIFRegTEXA& TEXA) const; + typedef void (GSLocalMemory::*writeImage)(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + typedef void (GSLocalMemory::*readImage)(int& tx, int& ty, BYTE* dst, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) const; + typedef void (GSLocalMemory::*readTexture)(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + + typedef union + { + struct + { + pixelAddress pa, ba, pga, pgn; + readPixel rp; + readPixelAddr rpa; + writePixel wp; + writePixelAddr wpa; + readTexel rt, rtNP; + readTexelAddr rta; + writeFrameAddr wfa; + writeImage wi; + readImage ri; + readTexture rtx, rtxNP, rtxP; + DWORD bpp, pal, trbpp; + CSize bs, pgs; + int* rowOffset[8]; + }; + BYTE dummy[128]; + } psm_t; + + static psm_t m_psm[64]; + + static const int m_vmsize = 1024 * 1024 * 4; + + union {BYTE* m_vm8; WORD* m_vm16; DWORD* m_vm32;}; + + GSClut m_clut; + + struct Offset + { + GSVector4i row[2048]; // 0 | 0 | 0 | 0 + int* col[4]; // x | x+1 | x+2 | x+3 + DWORD hash; + }; + + struct Offset4 + { + // 16 bit offsets (m_vm16[...]) + + GSVector2i row[2048]; // f yn | z yn (n = 0 1 2 ...) + GSVector2i col[512]; // f xn | z xn (n = 0 4 8 ...) + DWORD hash; + }; + +protected: + static DWORD pageOffset32[32][32][64]; + static DWORD pageOffset32Z[32][32][64]; + static DWORD pageOffset16[32][64][64]; + static DWORD pageOffset16S[32][64][64]; + static DWORD pageOffset16Z[32][64][64]; + static DWORD pageOffset16SZ[32][64][64]; + static DWORD pageOffset8[32][64][128]; + static DWORD pageOffset4[32][128][128]; + + static int rowOffset32[2048]; + static int rowOffset32Z[2048]; + static int rowOffset16[2048]; + static int rowOffset16S[2048]; + static int rowOffset16Z[2048]; + static int rowOffset16SZ[2048]; + static int rowOffset8[2][2048]; + static int rowOffset4[2][2048]; + + __forceinline static DWORD Expand24To32(DWORD c, const GIFRegTEXA& TEXA) + { + return (((!TEXA.AEM | (c & 0xffffff)) ? TEXA.TA0 : 0) << 24) | (c & 0xffffff); + } + + __forceinline static DWORD Expand16To32(WORD c, const GIFRegTEXA& TEXA) + { + return (((c & 0x8000) ? TEXA.TA1 : (!TEXA.AEM | c) ? TEXA.TA0 : 0) << 24) | ((c & 0x7c00) << 9) | ((c & 0x03e0) << 6) | ((c & 0x001f) << 3); + } + + // TODO + + friend class GSClut; + + // + + CRBMapC m_omap; + CRBMapC m_o4map; + +public: + GSLocalMemory(); + virtual ~GSLocalMemory(); + + Offset* GetOffset(DWORD bp, DWORD bw, DWORD psm); + Offset4* GetOffset4(const GIFRegFRAME& FRAME, const GIFRegZBUF& ZBUF); + + // address + + static DWORD PageNumber32(int x, int y, DWORD bp, DWORD bw) + { + return (bp >> 5) + (y >> 5) * bw + (x >> 6); + } + + static DWORD PageNumber16(int x, int y, DWORD bp, DWORD bw) + { + return (bp >> 5) + (y >> 6) * bw + (x >> 6); + } + + static DWORD PageNumber8(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + + return (bp >> 5) + (y >> 6) * (bw >> 1) + (x >> 7); + } + + static DWORD PageNumber4(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + + return (bp >> 5) + (y >> 7) * (bw >> 1) + (x >> 7); + } + + static DWORD PageAddress32(int x, int y, DWORD bp, DWORD bw) + { + return PageNumber32(x, y, bp, bw) << 11; + } + + static DWORD PageAddress16(int x, int y, DWORD bp, DWORD bw) + { + return PageNumber16(x, y, bp, bw) << 12; + } + + static DWORD PageAddress8(int x, int y, DWORD bp, DWORD bw) + { + return PageNumber8(x, y, bp, bw) << 13; + } + + static DWORD PageAddress4(int x, int y, DWORD bp, DWORD bw) + { + return PageNumber4(x, y, bp, bw) << 14; + } + + static DWORD BlockAddress32(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + (y & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable32[(y >> 3) & 3][(x >> 3) & 7]; + return (page + block) << 6; + } + + static DWORD BlockAddress16(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16[(y >> 3) & 7][(x >> 4) & 3]; + return (page + block) << 7; + } + + static DWORD BlockAddress16S(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16S[(y >> 3) & 7][(x >> 4) & 3]; + return (page + block) << 7; + } + + static DWORD BlockAddress8(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = bp + ((y >> 1) & ~0x1f) * (bw >> 1) + ((x >> 2) & ~0x1f); + DWORD block = blockTable8[(y >> 4) & 3][(x >> 4) & 7]; + return (page + block) << 8; + } + + static DWORD BlockAddress4(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = bp + ((y >> 2) & ~0x1f) * (bw >> 1) + ((x >> 2) & ~0x1f); + DWORD block = blockTable4[(y >> 4) & 7][(x >> 5) & 3]; + return (page + block) << 9; + } + + static DWORD BlockAddress32Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + (y & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable32Z[(y >> 3) & 3][(x >> 3) & 7]; + return (page + block) << 6; + } + + static DWORD BlockAddress16Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16Z[(y >> 3) & 7][(x >> 4) & 3]; + return (page + block) << 7; + } + + static DWORD BlockAddress16SZ(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16SZ[(y >> 3) & 7][(x >> 4) & 3]; + return (page + block) << 7; + } + + static DWORD PixelAddressOrg32(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + (y & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable32[(y >> 3) & 3][(x >> 3) & 7]; + DWORD word = ((page + block) << 6) + columnTable32[y & 7][x & 7]; + ASSERT(word < 1024*1024); + return word; + } + + static DWORD PixelAddressOrg16(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16[(y >> 3) & 7][(x >> 4) & 3]; + DWORD word = ((page + block) << 7) + columnTable16[y & 7][x & 15]; + ASSERT(word < 1024*1024*2); + return word; + } + + static DWORD PixelAddressOrg16S(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16S[(y >> 3) & 7][(x >> 4) & 3]; + DWORD word = ((page + block) << 7) + columnTable16[y & 7][x & 15]; + ASSERT(word < 1024*1024*2); + return word; + } + + static DWORD PixelAddressOrg8(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = bp + ((y >> 1) & ~0x1f) * (bw >> 1) + ((x >> 2) & ~0x1f); + DWORD block = blockTable8[(y >> 4) & 3][(x >> 4) & 7]; + DWORD word = ((page + block) << 8) + columnTable8[y & 15][x & 15]; + ASSERT(word < 1024*1024*4); + return word; + } + + static DWORD PixelAddressOrg4(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = bp + ((y >> 2) & ~0x1f) * (bw >> 1) + ((x >> 2) & ~0x1f); + DWORD block = blockTable4[(y >> 4) & 7][(x >> 5) & 3]; + DWORD word = ((page + block) << 9) + columnTable4[y & 15][x & 31]; + ASSERT(word < 1024*1024*8); + return word; + } + + static DWORD PixelAddressOrg32Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + (y & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable32Z[(y >> 3) & 3][(x >> 3) & 7]; + DWORD word = ((page + block) << 6) + columnTable32[y & 7][x & 7]; + ASSERT(word < 1024*1024); + return word; + } + + static DWORD PixelAddressOrg16Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16Z[(y >> 3) & 7][(x >> 4) & 3]; + DWORD word = ((page + block) << 7) + columnTable16[y & 7][x & 15]; + ASSERT(word < 1024*1024*2); + return word; + } + + static DWORD PixelAddressOrg16SZ(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = bp + ((y >> 1) & ~0x1f) * bw + ((x >> 1) & ~0x1f); + DWORD block = blockTable16SZ[(y >> 3) & 7][(x >> 4) & 3]; + DWORD word = ((page + block) << 7) + columnTable16[y & 7][x & 15]; + ASSERT(word < 1024*1024*2); + return word; + } + + static __forceinline DWORD PixelAddress32(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 5) * bw + (x >> 6); + DWORD word = (page << 11) + pageOffset32[bp & 0x1f][y & 0x1f][x & 0x3f]; + return word; + } + + static __forceinline DWORD PixelAddress16(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 6) * bw + (x >> 6); + DWORD word = (page << 12) + pageOffset16[bp & 0x1f][y & 0x3f][x & 0x3f]; + return word; + } + + static __forceinline DWORD PixelAddress16S(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 6) * bw + (x >> 6); + DWORD word = (page << 12) + pageOffset16S[bp & 0x1f][y & 0x3f][x & 0x3f]; + return word; + } + + static __forceinline DWORD PixelAddress8(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = (bp >> 5) + (y >> 6) * (bw >> 1) + (x >> 7); + DWORD word = (page << 13) + pageOffset8[bp & 0x1f][y & 0x3f][x & 0x7f]; + return word; + } + + static __forceinline DWORD PixelAddress4(int x, int y, DWORD bp, DWORD bw) + { + ASSERT((bw & 1) == 0); + DWORD page = (bp >> 5) + (y >> 7) * (bw >> 1) + (x >> 7); + DWORD word = (page << 14) + pageOffset4[bp & 0x1f][y & 0x7f][x & 0x7f]; + return word; + } + + static __forceinline DWORD PixelAddress32Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 5) * bw + (x >> 6); + DWORD word = (page << 11) + pageOffset32Z[bp & 0x1f][y & 0x1f][x & 0x3f]; + return word; + } + + static __forceinline DWORD PixelAddress16Z(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 6) * bw + (x >> 6); + DWORD word = (page << 12) + pageOffset16Z[bp & 0x1f][y & 0x3f][x & 0x3f]; + return word; + } + + static __forceinline DWORD PixelAddress16SZ(int x, int y, DWORD bp, DWORD bw) + { + DWORD page = (bp >> 5) + (y >> 6) * bw + (x >> 6); + DWORD word = (page << 12) + pageOffset16SZ[bp & 0x1f][y & 0x3f][x & 0x3f]; + return word; + } + + // pixel R/W + + __forceinline DWORD ReadPixel32(DWORD addr) const + { + return m_vm32[addr]; + } + + __forceinline DWORD ReadPixel24(DWORD addr) const + { + return m_vm32[addr] & 0x00ffffff; + } + + __forceinline DWORD ReadPixel16(DWORD addr) const + { + return (DWORD)m_vm16[addr]; + } + + __forceinline DWORD ReadPixel8(DWORD addr) const + { + return (DWORD)m_vm8[addr]; + } + + __forceinline DWORD ReadPixel4(DWORD addr) const + { + return (m_vm8[addr >> 1] >> ((addr & 1) << 2)) & 0x0f; + } + + __forceinline DWORD ReadPixel8H(DWORD addr) const + { + return m_vm32[addr] >> 24; + } + + __forceinline DWORD ReadPixel4HL(DWORD addr) const + { + return (m_vm32[addr] >> 24) & 0x0f; + } + + __forceinline DWORD ReadPixel4HH(DWORD addr) const + { + return (m_vm32[addr] >> 28) & 0x0f; + } + + __forceinline DWORD ReadFrame24(DWORD addr) const + { + return 0x80000000 | (m_vm32[addr] & 0xffffff); + } + + __forceinline DWORD ReadFrame16(DWORD addr) const + { + DWORD c = (DWORD)m_vm16[addr]; + + return ((c & 0x8000) << 16) | ((c & 0x7c00) << 9) | ((c & 0x03e0) << 6) | ((c & 0x001f) << 3); + } + + __forceinline DWORD ReadPixel32(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel32(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel24(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel24(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel16(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel16(PixelAddress16(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel16S(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel16(PixelAddress16S(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel8(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel8(PixelAddress8(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel4(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel4(PixelAddress4(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel8H(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel8H(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel4HL(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel4HL(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel4HH(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel4HH(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel32Z(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel32(PixelAddress32Z(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel24Z(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel24(PixelAddress32Z(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel16Z(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel16(PixelAddress16Z(x, y, bp, bw)); + } + + __forceinline DWORD ReadPixel16SZ(int x, int y, DWORD bp, DWORD bw) const + { + return ReadPixel16(PixelAddress16SZ(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame24(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame24(PixelAddress32(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame16(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame16(PixelAddress16(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame16S(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame16(PixelAddress16S(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame24Z(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame24(PixelAddress32Z(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame16Z(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame16(PixelAddress16Z(x, y, bp, bw)); + } + + __forceinline DWORD ReadFrame16SZ(int x, int y, DWORD bp, DWORD bw) const + { + return ReadFrame16(PixelAddress16SZ(x, y, bp, bw)); + } + + __forceinline void WritePixel32(DWORD addr, DWORD c) + { + m_vm32[addr] = c; + } + + __forceinline void WritePixel24(DWORD addr, DWORD c) + { + m_vm32[addr] = (m_vm32[addr] & 0xff000000) | (c & 0x00ffffff); + } + + __forceinline void WritePixel16(DWORD addr, DWORD c) + { + m_vm16[addr] = (WORD)c; + } + + __forceinline void WritePixel8(DWORD addr, DWORD c) + { + m_vm8[addr] = (BYTE)c; + } + + __forceinline void WritePixel4(DWORD addr, DWORD c) + { + int shift = (addr & 1) << 2; addr >>= 1; + + m_vm8[addr] = (BYTE)((m_vm8[addr] & (0xf0 >> shift)) | ((c & 0x0f) << shift)); + } + + __forceinline void WritePixel8H(DWORD addr, DWORD c) + { + m_vm32[addr] = (m_vm32[addr] & 0x00ffffff) | (c << 24); + } + + __forceinline void WritePixel4HL(DWORD addr, DWORD c) + { + m_vm32[addr] = (m_vm32[addr] & 0xf0ffffff) | ((c & 0x0f) << 24); + } + + __forceinline void WritePixel4HH(DWORD addr, DWORD c) + { + m_vm32[addr] = (m_vm32[addr] & 0x0fffffff) | ((c & 0x0f) << 28); + } + + __forceinline void WriteFrame16(DWORD addr, DWORD c) + { + DWORD rb = c & 0x00f800f8; + DWORD ga = c & 0x8000f800; + + WritePixel16(addr, (ga >> 16) | (rb >> 9) | (ga >> 6) | (rb >> 3)); + } + + __forceinline void WritePixel32(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel32(PixelAddress32(x, y, bp, bw), c); + } + + __forceinline void WritePixel24(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel24(PixelAddress32(x, y, bp, bw), c); + } + + __forceinline void WritePixel16(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel16(PixelAddress16(x, y, bp, bw), c); + } + + __forceinline void WritePixel16S(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel16(PixelAddress16S(x, y, bp, bw), c); + } + + __forceinline void WritePixel8(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel8(PixelAddress8(x, y, bp, bw), c); + } + + __forceinline void WritePixel4(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel4(PixelAddress4(x, y, bp, bw), c); + } + + __forceinline void WritePixel8H(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel8H(PixelAddress32(x, y, bp, bw), c); + } + + __forceinline void WritePixel4HL(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel4HL(PixelAddress32(x, y, bp, bw), c); + } + + __forceinline void WritePixel4HH(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel4HH(PixelAddress32(x, y, bp, bw), c); + } + + __forceinline void WritePixel32Z(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel32(PixelAddress32Z(x, y, bp, bw), c); + } + + __forceinline void WritePixel24Z(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel24(PixelAddress32Z(x, y, bp, bw), c); + } + + __forceinline void WritePixel16Z(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel16(PixelAddress16Z(x, y, bp, bw), c); + } + + __forceinline void WritePixel16SZ(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WritePixel16(PixelAddress16SZ(x, y, bp, bw), c); + } + + __forceinline void WriteFrame16(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WriteFrame16(PixelAddress16(x, y, bp, bw), c); + } + + __forceinline void WriteFrame16S(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WriteFrame16(PixelAddress16S(x, y, bp, bw), c); + } + + __forceinline void WriteFrame16Z(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WriteFrame16(PixelAddress16Z(x, y, bp, bw), c); + } + + __forceinline void WriteFrame16SZ(int x, int y, DWORD c, DWORD bp, DWORD bw) + { + WriteFrame16(PixelAddress16SZ(x, y, bp, bw), c); + } + + __forceinline DWORD ReadTexel32(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_vm32[addr]; + } + + __forceinline DWORD ReadTexel24(DWORD addr, const GIFRegTEXA& TEXA) const + { + return Expand24To32(m_vm32[addr], TEXA); + } + + __forceinline DWORD ReadTexel16(DWORD addr, const GIFRegTEXA& TEXA) const + { + return Expand16To32(m_vm16[addr], TEXA); + } + + __forceinline DWORD ReadTexel8(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_clut[ReadPixel8(addr)]; + } + + __forceinline DWORD ReadTexel4(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_clut[ReadPixel4(addr)]; + } + + __forceinline DWORD ReadTexel8H(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_clut[ReadPixel8H(addr)]; + } + + __forceinline DWORD ReadTexel4HL(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_clut[ReadPixel4HL(addr)]; + } + + __forceinline DWORD ReadTexel4HH(DWORD addr, const GIFRegTEXA& TEXA) const + { + return m_clut[ReadPixel4HH(addr)]; + } + + __forceinline DWORD ReadTexel32(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel32(PixelAddress32(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel24(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel24(PixelAddress32(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel16(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel16(PixelAddress16(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel16S(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel16(PixelAddress16S(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel8(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel8(PixelAddress8(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel4(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel4(PixelAddress4(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel8H(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel8H(PixelAddress32(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel4HL(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel4HL(PixelAddress32(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel4HH(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel4HH(PixelAddress32(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel32Z(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel32(PixelAddress32Z(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel24Z(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel24(PixelAddress32Z(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel16Z(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel16(PixelAddress16Z(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel16SZ(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadTexel16(PixelAddress16SZ(x, y, TEX0.TBP0, TEX0.TBW), TEXA); + } + + __forceinline DWORD ReadTexel16NP(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadPixel16(x, y, TEX0.TBP0, TEX0.TBW); + } + + __forceinline DWORD ReadTexel16SNP(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadPixel16S(x, y, TEX0.TBP0, TEX0.TBW); + } + + __forceinline DWORD ReadTexel16ZNP(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadPixel16Z(x, y, TEX0.TBP0, TEX0.TBW); + } + + __forceinline DWORD ReadTexel16SZNP(int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + return ReadPixel16SZ(x, y, TEX0.TBP0, TEX0.TBW); + } + + // + + __forceinline DWORD PixelAddressX(int PSM, int x, int y, DWORD bp, DWORD bw) + { + switch(PSM) + { + case PSM_PSMCT32: return PixelAddress32(x, y, bp, bw); + case PSM_PSMCT24: return PixelAddress32(x, y, bp, bw); + case PSM_PSMCT16: return PixelAddress16(x, y, bp, bw); + case PSM_PSMCT16S: return PixelAddress16S(x, y, bp, bw); + case PSM_PSMT8: return PixelAddress8(x, y, bp, bw); + case PSM_PSMT4: return PixelAddress4(x, y, bp, bw); + case PSM_PSMT8H: return PixelAddress32(x, y, bp, bw); + case PSM_PSMT4HL: return PixelAddress32(x, y, bp, bw); + case PSM_PSMT4HH: return PixelAddress32(x, y, bp, bw); + case PSM_PSMZ32: return PixelAddress32Z(x, y, bp, bw); + case PSM_PSMZ24: return PixelAddress32Z(x, y, bp, bw); + case PSM_PSMZ16: return PixelAddress16Z(x, y, bp, bw); + case PSM_PSMZ16S: return PixelAddress16SZ(x, y, bp, bw); + default: ASSERT(0); return PixelAddress32(x, y, bp, bw); + } + } + + __forceinline DWORD ReadPixelX(int PSM, DWORD addr) const + { + switch(PSM) + { + case PSM_PSMCT32: return ReadPixel32(addr); + case PSM_PSMCT24: return ReadPixel24(addr); + case PSM_PSMCT16: return ReadPixel16(addr); + case PSM_PSMCT16S: return ReadPixel16(addr); + case PSM_PSMT8: return ReadPixel8(addr); + case PSM_PSMT4: return ReadPixel4(addr); + case PSM_PSMT8H: return ReadPixel8H(addr); + case PSM_PSMT4HL: return ReadPixel4HL(addr); + case PSM_PSMT4HH: return ReadPixel4HH(addr); + case PSM_PSMZ32: return ReadPixel32(addr); + case PSM_PSMZ24: return ReadPixel24(addr); + case PSM_PSMZ16: return ReadPixel16(addr); + case PSM_PSMZ16S: return ReadPixel16(addr); + default: ASSERT(0); return ReadPixel32(addr); + } + } + + __forceinline DWORD ReadFrameX(int PSM, DWORD addr) const + { + switch(PSM) + { + case PSM_PSMCT32: return ReadPixel32(addr); + case PSM_PSMCT24: return ReadFrame24(addr); + case PSM_PSMCT16: return ReadFrame16(addr); + case PSM_PSMCT16S: return ReadFrame16(addr); + case PSM_PSMZ32: return ReadPixel32(addr); + case PSM_PSMZ24: return ReadFrame24(addr); + case PSM_PSMZ16: return ReadFrame16(addr); + case PSM_PSMZ16S: return ReadFrame16(addr); + default: ASSERT(0); return ReadPixel32(addr); + } + } + + __forceinline DWORD ReadTexelX(int PSM, DWORD addr, const GIFRegTEXA& TEXA) const + { + switch(PSM) + { + case PSM_PSMCT32: return ReadTexel32(addr, TEXA); + case PSM_PSMCT24: return ReadTexel24(addr, TEXA); + case PSM_PSMCT16: return ReadTexel16(addr, TEXA); + case PSM_PSMCT16S: return ReadTexel16(addr, TEXA); + case PSM_PSMT8: return ReadTexel8(addr, TEXA); + case PSM_PSMT4: return ReadTexel4(addr, TEXA); + case PSM_PSMT8H: return ReadTexel8H(addr, TEXA); + case PSM_PSMT4HL: return ReadTexel4HL(addr, TEXA); + case PSM_PSMT4HH: return ReadTexel4HH(addr, TEXA); + case PSM_PSMZ32: return ReadTexel32(addr, TEXA); + case PSM_PSMZ24: return ReadTexel24(addr, TEXA); + case PSM_PSMZ16: return ReadTexel16(addr, TEXA); + case PSM_PSMZ16S: return ReadTexel16(addr, TEXA); + default: ASSERT(0); return ReadTexel32(addr, TEXA); + } + } + + __forceinline DWORD ReadTexelX(int PSM, int x, int y, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const + { + switch(PSM) + { + case PSM_PSMCT32: return ReadTexel32(x, y, TEX0, TEXA); + case PSM_PSMCT24: return ReadTexel24(x, y, TEX0, TEXA); + case PSM_PSMCT16: return ReadTexel16(x, y, TEX0, TEXA); + case PSM_PSMCT16S: return ReadTexel16(x, y, TEX0, TEXA); + case PSM_PSMT8: return ReadTexel8(x, y, TEX0, TEXA); + case PSM_PSMT4: return ReadTexel4(x, y, TEX0, TEXA); + case PSM_PSMT8H: return ReadTexel8H(x, y, TEX0, TEXA); + case PSM_PSMT4HL: return ReadTexel4HL(x, y, TEX0, TEXA); + case PSM_PSMT4HH: return ReadTexel4HH(x, y, TEX0, TEXA); + case PSM_PSMZ32: return ReadTexel32Z(x, y, TEX0, TEXA); + case PSM_PSMZ24: return ReadTexel24Z(x, y, TEX0, TEXA); + case PSM_PSMZ16: return ReadTexel16Z(x, y, TEX0, TEXA); + case PSM_PSMZ16S: return ReadTexel16Z(x, y, TEX0, TEXA); + default: ASSERT(0); return ReadTexel32(x, y, TEX0, TEXA); + } + } + + __forceinline void WritePixelX(int PSM, DWORD addr, DWORD c) + { + switch(PSM) + { + case PSM_PSMCT32: WritePixel32(addr, c); break; + case PSM_PSMCT24: WritePixel24(addr, c); break; + case PSM_PSMCT16: WritePixel16(addr, c); break; + case PSM_PSMCT16S: WritePixel16(addr, c); break; + case PSM_PSMT8: WritePixel8(addr, c); break; + case PSM_PSMT4: WritePixel4(addr, c); break; + case PSM_PSMT8H: WritePixel8H(addr, c); break; + case PSM_PSMT4HL: WritePixel4HL(addr, c); break; + case PSM_PSMT4HH: WritePixel4HH(addr, c); break; + case PSM_PSMZ32: WritePixel32(addr, c); break; + case PSM_PSMZ24: WritePixel24(addr, c); break; + case PSM_PSMZ16: WritePixel16(addr, c); break; + case PSM_PSMZ16S: WritePixel16(addr, c); break; + default: ASSERT(0); WritePixel32(addr, c); break; + } + } + + __forceinline void WriteFrameX(int PSM, DWORD addr, DWORD c) + { + switch(PSM) + { + case PSM_PSMCT32: WritePixel32(addr, c); break; + case PSM_PSMCT24: WritePixel24(addr, c); break; + case PSM_PSMCT16: WriteFrame16(addr, c); break; + case PSM_PSMCT16S: WriteFrame16(addr, c); break; + case PSM_PSMZ32: WritePixel32(addr, c); break; + case PSM_PSMZ24: WritePixel24(addr, c); break; + case PSM_PSMZ16: WriteFrame16(addr, c); break; + case PSM_PSMZ16S: WriteFrame16(addr, c); break; + default: ASSERT(0); WritePixel32(addr, c); break; + } + } + + // FillRect + + bool FillRect(const GSVector4i& r, DWORD c, DWORD psm, DWORD bp, DWORD bw); + + // + + template + void WriteImageColumn(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF); + + template + void WriteImageBlock(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF); + + template + void WriteImageLeftRight(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF); + + template + void WriteImageTopBottom(int l, int r, int y, int h, BYTE* src, int srcpitch, const GIFRegBITBLTBUF& BITBLTBUF); + + template + void WriteImage(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + + void WriteImage24(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + void WriteImage8H(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + void WriteImage4HL(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + void WriteImage4HH(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + void WriteImage24Z(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + void WriteImageX(int& tx, int& ty, BYTE* src, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG); + + // TODO: ReadImage32/24/... + + void ReadImageX(int& tx, int& ty, BYTE* dst, int len, GIFRegBITBLTBUF& BITBLTBUF, GIFRegTRXPOS& TRXPOS, GIFRegTRXREG& TRXREG) const; + + // + + void ReadTexture32(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture24(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16S(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture8(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture8H(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HL(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HH(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture32Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture24Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16Z(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16SZ(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + + void ReadTexture(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP); + void ReadTextureNC(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP); + + // 32/16 + + void ReadTexture16NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16SNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture8NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4NP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture8HNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HLNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HHNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16ZNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture16SZNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + + void ReadTextureNP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP); + void ReadTextureNPNC(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP); + + // 32/8 + + void ReadTexture8P(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4P(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture8HP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HLP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + void ReadTexture4HHP(const CRect& r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA) const; + + // + + static DWORD m_xtbl[1024], m_ytbl[1024]; + + template void ReadTexture(CRect r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const GIFRegCLAMP& CLAMP, readTexel rt, readTexture rtx); + template void ReadTextureNC(CRect r, BYTE* dst, int dstpitch, const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, readTexel rt, readTexture rtx); + + // + + HRESULT SaveBMP(LPCTSTR fn, DWORD bp, DWORD bw, DWORD psm, int w, int h); +}; + +#pragma warning(default: 4244) \ No newline at end of file diff --git a/plugins/GSdx/GSPerfMon.cpp b/plugins/GSdx/GSPerfMon.cpp new file mode 100644 index 0000000000..9164d8c9ea --- /dev/null +++ b/plugins/GSdx/GSPerfMon.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSPerfMon.h" + +extern "C" unsigned __int64 __rdtsc(); + +GSPerfMon::GSPerfMon() + : m_total(0) + , m_begin(0) + , m_frame(0) + , m_lastframe(0) + , m_count(0) +{ + memset(m_counters, 0, sizeof(m_counters)); + memset(m_stats, 0, sizeof(m_stats)); + memset(m_warnings, 0, sizeof(m_warnings)); +} + +void GSPerfMon::Put(counter_t c, double val) +{ + if(c == Frame) + { + clock_t now = clock(); + + if(m_lastframe != 0) + { + m_counters[c] += now - m_lastframe; + } + + m_lastframe = now; + m_frame++; + m_count++; + } + else + { + m_counters[c] += val; + } +} + +void GSPerfMon::Update() +{ + if(m_count > 0) + { + for(int i = 0; i < countof(m_counters); i++) + { + m_stats[i] = m_counters[i] / m_count; + } + + m_count = 0; + } + + memset(m_counters, 0, sizeof(m_counters)); +} + +void GSPerfMon::Start() +{ + m_start = __rdtsc(); + + if(m_begin == 0) + { + m_begin = m_start; + } +} + +void GSPerfMon::Stop() +{ + if(m_start > 0) + { + m_total += __rdtsc() - m_start; + m_start = 0; + } +} + +int GSPerfMon::CPU() +{ + int percent = (int)(100 * m_total / (__rdtsc() - m_begin)); + + m_begin = 0; + m_start = 0; + m_total = 0; + + return percent; +} \ No newline at end of file diff --git a/plugins/GSdx/GSPerfMon.h b/plugins/GSdx/GSPerfMon.h new file mode 100644 index 0000000000..f1e7bb6d35 --- /dev/null +++ b/plugins/GSdx/GSPerfMon.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +class GSPerfMon +{ +public: + enum counter_t {Frame, Prim, Draw, Swizzle, Unswizzle, Fillrate, Quad, CounterLast}; + enum warning_t {DATE, PABE, ABE, COLCLAMP, DepthTexture, WarningLast}; + +protected: + double m_counters[CounterLast]; + double m_stats[CounterLast]; + bool m_warnings[WarningLast]; + UINT64 m_begin, m_total, m_start, m_frame; + clock_t m_lastframe; + int m_count; + + void Start(); + void Stop(); + + friend class GSPerfMonAutoTimer; + +public: + GSPerfMon(); + + void SetFrame(UINT64 frame) {m_frame = frame;} + UINT64 GetFrame() {return m_frame;} + void Put(counter_t c, double val = 0); + double Get(counter_t c) {return m_stats[c];} + void Put(warning_t c) {m_warnings[c] = true;} + bool Get(warning_t c) {bool b = m_warnings[c]; m_warnings[c] = false; return b;} + void Update(); + int CPU(); +}; + +class GSPerfMonAutoTimer +{ + GSPerfMon* m_pm; + +public: + GSPerfMonAutoTimer(GSPerfMon& pm) {(m_pm = &pm)->Start();} + ~GSPerfMonAutoTimer() {m_pm->Stop();} +}; diff --git a/plugins/GSdx/GSRasterizer.cpp b/plugins/GSdx/GSRasterizer.cpp new file mode 100644 index 0000000000..0362ef3c69 --- /dev/null +++ b/plugins/GSdx/GSRasterizer.cpp @@ -0,0 +1,726 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSRasterizer.h" + +GSRasterizer::GSRasterizer(IDrawScanline* ds, int id, int threads) + : m_ds(ds) + , m_id(id) + , m_threads(threads) +{ +} + +GSRasterizer::~GSRasterizer() +{ + delete m_ds; +} + +void GSRasterizer::Draw(const GSRasterizerData* data) +{ + m_dsf.sl = NULL; + m_dsf.sr = NULL; + m_dsf.sp = NULL; + + m_ds->BeginDraw(data, &m_dsf); + + const GSVector4i scissor = data->scissor; + const GSVertexSW* vertices = data->vertices; + const int count = data->count; + + m_stats.Reset(); + + __int64 start = __rdtsc(); + + switch(data->primclass) + { + case GS_POINT_CLASS: + m_stats.prims = count; + for(int i = 0; i < count; i++) DrawPoint(&vertices[i], scissor); + break; + case GS_LINE_CLASS: + ASSERT(!(count & 1)); + m_stats.prims = count / 2; + for(int i = 0; i < count; i += 2) DrawLine(&vertices[i], scissor); + break; + case GS_TRIANGLE_CLASS: + ASSERT(!(count % 3)); + m_stats.prims = count / 3; + for(int i = 0; i < count; i += 3) DrawTriangle(&vertices[i], scissor); + break; + case GS_SPRITE_CLASS: + ASSERT(!(count & 1)); + m_stats.prims = count / 2; + for(int i = 0; i < count; i += 2) DrawSprite(&vertices[i], scissor); + break; + default: + __assume(0); + } + + m_stats.ticks = __rdtsc() - start; + + m_ds->EndDraw(m_stats); +} + +void GSRasterizer::GetStats(GSRasterizerStats& stats) +{ + stats = m_stats; +} + +void GSRasterizer::DrawPoint(const GSVertexSW* v, const GSVector4i& scissor) +{ + // TODO: round to closest for point, prestep for line + + GSVector4i p(v->p); + + if(scissor.x <= p.x && p.x < scissor.z && scissor.y <= p.y && p.y < scissor.w) + { + if((p.y % m_threads) == m_id) + { + (m_ds->*m_dsf.sp)(v, *v); + + (m_ds->*m_dsf.sl)(p.y, p.x, p.x + 1, *v); + + m_stats.pixels++; + } + } +} + +void GSRasterizer::DrawLine(const GSVertexSW* v, const GSVector4i& scissor) +{ + GSVertexSW dv = v[1] - v[0]; + + GSVector4 dp = dv.p.abs(); + GSVector4i dpi(dp); + + if(dpi.y == 0) + { + if(dpi.x > 0) + { + // shortcut for horizontal lines + + GSVector4 mask = (v[0].p > v[1].p).xxxx(); + + GSVertexSW l, dl; + + l.p = v[0].p.blend8(v[1].p, mask); + l.t = v[0].t.blend8(v[1].t, mask); + l.c = v[0].c.blend8(v[1].c, mask); + + GSVector4 r; + + r = v[1].p.blend8(v[0].p, mask); + + GSVector4i p(l.p); + + if(scissor.y <= p.y && p.y < scissor.w) + { + GSVertexSW dscan = dv / dv.p.xxxx(); + + (m_ds->*m_dsf.sp)(v, dscan); + + l.p = l.p.upl(r).xyzw(l.p); // r.x => l.y + + DrawTriangleSection(p.y, p.y + 1, l, dl, dscan, scissor); + } + } + + return; + } + + int i = dpi.x > dpi.y ? 0 : 1; + + GSVertexSW edge = v[0]; + GSVertexSW dedge = dv / dp.v[i]; + + // TODO: prestep + clip with the scissor + + int steps = dpi.v[i]; + + while(steps-- > 0) + { + DrawPoint(&edge, scissor); + + edge += dedge; + } +} + +static const int s_abc[8][4] = +{ + {0, 1, 2, 0}, + {1, 0, 2, 0}, + {0, 0, 0, 0}, + {1, 2, 0, 0}, + {0, 2, 1, 0}, + {0, 0, 0, 0}, + {2, 0, 1, 0}, + {2, 1, 0, 0}, +}; + +void GSRasterizer::DrawTriangle(const GSVertexSW* vertices, const GSVector4i& scissor) +{ + GSVertexSW v[3]; + + GSVector4 aabb = vertices[0].p.yyyy(vertices[1].p); + GSVector4 bccb = vertices[1].p.yyyy(vertices[2].p).xzzx(); + + int i = (aabb > bccb).mask() & 7; + + v[0] = vertices[s_abc[i][0]]; + v[1] = vertices[s_abc[i][1]]; + v[2] = vertices[s_abc[i][2]]; + + aabb = v[0].p.yyyy(v[1].p); + bccb = v[1].p.yyyy(v[2].p).xzzx(); + + i = (aabb == bccb).mask() & 7; + + switch(i) + { + case 0: // a < b < c + DrawTriangleTopBottom(v, scissor); + break; + case 1: // a == b < c + DrawTriangleBottom(v, scissor); + break; + case 4: // a < b == c + DrawTriangleTop(v, scissor); + break; + case 7: // a == b == c + break; + default: + __assume(0); + } +} + +void GSRasterizer::DrawTriangleTop(GSVertexSW* v, const GSVector4i& scissor) +{ + GSVertexSW longest; + + longest.p = v[2].p - v[1].p; + + int i = (longest.p > GSVector4::zero()).upl(longest.p == GSVector4::zero()).mask(); + + if(i & 2) return; + + i &= 1; + + GSVertexSW& l = v[0]; + GSVector4& r = v[0].p; + + GSVector4i tb(l.p.xyxy(v[2].p).ceil()); + + int top = tb.extract32<1>(); + int bottom = tb.extract32<3>(); + + if(top < scissor.y) top = scissor.y; + if(bottom > scissor.w) bottom = scissor.w; + if(top >= bottom) return; + + longest.t = v[2].t - v[1].t; + longest.c = v[2].c - v[1].c; + + GSVertexSW dscan = longest * longest.p.xxxx().rcp(); + + GSVertexSW vl = v[2 - i] - l; + GSVector4 vr = v[1 + i].p - r; + + GSVertexSW dl = vl / vl.p.yyyy(); + GSVector4 dr = vr / vr.yyyy(); + + float py = (float)top - l.p.y; + + l.p = l.p.upl(r).xyzw(l.p); // r.x => l.y + dl.p = dl.p.upl(dr).xyzw(dl.p); // dr.x => dl.y + + if(py > 0) l += dl * py; + + (m_ds->*m_dsf.sp)(v, dscan); + + DrawTriangleSection(top, bottom, l, dl, dscan, scissor); +} + +void GSRasterizer::DrawTriangleBottom(GSVertexSW* v, const GSVector4i& scissor) +{ + GSVertexSW longest; + + longest.p = v[1].p - v[0].p; + + int i = (longest.p > GSVector4::zero()).upl(longest.p == GSVector4::zero()).mask(); + + if(i & 2) return; + + i &= 1; + + GSVertexSW& l = v[1 - i]; + GSVector4& r = v[i].p; + + GSVector4i tb(l.p.xyxy(v[2].p).ceil()); + + int top = tb.extract32<1>(); + int bottom = tb.extract32<3>(); + + if(top < scissor.y) top = scissor.y; + if(bottom > scissor.w) bottom = scissor.w; + if(top >= bottom) return; + + longest.t = v[1].t - v[0].t; + longest.c = v[1].c - v[0].c; + + GSVertexSW dscan = longest * longest.p.xxxx().rcp(); + + GSVertexSW vl = v[2] - l; + GSVector4 vr = v[2].p - r; + + GSVertexSW dl = vl / vl.p.yyyy(); + GSVector4 dr = vr / vr.yyyy(); + + float py = (float)top - l.p.y; + + l.p = l.p.upl(r).xyzw(l.p); // r.x => l.y + dl.p = dl.p.upl(dr).xyzw(dl.p); // dr.x => dl.y + + if(py > 0) l += dl * py; + + (m_ds->*m_dsf.sp)(v, dscan); + + DrawTriangleSection(top, bottom, l, dl, dscan, scissor); +} + +void GSRasterizer::DrawTriangleTopBottom(GSVertexSW* v, const GSVector4i& scissor) +{ + GSVertexSW dv[3]; + + dv[0] = v[1] - v[0]; + dv[1] = v[2] - v[0]; + + GSVertexSW longest = v[0] + dv[1] * (dv[0].p / dv[1].p).yyyy() - v[1]; + + int i = (longest.p > GSVector4::zero()).upl(longest.p == GSVector4::zero()).mask(); + + if(i & 2) return; + + i &= 1; + + GSVertexSW dscan = longest * longest.p.xxxx().rcp(); + + (m_ds->*m_dsf.sp)(v, dscan); + + GSVertexSW& l = v[0]; + GSVector4 r = v[0].p; + + GSVertexSW dl; + GSVector4 dr; + + dl = dv[1 - i] / dv[1 - i].p.yyyy(); + dr = dv[i].p / dv[i].p.yyyy(); + + GSVector4i tb(v[0].p.yyyy(v[1].p).xzyy(v[2].p).ceil()); + + int top = tb.x; + int bottom = tb.y; + + if(top < scissor.y) top = scissor.y; + if(bottom > scissor.w) bottom = scissor.w; + + float py = (float)top - l.p.y; + + if(py > 0) + { + GSVector4 dy(py); + + l += dl * dy; + r += dr * dy; + } + + if(top < bottom) + { + DrawTriangleSection(top, bottom, l, dl, r, dr, dscan, scissor); + } + + if(i) + { + l = v[1]; + + dv[2] = v[2] - v[1]; + + dl = dv[2] / dv[2].p.yyyy(); + } + else + { + r = v[1].p; + + dv[2].p = v[2].p - v[1].p; + + dr = dv[2].p / dv[2].p.yyyy(); + } + + top = tb.y; + bottom = tb.z; + + if(top < scissor.y) top = scissor.y; + if(bottom > scissor.w) bottom = scissor.w; + + if(top < bottom) + { + py = (float)top - l.p.y; + + if(py > 0) l += dl * py; + + py = (float)top - r.y; + + if(py > 0) r += dr * py; + + l.p = l.p.upl(r).xyzw(l.p); // r.x => l.y + dl.p = dl.p.upl(dr).xyzw(dl.p); // dr.x => dl.y + + DrawTriangleSection(top, bottom, l, dl, dscan, scissor); + } +} + +void GSRasterizer::DrawTriangleSection(int top, int bottom, GSVertexSW& l, const GSVertexSW& dl, GSVector4& r, const GSVector4& dr, const GSVertexSW& dscan, const GSVector4i& scissor) +{ + ASSERT(top < bottom); + + while(1) + { + do + { + if((top % m_threads) == m_id) + { + GSVector4i lr(l.p.xyxy(r).ceil()); + + int left = lr.extract32<0>(); + int right = lr.extract32<2>(); + + if(left < scissor.x) left = scissor.x; + if(right > scissor.z) right = scissor.z; + + int pixels = right - left; + + if(pixels > 0) + { + m_stats.pixels += pixels; + + GSVertexSW scan; + + float px = (float)left - l.p.x; + + if(px > 0) + { + scan = l + dscan * px; + } + else + { + scan = l; + } + + (m_ds->*m_dsf.sl)(top, left, right, scan); + } + } + } + while(0); + + if(++top >= bottom) break; + + l += dl; + r += dr; + } +} + + +void GSRasterizer::DrawTriangleSection(int top, int bottom, GSVertexSW& l, const GSVertexSW& dl, const GSVertexSW& dscan, const GSVector4i& scissor) +{ + ASSERT(top < bottom); + + while(1) + { + do + { + if((top % m_threads) == m_id) + { + GSVector4i lr(l.p.ceil()); + + int left = lr.extract32<0>(); + int right = lr.extract32<1>(); + + if(left < scissor.x) left = scissor.x; + if(right > scissor.z) right = scissor.z; + + int pixels = right - left; + + if(pixels > 0) + { + m_stats.pixels += pixels; + + GSVertexSW scan; + + float px = (float)left - l.p.x; + + if(px > 0) + { + scan = l + dscan * px; + } + else + { + scan = l; + } + + (m_ds->*m_dsf.sl)(top, left, right, scan); + } + } + } + while(0); + + if(++top >= bottom) break; + + l += dl; + } +} + +void GSRasterizer::DrawSprite(const GSVertexSW* vertices, const GSVector4i& scissor) +{ + GSVertexSW v[2]; + + GSVector4 mask = (vertices[0].p < vertices[1].p).xyzw(GSVector4::zero()); + + v[0].p = vertices[1].p.blend8(vertices[0].p, mask); + v[0].t = vertices[1].t.blend8(vertices[0].t, mask); + v[0].c = vertices[1].c; + + v[1].p = vertices[0].p.blend8(vertices[1].p, mask); + v[1].t = vertices[0].t.blend8(vertices[1].t, mask); + + GSVector4i r(v[0].p.xyxy(v[1].p).ceil()); + + int& top = r.y; + int& bottom = r.w; + + int& left = r.x; + int& right = r.z; + + #if _M_SSE >= 0x401 + + r = r.sat_i32(scissor); + + if((r < r.zwzw()).mask() != 0x00ff) return; + + #else + + if(top < scissor.y) top = scissor.y; + if(bottom > scissor.w) bottom = scissor.w; + if(top >= bottom) return; + + if(left < scissor.x) left = scissor.x; + if(right > scissor.z) right = scissor.z; + if(left >= right) return; + + #endif + + GSVertexSW scan = v[0]; + + if(m_dsf.sr) + { + if(m_id == 0) + { + (m_ds->*m_dsf.sr)(r, scan); + + m_stats.pixels += (r.z - r.x) * (r.w - r.y); + } + + return; + } + + GSVector4 zero = GSVector4::zero(); + + GSVertexSW dedge, dscan; + + dedge.p = zero; + dscan.p = zero; + + dedge.c = zero; + dscan.c = zero; + + GSVertexSW dv = v[1] - v[0]; + + dedge.t = (dv.t / dv.p.yyyy()).xyxy(zero).wyww(); + dscan.t = (dv.t / dv.p.xxxx()).xyxy(zero).xwww(); + + if(scan.p.y < (float)top) scan.t += dedge.t * ((float)top - scan.p.y); + if(scan.p.x < (float)left) scan.t += dscan.t * ((float)left - scan.p.x); + + (m_ds->*m_dsf.sp)(v, dscan); + + for(; top < bottom; top++, scan.t += dedge.t) + { + if((top % m_threads) == m_id) + { + (m_ds->*m_dsf.sl)(top, left, right, scan); + + m_stats.pixels += right - left; + } + } +} + +// + +GSRasterizerMT::GSRasterizerMT(IDrawScanline* ds, int id, int threads, long* sync) + : GSRasterizer(ds, id, threads) + , m_sync(sync) + , m_exit(false) + , m_ThreadId(0) + , m_hThread(NULL) + , m_data(NULL) +{ + if(id > 0) + { + m_hThread = CreateThread(NULL, 0, StaticThreadProc, (LPVOID)this, 0, &m_ThreadId); + } +} + +GSRasterizerMT::~GSRasterizerMT() +{ + if(m_hThread != NULL) + { + m_exit = true; + + if(WaitForSingleObject(m_hThread, 5000) != WAIT_OBJECT_0) + { + TerminateThread(m_hThread, 1); + } + + CloseHandle(m_hThread); + } +} + +void GSRasterizerMT::Draw(const GSRasterizerData* data) +{ + if(m_id == 0) + { + __super::Draw(data); + } + else + { + m_data = data; + + InterlockedBitTestAndSet(m_sync, m_id); + } +} + +DWORD WINAPI GSRasterizerMT::StaticThreadProc(LPVOID lpParam) +{ + return ((GSRasterizerMT*)lpParam)->ThreadProc(); +} + +DWORD GSRasterizerMT::ThreadProc() +{ + // _mm_setcsr(MXCSR); + + while(!m_exit) + { + if(*m_sync & (1 << m_id)) + { + __super::Draw(m_data); + + InterlockedBitTestAndReset(m_sync, m_id); + } + else + { + _mm_pause(); + } + } + + return 0; +} + +// + +GSRasterizerList::GSRasterizerList() +{ + // get a whole cache line (twice the size for future cpus ;) + + m_sync = (long*)_aligned_malloc(sizeof(*m_sync), 128); + + *m_sync = 0; +} + +GSRasterizerList::~GSRasterizerList() +{ + _aligned_free(m_sync); + + FreeRasterizers(); +} + +void GSRasterizerList::FreeRasterizers() +{ + while(!IsEmpty()) + { + delete RemoveHead(); + } +} + +void GSRasterizerList::Draw(const GSRasterizerData* data) +{ + *m_sync = 0; + + m_stats.Reset(); + + __int64 start = __rdtsc(); + + POSITION pos = GetTailPosition(); + + while(pos) + { + GetPrev(pos)->Draw(data); + } + + while(*m_sync) + { + _mm_pause(); + } + + m_stats.ticks = __rdtsc() - start; + + pos = GetHeadPosition(); + + while(pos) + { + GSRasterizerStats s; + + GetNext(pos)->GetStats(s); + + m_stats.pixels += s.pixels; + m_stats.prims = max(m_stats.prims, s.prims); + } +} + +void GSRasterizerList::GetStats(GSRasterizerStats& stats) +{ + stats = m_stats; +} + +void GSRasterizerList::PrintStats() +{ + if(!IsEmpty()) + { + GetHead()->PrintStats(); + } +} diff --git a/plugins/GSdx/GSRasterizer.h b/plugins/GSdx/GSRasterizer.h new file mode 100644 index 0000000000..a34e33a45b --- /dev/null +++ b/plugins/GSdx/GSRasterizer.h @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSVertexSW.h" +#include "GSFunctionMap.h" + +// +#define FAST_DRAWSCANLINE + +__declspec(align(16)) class GSRasterizerData +{ +public: + GSVector4i scissor; + GS_PRIM_CLASS primclass; + const GSVertexSW* vertices; + int count; + const void* param; +}; + +class IRasterizer +{ +public: + virtual ~IRasterizer() {} + + virtual void Draw(const GSRasterizerData* data) = 0; + virtual void GetStats(GSRasterizerStats& stats) = 0; + virtual void PrintStats() = 0; +}; + +class IDrawScanline +{ +public: + typedef void (IDrawScanline::*DrawScanlinePtr)(int top, int left, int right, const GSVertexSW& v); + typedef void (IDrawScanline::*DrawSolidRectPtr)(const GSVector4i& r, const GSVertexSW& v); + typedef void (IDrawScanline::*SetupPrimPtr)(const GSVertexSW* vertices, const GSVertexSW& dscan); + + struct Functions + { + DrawScanlinePtr sl; + DrawSolidRectPtr sr; + SetupPrimPtr sp; + }; + + virtual ~IDrawScanline() {} + + virtual void BeginDraw(const GSRasterizerData* data, Functions* dsf) = 0; + virtual void EndDraw(const GSRasterizerStats& stats) = 0; + virtual void PrintStats() = 0; +}; + +class GSRasterizer : public IRasterizer +{ +protected: + IDrawScanline* m_ds; + IDrawScanline::Functions m_dsf; + int m_id; + int m_threads; + GSRasterizerStats m_stats; + + void DrawPoint(const GSVertexSW* v, const GSVector4i& scissor); + void DrawLine(const GSVertexSW* v, const GSVector4i& scissor); + void DrawTriangle(const GSVertexSW* v, const GSVector4i& scissor); + void DrawSprite(const GSVertexSW* v, const GSVector4i& scissor); + + void DrawTriangleTop(GSVertexSW* v, const GSVector4i& scissor); + void DrawTriangleBottom(GSVertexSW* v, const GSVector4i& scissor); + void DrawTriangleTopBottom(GSVertexSW* v, const GSVector4i& scissor); + + __forceinline void DrawTriangleSection(int top, int bottom, GSVertexSW& l, const GSVertexSW& dl, GSVector4& r, const GSVector4& dr, const GSVertexSW& dscan, const GSVector4i& scissor); + __forceinline void DrawTriangleSection(int top, int bottom, GSVertexSW& l, const GSVertexSW& dl, const GSVertexSW& dscan, const GSVector4i& scissor); + +public: + GSRasterizer(IDrawScanline* ds, int id = 0, int threads = 0); + virtual ~GSRasterizer(); + + // IRasterizer + + void Draw(const GSRasterizerData* data); + void GetStats(GSRasterizerStats& stats); + void PrintStats() {m_ds->PrintStats();} +}; + +class GSRasterizerMT : public GSRasterizer +{ + long* m_sync; + bool m_exit; + DWORD m_ThreadId; + HANDLE m_hThread; + const GSRasterizerData* m_data; + + static DWORD WINAPI StaticThreadProc(LPVOID lpParam); + + DWORD ThreadProc(); + +public: + GSRasterizerMT(IDrawScanline* ds, int id, int threads, long* sync); + virtual ~GSRasterizerMT(); + + // IRasterizer + + void Draw(const GSRasterizerData* data); +}; + +class GSRasterizerList : protected CAtlList, public IRasterizer +{ + long* m_sync; + GSRasterizerStats m_stats; + + void FreeRasterizers(); + +public: + GSRasterizerList(); + virtual ~GSRasterizerList(); + + template void Create(T* parent, int threads) + { + FreeRasterizers(); + + threads = max(threads, 1); // TODO: min(threads, number of cpu cores) + + for(int i = 0; i < threads; i++) + { + AddTail(new GSRasterizerMT(new DS(parent, i), i, threads, m_sync)); + } + } + + // IRasterizer + + void Draw(const GSRasterizerData* data); + void GetStats(GSRasterizerStats& stats); + void PrintStats(); +}; diff --git a/plugins/GSdx/GSRenderer.cpp b/plugins/GSdx/GSRenderer.cpp new file mode 100644 index 0000000000..c0df5dde46 --- /dev/null +++ b/plugins/GSdx/GSRenderer.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSRenderer.h" diff --git a/plugins/GSdx/GSRenderer.h b/plugins/GSdx/GSRenderer.h new file mode 100644 index 0000000000..745106a5e2 --- /dev/null +++ b/plugins/GSdx/GSRenderer.h @@ -0,0 +1,594 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSWnd.h" +#include "GSState.h" +#include "GSVertexList.h" +#include "GSSettingsDlg.h" +#include "GSCapture.h" + +struct GSRendererSettings +{ + int m_interlace; + int m_aspectratio; + int m_filter; + bool m_vsync; + bool m_nativeres; +}; + +class GSRendererBase : public GSState, protected GSRendererSettings +{ +protected: + bool m_osd; + int m_field; + + void ProcessWindowMessages() + { + MSG msg; + + memset(&msg, 0, sizeof(msg)); + + while(msg.message != WM_QUIT && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + if(OnMessage(msg)) + { + continue; + } + + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + + virtual bool OnMessage(const MSG& msg) + { + if(msg.message == WM_KEYDOWN) + { + int step = (::GetAsyncKeyState(VK_SHIFT) & 0x8000) ? -1 : 1; + + if(msg.wParam == VK_F5) + { + m_interlace = (m_interlace + 7 + step) % 7; + return true; + } + + if(msg.wParam == VK_F6) + { + m_aspectratio = (m_aspectratio + 3 + step) % 3; + return true; + } + + if(msg.wParam == VK_F7) + { + m_wnd.SetWindowText(_T("PCSX2")); + m_osd = !m_osd; + return true; + } + } + + return false; + } + +public: + GSWnd m_wnd; + +public: + GSRendererBase(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs) + : GSState(base, mt, irq, nloophack) + , m_osd(true) + , m_field(0) + { + m_interlace = rs.m_interlace; + m_aspectratio = rs.m_aspectratio; + m_filter = rs.m_filter; + m_vsync = rs.m_vsync; + m_nativeres = rs.m_nativeres; + }; + + virtual bool Create(LPCTSTR title) = 0; + virtual void VSync(int field) = 0; + virtual bool MakeSnapshot(LPCTSTR path) = 0; +}; + +template class GSRenderer : public GSRendererBase +{ +protected: + typedef typename Device::Texture Texture; + + virtual void ResetDevice() {} + virtual bool GetOutput(int i, Texture& t) = 0; + + bool Merge() + { + int baseline = INT_MAX; + + for(int i = 0; i < 2; i++) + { + if(IsEnabled(i)) + { + baseline = min(GetDisplayPos(i).y, baseline); + } + } + + CSize fs(0, 0); + CSize ds(0, 0); + + Texture st[2]; + GSVector4 sr[2]; + GSVector4 dr[2]; + + for(int i = 0; i < 2; i++) + { + if(IsEnabled(i) && GetOutput(i, st[i])) + { + CRect r = GetFrameRect(i); + + // overscan hack + + if(GetDisplaySize(i).cy > 512) // hmm + { + int y = GetDeviceSize(i).cy; + if(SMODE2->INT && SMODE2->FFMD) y /= 2; + r.bottom = r.top + y; + } + + // + + sr[i].x = st[i].m_scale.x * r.left / st[i].GetWidth(); + sr[i].y = st[i].m_scale.y * r.top / st[i].GetHeight(); + sr[i].z = st[i].m_scale.x * r.right / st[i].GetWidth(); + sr[i].w = st[i].m_scale.y * r.bottom / st[i].GetHeight(); + + GSVector2 o; + + o.x = 0; + o.y = 0; + + CPoint p = GetDisplayPos(i); + + if(p.y - baseline >= 4) // 2? + { + o.y = st[i].m_scale.y * (p.y - baseline); + } + + if(SMODE2->INT && SMODE2->FFMD) o.y /= 2; + + dr[i].x = o.x; + dr[i].y = o.y; + dr[i].z = o.x + st[i].m_scale.x * r.Width(); + dr[i].w = o.y + st[i].m_scale.y * r.Height(); + +#ifdef _M_AMD64 +// schrödinger's bug, fs will be trashed unless we access these values +CString str; +str.Format(_T("%d %f %f %f %f "), i, o.x, o.y, dr[i].z, dr[i].w); +//::MessageBox(NULL, str, _T(""), MB_OK); +#endif + fs.cx = max(fs.cx, (int)(dr[i].z + 0.5f)); + fs.cy = max(fs.cy, (int)(dr[i].w + 0.5f)); + } + } + + ds.cx = fs.cx; + ds.cy = fs.cy; + + if(SMODE2->INT && SMODE2->FFMD) ds.cy *= 2; + + bool slbg = PMODE->SLBG; + bool mmod = PMODE->MMOD; + + if(st[0] || st[1]) + { + GSVector4 c; + + c.r = (float)BGCOLOR->R / 255; + c.g = (float)BGCOLOR->G / 255; + c.b = (float)BGCOLOR->B / 255; + c.a = (float)PMODE->ALP / 255; + + m_dev.Merge(st, sr, dr, fs, slbg, mmod, c); + + if(SMODE2->INT && m_interlace > 0) + { + int field = 1 - ((m_interlace - 1) & 1); + int mode = (m_interlace - 1) >> 1; + + if(!m_dev.Interlace(ds, m_field ^ field, mode, st[1].m_scale.y)) // st[1].m_scale.y + { + return false; + } + } + } + + return true; + } + + void DoCapture() + { + if(!m_capture.IsCapturing()) + { + return; + } + + CSize size = m_capture.GetSize(); + + Texture current; + + m_dev.GetCurrent(current); + + Texture offscreen; + + if(m_dev.CopyOffscreen(current, GSVector4(0, 0, 1, 1), offscreen, size.cx, size.cy)) + { + BYTE* bits = NULL; + int pitch = 0; + + if(offscreen.Map(&bits, pitch)) + { + m_capture.DeliverFrame(bits, pitch, m_dev.IsCurrentRGBA()); + + offscreen.Unmap(); + } + + m_dev.Recycle(offscreen); + } + } + + virtual bool OnMessage(const MSG& msg) + { + if(msg.message == WM_KEYDOWN) + { + if(msg.wParam == VK_F12) + { + if(m_capture.IsCapturing()) + { + m_capture.EndCapture(); + } + else + { + m_capture.BeginCapture(GetFPS()); + } + + return true; + } + } + + return __super::OnMessage(msg); + } + +public: + Device m_dev; + bool m_psrr; + + int s_n; + bool s_dump; + bool s_save; + bool s_savez; + + GSCapture m_capture; + +public: + GSRenderer(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs, bool psrr) + : GSRendererBase(base, mt, irq, nloophack, rs) + , m_psrr(psrr) + { + s_n = 0; + s_dump = !!AfxGetApp()->GetProfileInt(_T("Debug"), _T("dump"), 0); + s_save = !!AfxGetApp()->GetProfileInt(_T("Debug"), _T("save"), 0); + s_savez = !!AfxGetApp()->GetProfileInt(_T("Debug"), _T("savez"), 0); + } + + bool Create(LPCTSTR title) + { + if(!m_wnd.Create(title)) + { + return false; + } + + if(!m_dev.Create(m_wnd, m_vsync)) + { + return false; + } + + Reset(); + + return true; + } + + void VSync(int field) + { + // printf("VSYNC\n"); + + GSPerfMonAutoTimer pmat(m_perfmon); + + m_field = !!field; + + Flush(); + + m_perfmon.Put(GSPerfMon::Frame); + + ProcessWindowMessages(); + + if(m_dump) + { + m_dump.VSync(m_field, !(::GetAsyncKeyState(VK_CONTROL) & 0x8000), PMODE); + } + + if(!Merge()) return; + + // osd + + static UINT64 s_frame = 0; + static CString s_stats; + + if(m_perfmon.GetFrame() - s_frame >= 30) + { + m_perfmon.Update(); + + s_frame = m_perfmon.GetFrame(); + + double fps = 1000.0f / m_perfmon.Get(GSPerfMon::Frame); + + s_stats.Format( + _T("%I64d | %d x %d | %.2f fps (%d%%) | %s - %s | %s | %d/%d/%d | %d%% CPU | %.2f | %.2f"), + m_perfmon.GetFrame(), GetDisplaySize().cx, GetDisplaySize().cy, fps, (int)(100.0 * fps / GetFPS()), + SMODE2->INT ? (CString(_T("Interlaced ")) + (SMODE2->FFMD ? _T("(frame)") : _T("(field)"))) : _T("Progressive"), + GSSettingsDlg::g_interlace[m_interlace].name, + GSSettingsDlg::g_aspectratio[m_aspectratio].name, + (int)m_perfmon.Get(GSPerfMon::Quad), + (int)m_perfmon.Get(GSPerfMon::Prim), + (int)m_perfmon.Get(GSPerfMon::Draw), + m_perfmon.CPU(), + m_perfmon.Get(GSPerfMon::Swizzle) / 1024, + m_perfmon.Get(GSPerfMon::Unswizzle) / 1024 + ); + + double fillrate = m_perfmon.Get(GSPerfMon::Fillrate); + + if(fillrate > 0) + { + s_stats.Format(_T("%s | %.2f mpps"), CString(s_stats), fps * fillrate / (1024 * 1024)); + } + + if(m_capture.IsCapturing()) + { + s_stats += _T(" | Recording..."); + } + + if(m_perfmon.Get(GSPerfMon::COLCLAMP)) _tprintf(_T("*** NOT SUPPORTED: color wrap ***\n")); + if(m_perfmon.Get(GSPerfMon::PABE)) _tprintf(_T("*** NOT SUPPORTED: per pixel alpha blend ***\n")); + if(m_perfmon.Get(GSPerfMon::DATE)) _tprintf(_T("*** PERFORMANCE WARNING: destination alpha test used ***\n")); + if(m_perfmon.Get(GSPerfMon::ABE)) _tprintf(_T("*** NOT SUPPORTED: alpha blending mode ***\n")); + if(m_perfmon.Get(GSPerfMon::DepthTexture)) _tprintf(_T("*** NOT SUPPORTED: depth texture ***\n")); + + m_wnd.SetWindowText(s_stats); + } + + if(m_osd) + { + m_dev.Draw(s_stats + _T("\n\nF5: interlace mode\nF6: aspect ratio\nF7: OSD")); + } + + if(m_frameskip) + { + return; + } + + // + + if(m_dev.IsLost()) + { + ResetDevice(); + } + + // + + CRect r; + + m_wnd.GetClientRect(&r); + + GSUtil::FitRect(r, m_aspectratio); + + m_dev.Present(r); + + DoCapture(); + } + + bool MakeSnapshot(LPCTSTR path) + { + CString fn; + + fn.Format(_T("%s_%s"), path, CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S"))); + + if((::GetAsyncKeyState(VK_SHIFT) & 0x8000) && !m_dump) + { + GSFreezeData fd; + fd.size = 0; + fd.data = NULL; + Freeze(&fd, true); + fd.data = new BYTE[fd.size]; + Freeze(&fd, false); + + m_dump.Open(fn + _T(".gs"), m_crc, fd, PMODE); + + delete [] fd.data; + } + + return m_dev.SaveCurrent(fn + _T(".bmp")); + } + + virtual void MinMaxUV(int w, int h, CRect& r) {r = CRect(0, 0, w, h);} + virtual bool CanUpscale() {return !m_nativeres;} +}; + +template class GSRendererT : public GSRenderer +{ +protected: + Vertex* m_vertices; + int m_count; + int m_maxcount; + GSVertexList m_vl; + + void Reset() + { + m_count = 0; + m_vl.RemoveAll(); + + __super::Reset(); + } + + void ResetPrim() + { + m_vl.RemoveAll(); + } + + void FlushPrim() + { + if(m_count > 0) + { + /* + TRACE(_T("[%d] Draw f %05x (%d) z %05x (%d %d %d %d) t %05x %05x (%d)\n"), + (int)m_perfmon.GetFrame(), + (int)m_context->FRAME.Block(), + (int)m_context->FRAME.PSM, + (int)m_context->ZBUF.Block(), + (int)m_context->ZBUF.PSM, + m_context->TEST.ZTE, + m_context->TEST.ZTST, + m_context->ZBUF.ZMSK, + PRIM->TME ? (int)m_context->TEX0.TBP0 : 0xfffff, + PRIM->TME && m_context->TEX0.PSM > PSM_PSMCT16S ? (int)m_context->TEX0.CBP : 0xfffff, + PRIM->TME ? (int)m_context->TEX0.PSM : 0xff); + */ + + if(GSUtil::EncodePSM(m_context->FRAME.PSM) != 3 && GSUtil::EncodePSM(m_context->ZBUF.PSM) != 3) + { + // FIXME: berserk fpsm = 27 (8H) + + Draw(); + } + + m_count = 0; + } + } + + void GrowVertexBuffer() + { + m_maxcount = max(10000, m_maxcount * 3/2); + m_vertices = (Vertex*)_aligned_realloc(m_vertices, sizeof(Vertex) * m_maxcount, 16); + m_maxcount -= 100; + } + + template __forceinline Vertex* DrawingKick(bool skip, DWORD& count) + { + switch(prim) + { + case GS_POINTLIST: count = 1; break; + case GS_LINELIST: count = 2; break; + case GS_LINESTRIP: count = 2; break; + case GS_TRIANGLELIST: count = 3; break; + case GS_TRIANGLESTRIP: count = 3; break; + case GS_TRIANGLEFAN: count = 3; break; + case GS_SPRITE: count = 2; break; + case GS_INVALID: count = 1; break; + default: __assume(0); + } + + if(m_vl.GetCount() < count) + { + return NULL; + } + + if(m_count >= m_maxcount) + { + GrowVertexBuffer(); + } + + Vertex* v = &m_vertices[m_count]; + + switch(prim) + { + case GS_POINTLIST: + m_vl.GetAt(0, v[0]); + m_vl.RemoveAll(); + break; + case GS_LINELIST: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.RemoveAll(); + break; + case GS_LINESTRIP: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.RemoveAt(0, 1); + break; + case GS_TRIANGLELIST: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.GetAt(2, v[2]); + m_vl.RemoveAll(); + break; + case GS_TRIANGLESTRIP: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.GetAt(2, v[2]); + m_vl.RemoveAt(0, 2); + break; + case GS_TRIANGLEFAN: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.GetAt(2, v[2]); + m_vl.RemoveAt(1, 1); + break; + case GS_SPRITE: + m_vl.GetAt(0, v[0]); + m_vl.GetAt(1, v[1]); + m_vl.RemoveAll(); + break; + case GS_INVALID: + ASSERT(0); + m_vl.RemoveAll(); + return NULL; + default: + __assume(0); + } + + return !skip ? v : NULL; + } + + virtual void Draw() = 0; + +public: + GSRendererT(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs, bool psrr = true) + : GSRenderer(base, mt, irq, nloophack, rs, psrr) + , m_count(0) + , m_maxcount(0) + , m_vertices(NULL) + { + } + + ~GSRendererT() + { + if(m_vertices) _aligned_free(m_vertices); + } +}; diff --git a/plugins/GSdx/GSRendererHW.cpp b/plugins/GSdx/GSRendererHW.cpp new file mode 100644 index 0000000000..a5fa193796 --- /dev/null +++ b/plugins/GSdx/GSRendererHW.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSRendererHW.h" \ No newline at end of file diff --git a/plugins/GSdx/GSRendererHW.h b/plugins/GSdx/GSRendererHW.h new file mode 100644 index 0000000000..5296f2ea06 --- /dev/null +++ b/plugins/GSdx/GSRendererHW.h @@ -0,0 +1,633 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRenderer.h" +#include "GSTextureCache.h" +#include "GSCrc.h" + +template +class GSRendererHW : public GSRendererT +{ + TextureCache* m_tc; + int m_width; + int m_height; + int m_skip; + bool m_reset; + +protected: + void Reset() + { + // TODO: GSreset can come from the main thread too => crash + // m_tc->RemoveAll(); + + m_reset = true; + + __super::Reset(); + } + + void MinMaxUV(int w, int h, CRect& r) + { + int wms = m_context->CLAMP.WMS; + int wmt = m_context->CLAMP.WMT; + + int minu = (int)m_context->CLAMP.MINU; + int minv = (int)m_context->CLAMP.MINV; + int maxu = (int)m_context->CLAMP.MAXU; + int maxv = (int)m_context->CLAMP.MAXV; + + GSVector4i vr = GSVector4i(0, 0, w, h); + + GSVector4i wm[3]; + + if(wms + wmt < 6) + { + GSVector4 mm; + + if(m_count < 100) + { + Vertex* v = m_vertices; + + GSVector4 minv(+1e10f); + GSVector4 maxv(-1e10f); + + int i = 0; + + if(PRIM->FST) + { + for(int j = m_count - 3; i < j; i += 4) + { + GSVector4 v0 = GSVector4(v[i + 0].m128[0]); + GSVector4 v1 = GSVector4(v[i + 1].m128[0]); + GSVector4 v2 = GSVector4(v[i + 2].m128[0]); + GSVector4 v3 = GSVector4(v[i + 3].m128[0]); + + minv = minv.minv((v0.minv(v1)).minv(v2.minv(v3))); + maxv = maxv.maxv((v0.maxv(v1)).maxv(v2.maxv(v3))); + } + + for(int j = m_count; i < j; i++) + { + GSVector4 v0 = GSVector4(v[i + 0].m128[0]); + + minv = minv.minv(v0); + maxv = maxv.maxv(v0); + } + + mm = minv.xyxy(maxv) * GSVector4(16 << m_context->TEX0.TW, 16 << m_context->TEX0.TH, 16 << m_context->TEX0.TW, 16 << m_context->TEX0.TH).rcpnr(); + } + else + { + /* + for(int j = m_count - 3; i < j; i += 4) + { + GSVector4 v0 = GSVector4(v[i + 0].m128[0]) / GSVector4(v[i + 0].GetQ()); + GSVector4 v1 = GSVector4(v[i + 1].m128[0]) / GSVector4(v[i + 1].GetQ()); + GSVector4 v2 = GSVector4(v[i + 2].m128[0]) / GSVector4(v[i + 2].GetQ()); + GSVector4 v3 = GSVector4(v[i + 3].m128[0]) / GSVector4(v[i + 3].GetQ()); + + minv = minv.minv((v0.minv(v1)).minv(v2.minv(v3))); + maxv = maxv.maxv((v0.maxv(v1)).maxv(v2.maxv(v3))); + } + + for(int j = m_count; i < j; i++) + { + GSVector4 v0 = GSVector4(v[i + 0].m128[0]) / GSVector4(v[i + 0].GetQ());; + + minv = minv.minv(v0); + maxv = maxv.maxv(v0); + } + + mm = minv.xyxy(maxv); + */ + + // just can't beat the compiler generated scalar sse code with packed div or rcp + + mm.x = mm.y = +1e10; + mm.z = mm.w = -1e10; + + for(int j = m_count; i < j; i++) + { + float w = 1.0f / v[i].GetQ(); + + float x = v[i].t.x * w; + + if(x < mm.x) mm.x = x; + if(x > mm.z) mm.z = x; + + float y = v[i].t.y * w; + + if(y < mm.y) mm.y = y; + if(y > mm.w) mm.w = y; + } + } + } + else + { + mm = GSVector4(0.0f, 0.0f, 1.0f, 1.0f); + } + + GSVector4 v0 = GSVector4(vr); + GSVector4 v1 = v0.zwzw(); + + GSVector4 mmf = mm.floor(); + GSVector4 mask = mmf.xyxy() == mmf.zwzw(); + + wm[0] = GSVector4i(v0.blend8((mm - mmf) * v1, mask)); + + mm *= v1; + + wm[1] = GSVector4i(mm.sat(GSVector4::zero(), v1)); + wm[2] = GSVector4i(mm.sat(GSVector4(minu, minv, maxu, maxv))); + } + + GSVector4i v; + + switch(wms) + { + case CLAMP_REPEAT: + v = wm[0]; + if(v.x == 0 && v.z != w) v.z = w; // FIXME + vr.x = v.x; + vr.z = v.z; + break; + case CLAMP_CLAMP: + case CLAMP_REGION_CLAMP: + v = wm[wms]; + if(v.x > v.z) v.x = v.z; + vr.x = v.x; + vr.z = v.z; + break; + case CLAMP_REGION_REPEAT: + if(m_psrr) {vr.x = maxu; vr.z = vr.x + (minu + 1);} + //else {vr.x = 0; vr.z = w;} + break; + default: + __assume(0); + } + + switch(wmt) + { + case CLAMP_REPEAT: + v = wm[0]; + if(v.y == 0 && v.w != h) v.w = h; // FIXME + vr.y = v.y; + vr.w = v.w; + break; + case CLAMP_CLAMP: + case CLAMP_REGION_CLAMP: + v = wm[wmt]; + if(v.y > v.w) v.y = v.w; + vr.y = v.y; + vr.w = v.w; + break; + case CLAMP_REGION_REPEAT: + if(m_psrr) {vr.y = maxv; vr.w = vr.y + (minv + 1);} + //else {r.y = 0; r.w = w;} + break; + default: + __assume(0); + } + + r = vr; + + r.InflateRect(1, 1); // one more pixel because of bilinear filtering + + CSize bs = GSLocalMemory::m_psm[m_context->TEX0.PSM].bs; + CSize bsm(bs.cx - 1, bs.cy - 1); + + r.left = max(r.left & ~bsm.cx, 0); + r.right = min((r.right + bsm.cx) & ~bsm.cx, w); + + r.top = max(r.top & ~bsm.cy, 0); + r.bottom = min((r.bottom + bsm.cy) & ~bsm.cy, h); + } + + void VSync(int field) + { + __super::VSync(field); + + m_tc->IncAge(); + + m_skip = 0; + + if(m_reset) + { + m_tc->RemoveAll(); + + m_reset = false; + } + } + + void ResetDevice() + { + m_tc->RemoveAll(); + } + + bool GetOutput(int i, Texture& t) + { + GIFRegTEX0 TEX0; + + TEX0.TBP0 = DISPFB[i]->Block(); + TEX0.TBW = DISPFB[i]->FBW; + TEX0.PSM = DISPFB[i]->PSM; + + TRACE(_T("[%d] GetOutput %d %05x (%d)\n"), (int)m_perfmon.GetFrame(), i, (int)TEX0.TBP0, (int)TEX0.PSM); + + if(GSTextureCache::GSRenderTarget* rt = m_tc->GetRenderTarget(TEX0, m_width, m_height, true)) + { + t = rt->m_texture; + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp2\\_%05d_f%I64d_fr%d_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), i, (int)TEX0.TBP0, (int)TEX0.PSM); + if(s_save) rt->m_texture.Save(str); + } + + return true; + } + + return false; + } + + void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, CRect r) + { + TRACE(_T("[%d] InvalidateVideoMem %d,%d - %d,%d %05x (%d)\n"), (int)m_perfmon.GetFrame(), r.left, r.top, r.right, r.bottom, (int)BITBLTBUF.DBP, (int)BITBLTBUF.DPSM); + + m_tc->InvalidateVideoMem(BITBLTBUF, r); + } + + void InvalidateLocalMem(const GIFRegBITBLTBUF& BITBLTBUF, CRect r) + { + TRACE(_T("[%d] InvalidateLocalMem %d,%d - %d,%d %05x (%d)\n"), (int)m_perfmon.GetFrame(), r.left, r.top, r.right, r.bottom, (int)BITBLTBUF.SBP, (int)BITBLTBUF.SPSM); + + m_tc->InvalidateLocalMem(BITBLTBUF, r); + } + + void Draw() + { + if(IsBadFrame(m_skip)) + { + return; + } + + GSDrawingEnvironment& env = m_env; + GSDrawingContext* context = m_context; + + GIFRegTEX0 TEX0; + + TEX0.TBP0 = context->FRAME.Block(); + TEX0.TBW = context->FRAME.FBW; + TEX0.PSM = context->FRAME.PSM; + + GSTextureCache::GSRenderTarget* rt = m_tc->GetRenderTarget(TEX0, m_width, m_height); + + TEX0.TBP0 = context->ZBUF.Block(); + TEX0.TBW = context->FRAME.FBW; + TEX0.PSM = context->ZBUF.PSM; + + GSTextureCache::GSDepthStencil* ds = m_tc->GetDepthStencil(TEX0, m_width, m_height); + + GSTextureCache::GSTexture* tex = NULL; + + if(PRIM->TME) + { + tex = m_tc->GetTexture(); + + if(!tex) return; + } + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp2\\_%05d_f%I64d_tex_%05x_%d_%d%d_%02x_%02x_%02x_%02x.dds"), + s_n++, m_perfmon.GetFrame(), (int)context->TEX0.TBP0, (int)context->TEX0.PSM, + (int)context->CLAMP.WMS, (int)context->CLAMP.WMT, + (int)context->CLAMP.MINU, (int)context->CLAMP.MAXU, + (int)context->CLAMP.MINV, (int)context->CLAMP.MAXV); + if(PRIM->TME) if(s_save) tex->m_texture.Save(str, true); + str.Format(_T("c:\\temp2\\_%05d_f%I64d_tpx_%05x_%d.dds"), s_n-1, m_perfmon.GetFrame(), context->TEX0.CBP, context->TEX0.CPSM); + if(PRIM->TME && tex->m_palette) if(s_save) tex->m_palette.Save(str, true); + str.Format(_T("c:\\temp2\\_%05d_f%I64d_rt0_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), context->FRAME.Block(), context->FRAME.PSM); + if(s_save) rt->m_texture.Save(str); + str.Format(_T("c:\\temp2\\_%05d_f%I64d_rz0_%05x_%d.bmp"), s_n-1, m_perfmon.GetFrame(), context->ZBUF.Block(), context->ZBUF.PSM); + if(s_savez) ds->m_texture.Save(str); + // if(s_savez) m_dev.SaveToFileD32S8X24(ds->m_texture, str); // TODO + // if(s_savez) m_dev.SaveToFileD24S8(ds->m_texture, str); // TODO + } + + int prim = PRIM->PRIM; + + if(!OverrideInput(prim, rt->m_texture, ds->m_texture, tex ? &tex->m_texture : NULL)) + { + return; + } + + Draw(prim, rt->m_texture, ds->m_texture, tex); + + OverrideOutput(); + + m_tc->InvalidateTextures(context->FRAME, context->ZBUF); + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp2\\_%05d_f%I64d_rt1_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), context->FRAME.Block(), context->FRAME.PSM); + if(s_save) rt->m_texture.Save(str); + str.Format(_T("c:\\temp2\\_%05d_f%I64d_rz1_%05x_%d.bmp"), s_n-1, m_perfmon.GetFrame(), context->ZBUF.Block(), context->ZBUF.PSM); + if(s_savez) ds->m_texture.Save(str); + // if(s_savez) m_dev.SaveToFileD32S8X24(ds->m_texture, str); // TODO + } + } + + virtual void Draw(int prim, Texture& rt, Texture& ds, typename GSTextureCache::GSTexture* tex) = 0; + + virtual bool OverrideInput(int& prim, Texture& rt, Texture& ds, Texture* t) + { + #pragma region ffxii pal video conversion + + if(m_game.title == CRC::FFXII && m_game.region == CRC::EU) + { + static DWORD* video = NULL; + static bool ok = false; + + if(prim == GS_POINTLIST && m_count >= 448*448 && m_count <= 448*512) + { + // incoming pixels are stored in columns, one column is 16x512, total res 448x512 or 448x454 + + if(!video) video = new DWORD[512*512]; + + for(int x = 0, i = 0, rows = m_count / 448; x < 448; x += 16) + { + DWORD* dst = &video[x]; + + for(int y = 0; y < rows; y++, dst += 512) + { + for(int j = 0; j < 16; j++, i++) + { + dst[j] = m_vertices[i].c0; + } + } + } + + ok = true; + + return false; + } + else if(prim == GS_LINELIST && m_count == 512*2 && ok) + { + // normally, this step would copy the video onto screen with 512 texture mapped horizontal lines, + // but we use the stored video data to create a new texture, and replace the lines with two triangles + + ok = false; + + m_dev.CreateTexture(*t, 512, 512); + + t->Update(CRect(0, 0, 448, 512), video, 512*4); + + m_vertices[0] = m_vertices[0]; + m_vertices[1] = m_vertices[1]; + m_vertices[2] = m_vertices[m_count - 2]; + m_vertices[3] = m_vertices[1]; + m_vertices[4] = m_vertices[2]; + m_vertices[5] = m_vertices[m_count - 1]; + + prim = GS_TRIANGLELIST; + m_count = 6; + + return true; + } + } + + #pragma endregion + + #pragma region ffx random battle transition (z buffer written directly, clear it now) + + if(m_game.title == CRC::FFX) + { + DWORD FBP = m_context->FRAME.Block(); + DWORD ZBP = m_context->ZBUF.Block(); + DWORD TBP = m_context->TEX0.TBP0; + + if((FBP == 0x00d00 || FBP == 0x00000) && ZBP == 0x02100 && PRIM->TME && TBP == 0x01a00 && m_context->TEX0.PSM == PSM_PSMCT16S) + { + m_dev.ClearDepth(ds, 0); + } + + return true; + } + + #pragma endregion + + #pragma region metal slug missing red channel fix + + if(m_game.title == CRC::MetalSlug6) + { + for(int i = 0, j = m_count; i < j; i++) + { + if(m_vertices[i].r == 0 && m_vertices[i].g != 0 && m_vertices[i].b != 0) + { + m_vertices[i].r = (m_vertices[i].g + m_vertices[i].b) / 2; + } + } + + return true; + } + + #pragma endregion + + #pragma region tomoyo after, clannad (palette uploaded in a point list, pure genius...) + + if(m_game.title == CRC::TomoyoAfter || m_game.title == CRC::Clannad) + { + if(prim == GS_POINTLIST && !PRIM->TME) + { + DWORD bp = m_context->FRAME.Block(); + DWORD bw = m_context->FRAME.FBW; + + if(bp >= 0x03f40 && (bp & 0x1f) == 0) + { + if(m_count == 16) + { + for(int i = 0; i < 16; i++) + { + m_vertices[i].a = m_vertices[i].a >= 0x80 ? 0xff : m_vertices[i].a * 2; + + m_mem.WritePixel32(i & 7, i >> 3, m_vertices[i].c0, bp, bw); + } + + m_mem.m_clut.Invalidate(); + + return false; + } + else if(m_count == 256) + { + for(int i = 0; i < 256; i++) + { + m_vertices[i].a = m_vertices[i].a >= 0x80 ? 0xff : m_vertices[i].a * 2; + + m_mem.WritePixel32(i & 15, i >> 4, m_vertices[i].c0, bp, bw); + } + + m_mem.m_clut.Invalidate(); + + return false; + } + else + { + ASSERT(0); + } + } + } + + return true; + } + + #pragma endregion + + return true; + } + + virtual void OverrideOutput() + { + #pragma region dbzbt2 palette readback (cannot detect yet, when fetching the texture later) + + if(m_game.title == CRC::DBZBT2) + { + DWORD FBP = m_context->FRAME.Block(); + DWORD TBP0 = m_context->TEX0.TBP0; + + if(PRIM->TME && (FBP == 0x03c00 && TBP0 == 0x03c80 || FBP == 0x03ac0 && TBP0 == 0x03b40)) + { + GIFRegBITBLTBUF BITBLTBUF; + + BITBLTBUF.SBP = FBP; + BITBLTBUF.SBW = 1; + BITBLTBUF.SPSM = PSM_PSMCT32; + + InvalidateLocalMem(BITBLTBUF, CRect(0, 0, 64, 64)); + } + } + + #pragma endregion + + #pragma region MajokkoALaMode2 palette readback + + if(m_game.title == CRC::MajokkoALaMode2) + { + DWORD FBP = m_context->FRAME.Block(); + + if(!PRIM->TME && FBP == 0x03f40) + { + GIFRegBITBLTBUF BITBLTBUF; + + BITBLTBUF.SBP = FBP; + BITBLTBUF.SBW = 1; + BITBLTBUF.SPSM = PSM_PSMCT32; + + InvalidateLocalMem(BITBLTBUF, CRect(0, 0, 16, 16)); + } + } + + #pragma endregion + } + + bool CanUpscale() + { + #pragma region dbzbt2 palette should stay 64 x 64 + + if(m_game.title == CRC::DBZBT2) + { + DWORD FBP = m_context->FRAME.Block(); + + if(FBP == 0x03c00 || FBP == 0x03ac0) + { + return false; + } + } + + #pragma endregion + + #pragma region MajokkoALaMode2 palette should stay 16 x 16 + + if(m_game.title == CRC::MajokkoALaMode2) + { + DWORD FBP = m_context->FRAME.Block(); + + if(FBP == 0x03f40) + { + return false; + } + } + + #pragma endregion + + #pragma region TalesOfAbyss full image blur and brightening + + if(m_game.title == CRC::TalesOfAbyss) + { + DWORD FBP = m_context->FRAME.Block(); + + if(FBP == 0x036e0 || FBP == 0x03560 || FBP == 0x038e0) + { + return false; + } + } + + #pragma endregion + + return __super::CanUpscale(); + } + +public: + GSRendererHW(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs, bool psrr) + : GSRendererT(base, mt, irq, nloophack, rs, psrr) + , m_width(1024) + , m_height(1024) + , m_skip(0) + , m_reset(false) + { + if(!m_nativeres) + { + m_width = AfxGetApp()->GetProfileInt(_T("Settings"), _T("resx"), m_width); + m_height = AfxGetApp()->GetProfileInt(_T("Settings"), _T("resy"), m_height); + } + + m_tc = new TextureCache(this); + } + + virtual ~GSRendererHW() + { + delete m_tc; + } + + void SetGameCRC(DWORD crc, int options) + { + __super::SetGameCRC(crc, options); + + if(m_game.title == CRC::JackieChanAdv) + { + m_width = 1280; // TODO: uses a 1280px wide 16 bit render target, but this only fixes half of the problem + } + } +}; diff --git a/plugins/GSdx/GSRendererHW10.cpp b/plugins/GSdx/GSRendererHW10.cpp new file mode 100644 index 0000000000..305c5f8c23 --- /dev/null +++ b/plugins/GSdx/GSRendererHW10.cpp @@ -0,0 +1,586 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSRendererHW10.h" +#include "GSCrc.h" +#include "resource.h" + +GSRendererHW10::GSRendererHW10(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs) + : GSRendererHW(base, mt, irq, nloophack, rs, true) +{ + InitVertexKick(); +} + +bool GSRendererHW10::Create(LPCTSTR title) +{ + if(!__super::Create(title)) + return false; + + if(!m_tfx.Create(&m_dev)) + return false; + + // + + D3D10_DEPTH_STENCIL_DESC dsd; + + memset(&dsd, 0, sizeof(dsd)); + + dsd.DepthEnable = false; + dsd.StencilEnable = true; + dsd.StencilReadMask = 1; + dsd.StencilWriteMask = 1; + dsd.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS; + dsd.FrontFace.StencilPassOp = D3D10_STENCIL_OP_REPLACE; + dsd.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP; + dsd.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_KEEP; + dsd.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS; + dsd.BackFace.StencilPassOp = D3D10_STENCIL_OP_REPLACE; + dsd.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP; + dsd.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_KEEP; + + m_dev->CreateDepthStencilState(&dsd, &m_date.dss); + + D3D10_BLEND_DESC bd; + + memset(&bd, 0, sizeof(bd)); + + m_dev->CreateBlendState(&bd, &m_date.bs); + + // + + return true; +} + +template +void GSRendererHW10::VertexKick(bool skip) +{ + Vertex& dst = m_vl.AddTail(); + + dst.m128i[0] = m_v.m128i[0]; + dst.m128i[1] = m_v.m128i[1]; + + if(tme && fst) + { + GSVector4::storel(&dst.ST, m_v.GetUV()); + } + + DWORD count = 0; + + if(Vertex* v = DrawingKick(skip, count)) + { + GSVector4i scissor = m_context->scissor.dx10; + + #if _M_SSE >= 0x401 + + GSVector4i pmin, pmax, v0, v1, v2; + + switch(prim) + { + case GS_POINTLIST: + v0 = GSVector4i::load((int)v[0].p.xy).upl16(); + pmin = v0; + pmax = v0; + break; + case GS_LINELIST: + case GS_LINESTRIP: + case GS_SPRITE: + v0 = GSVector4i::load((int)v[0].p.xy); + v1 = GSVector4i::load((int)v[1].p.xy); + pmin = v0.min_u16(v1).upl16(); + pmax = v0.max_u16(v1).upl16(); + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + v0 = GSVector4i::load((int)v[0].p.xy); + v1 = GSVector4i::load((int)v[1].p.xy); + v2 = GSVector4i::load((int)v[2].p.xy); + pmin = v0.min_u16(v1).min_u16(v2).upl16(); + pmax = v0.max_u16(v1).max_u16(v2).upl16(); + break; + } + + GSVector4i test = (pmax < scissor) | (pmin > scissor.zwxy()); + + if(test.mask() & 0xff) + { + return; + } + + #else + + switch(prim) + { + case GS_POINTLIST: + if(v[0].p.x < scissor.x + || v[0].p.x > scissor.z + || v[0].p.y < scissor.y + || v[0].p.y > scissor.w) + { + return; + } + break; + case GS_LINELIST: + case GS_LINESTRIP: + case GS_SPRITE: + if(v[0].p.x < scissor.x && v[1].p.x < scissor.x + || v[0].p.x > scissor.z && v[1].p.x > scissor.z + || v[0].p.y < scissor.y && v[1].p.y < scissor.y + || v[0].p.y > scissor.w && v[1].p.y > scissor.w) + { + return; + } + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + if(v[0].p.x < scissor.x && v[1].p.x < scissor.x && v[2].p.x < scissor.x + || v[0].p.x > scissor.z && v[1].p.x > scissor.z && v[2].p.x > scissor.z + || v[0].p.y < scissor.y && v[1].p.y < scissor.y && v[2].p.y < scissor.y + || v[0].p.y > scissor.w && v[1].p.y > scissor.w && v[2].p.y > scissor.w) + { + return; + } + break; + } + + #endif + + m_count += count; + } +} + +void GSRendererHW10::Draw(int prim, Texture& rt, Texture& ds, GSTextureCache::GSTexture* tex) +{ + GSDrawingEnvironment& env = m_env; + GSDrawingContext* context = m_context; +/* + if(s_dump) + { + TRACE(_T("\n")); + + TRACE(_T("PRIM = %d, ZMSK = %d, ZTE = %d, ZTST = %d, ATE = %d, ATST = %d, AFAIL = %d, AREF = %02x\n"), + PRIM->PRIM, context->ZBUF.ZMSK, + context->TEST.ZTE, context->TEST.ZTST, + context->TEST.ATE, context->TEST.ATST, context->TEST.AFAIL, context->TEST.AREF); + + for(int i = 0; i < m_count; i++) + { + TRACE(_T("[%d] %3.0f %3.0f %3.0f %3.0f\n"), i, (float)m_vertices[i].p.x / 16, (float)m_vertices[i].p.y / 16, (float)m_vertices[i].p.z, (float)m_vertices[i].a); + } + } +*/ + D3D10_PRIMITIVE_TOPOLOGY topology; + int prims = 0; + + switch(prim) + { + case GS_POINTLIST: + topology = D3D10_PRIMITIVE_TOPOLOGY_POINTLIST; + prims = m_count; + break; + case GS_LINELIST: + case GS_LINESTRIP: + case GS_SPRITE: + topology = D3D10_PRIMITIVE_TOPOLOGY_LINELIST; + prims = m_count / 2; + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + topology = D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST; + prims = m_count / 3; + break; + default: + __assume(0); + } + + m_perfmon.Put(GSPerfMon::Prim, prims); + m_perfmon.Put(GSPerfMon::Draw, 1); + + // date + + SetupDATE(rt, ds); + + // + + m_dev.BeginScene(); + + // om + + GSTextureFX10::OMDepthStencilSelector om_dssel; + + om_dssel.zte = context->TEST.ZTE; + om_dssel.ztst = context->TEST.ZTST; + om_dssel.zwe = !context->ZBUF.ZMSK; + om_dssel.date = context->FRAME.PSM != PSM_PSMCT24 ? context->TEST.DATE : 0; + + GSTextureFX10::OMBlendSelector om_bsel; + + om_bsel.abe = PRIM->ABE || (prim == 1 || prim == 2) && PRIM->AA1; + om_bsel.a = context->ALPHA.A; + om_bsel.b = context->ALPHA.B; + om_bsel.c = context->ALPHA.C; + om_bsel.d = context->ALPHA.D; + om_bsel.wr = (context->FRAME.FBMSK & 0x000000ff) != 0x000000ff; + om_bsel.wg = (context->FRAME.FBMSK & 0x0000ff00) != 0x0000ff00; + om_bsel.wb = (context->FRAME.FBMSK & 0x00ff0000) != 0x00ff0000; + om_bsel.wa = (context->FRAME.FBMSK & 0xff000000) != 0xff000000; + + float bf = (float)(int)context->ALPHA.FIX / 0x80; + + // vs + + GSTextureFX10::VSSelector vs_sel; + + vs_sel.bpp = 0; + vs_sel.bppz = 0; + vs_sel.tme = PRIM->TME; + vs_sel.fst = PRIM->FST; + vs_sel.prim = prim; + + if(tex) + { + vs_sel.bpp = tex->m_bpp2; + } + + if(om_dssel.zte && om_dssel.ztst > 0 && om_dssel.zwe) + { + if(context->ZBUF.PSM == PSM_PSMZ24) + { + if(WrapZ(0xffffff)) + { + vs_sel.bppz = 1; + om_dssel.ztst = 1; + } + } + else if(context->ZBUF.PSM == PSM_PSMZ16 || context->ZBUF.PSM == PSM_PSMZ16S) + { + if(WrapZ(0xffff)) + { + vs_sel.bppz = 2; + om_dssel.ztst = 1; + } + } + } + + GSTextureFX10::VSConstantBuffer vs_cb; + + float sx = 2.0f * rt.m_scale.x / (rt.GetWidth() * 16); + float sy = 2.0f * rt.m_scale.y / (rt.GetHeight() * 16); + float ox = (float)(int)context->XYOFFSET.OFX; + float oy = (float)(int)context->XYOFFSET.OFY; + + vs_cb.VertexScale = GSVector4(sx, -sy, 1.0f / UINT_MAX, 0.0f); + vs_cb.VertexOffset = GSVector4(ox * sx + 1, -(oy * sy + 1), 0.0f, -1.0f); + vs_cb.TextureScale = GSVector2(1.0f, 1.0f); + + if(PRIM->TME && PRIM->FST) + { + vs_cb.TextureScale.x = 1.0f / (16 << context->TEX0.TW); + vs_cb.TextureScale.y = 1.0f / (16 << context->TEX0.TH); + } + + // gs + + GSTextureFX10::GSSelector gs_sel; + + gs_sel.iip = PRIM->IIP; + gs_sel.prim = GSUtil::GetPrimClass(prim); + + // ps + + GSTextureFX10::PSSelector ps_sel; + + ps_sel.fst = PRIM->FST; + ps_sel.wms = context->CLAMP.WMS; + ps_sel.wmt = context->CLAMP.WMT; + ps_sel.bpp = 0; + ps_sel.aem = env.TEXA.AEM; + ps_sel.tfx = context->TEX0.TFX; + ps_sel.tcc = context->TEX0.TCC; + ps_sel.ate = context->TEST.ATE; + ps_sel.atst = context->TEST.ATST; + ps_sel.fog = PRIM->FGE; + ps_sel.clr1 = om_bsel.abe && om_bsel.a == 1 && om_bsel.b == 2 && om_bsel.d == 1; + ps_sel.fba = context->FBA.FBA; + ps_sel.aout = context->FRAME.PSM == PSM_PSMCT16 || context->FRAME.PSM == PSM_PSMCT16S || (context->FRAME.FBMSK & 0xff000000) == 0x7f000000 ? 1 : 0; + + GSTextureFX10::PSSamplerSelector ps_ssel; + + ps_ssel.min = m_filter == 2 ? (context->TEX1.MMIN & 1) : m_filter; + ps_ssel.mag = m_filter == 2 ? (context->TEX1.MMAG & 1) : m_filter; + ps_ssel.tau = 0; + ps_ssel.tav = 0; + + GSTextureFX10::PSConstantBuffer ps_cb; + + ps_cb.FogColor = GSVector4(env.FOGCOL.FCR, env.FOGCOL.FCG, env.FOGCOL.FCB, 0) / 255.0f; + ps_cb.TA0 = (float)(int)env.TEXA.TA0 / 255; + ps_cb.TA1 = (float)(int)env.TEXA.TA1 / 255; + ps_cb.AREF = (float)(int)context->TEST.AREF / 255; + + if(context->TEST.ATST == 2 || context->TEST.ATST == 5) + { + ps_cb.AREF -= 0.9f/256; + } + else if(context->TEST.ATST == 3 || context->TEST.ATST == 6) + { + ps_cb.AREF += 0.9f/256; + } + + if(tex) + { + ps_sel.bpp = tex->m_bpp2; + + switch(context->CLAMP.WMS) + { + case 0: + ps_ssel.tau = 1; + break; + case 1: + ps_ssel.tau = 0; + break; + case 2: + ps_cb.MINU = ((float)(int)context->CLAMP.MINU + 0.5f) / (1 << context->TEX0.TW); + ps_cb.MAXU = ((float)(int)context->CLAMP.MAXU) / (1 << context->TEX0.TW); + ps_ssel.tau = 0; + break; + case 3: + ps_cb.UMSK = context->CLAMP.MINU; + ps_cb.UFIX = context->CLAMP.MAXU; + ps_ssel.tau = 1; + break; + default: + __assume(0); + } + + switch(context->CLAMP.WMT) + { + case 0: + ps_ssel.tav = 1; + break; + case 1: + ps_ssel.tav = 0; + break; + case 2: + ps_cb.MINV = ((float)(int)context->CLAMP.MINV + 0.5f) / (1 << context->TEX0.TH); + ps_cb.MAXV = ((float)(int)context->CLAMP.MAXV) / (1 << context->TEX0.TH); + ps_ssel.tav = 0; + break; + case 3: + ps_cb.VMSK = context->CLAMP.MINV; + ps_cb.VFIX = context->CLAMP.MAXV; + ps_ssel.tav = 1; + break; + default: + __assume(0); + } + + float w = (float)tex->m_texture.GetWidth(); + float h = (float)tex->m_texture.GetHeight(); + + ps_cb.WH = GSVector2(w, h); + ps_cb.rWrH = GSVector2(1.0f / w, 1.0f / h); + } + else + { + ps_sel.tfx = 4; + } + + // rs + + int w = rt.GetWidth(); + int h = rt.GetHeight(); + + CRect scissor = (CRect)GSVector4i(GSVector4(rt.m_scale).xyxy() * context->scissor.in) & CRect(0, 0, w, h); + + // + + m_tfx.SetupOM(om_dssel, om_bsel, bf, rt, ds); + m_tfx.SetupIA(m_vertices, m_count, topology); + m_tfx.SetupVS(vs_sel, &vs_cb); + m_tfx.SetupGS(gs_sel); + m_tfx.SetupPS(ps_sel, &ps_cb, ps_ssel, + tex ? (ID3D10ShaderResourceView*)tex->m_texture : NULL, + tex ? (ID3D10ShaderResourceView*)tex->m_palette : NULL); + m_tfx.SetupRS(w, h, scissor); + + // draw + + if(context->TEST.DoFirstPass()) + { + m_tfx.Draw(); + } + + if(context->TEST.DoSecondPass()) + { + ASSERT(!env.PABE.PABE); + + static const DWORD iatst[] = {1, 0, 5, 6, 7, 2, 3, 4}; + + ps_sel.atst = iatst[ps_sel.atst]; + + m_tfx.UpdatePS(ps_sel, &ps_cb, ps_ssel); + + bool z = om_dssel.zwe; + bool r = om_bsel.wr; + bool g = om_bsel.wg; + bool b = om_bsel.wb; + bool a = om_bsel.wa; + + switch(context->TEST.AFAIL) + { + case 0: z = r = g = b = a = false; break; // none + case 1: z = false; break; // rgba + case 2: r = g = b = a = false; break; // z + case 3: z = a = false; break; // rgb + default: __assume(0); + } + + if(z || r || g || b || a) + { + om_dssel.zwe = z; + om_bsel.wr = r; + om_bsel.wg = g; + om_bsel.wb = b; + om_bsel.wa = a; + + m_tfx.UpdateOM(om_dssel, om_bsel, bf); + + m_tfx.Draw(); + } + } + + m_dev.EndScene(); +} + +bool GSRendererHW10::WrapZ(DWORD maxz) +{ + // should only run once if z values are in the z buffer range + + for(int i = 0, j = m_count; i < j; i++) + { + if(m_vertices[i].p.z <= maxz) + { + return false; + } + } + + return true; +} + +void GSRendererHW10::SetupDATE(Texture& rt, Texture& ds) +{ + if(!m_context->TEST.DATE) return; // || (::GetAsyncKeyState(VK_CONTROL) & 0x8000) + + // sfex3 (after the capcom logo), vf4 (first menu fading in), ffxii shadows, rumble roses shadows + + GSVector4 mm; + + // TODO + + mm = GSVector4(-1, -1, 1, 1); +/* + MinMaxXY(mm); + + int w = rt.GetWidth(); + int h = rt.GetHeight(); + + float sx = 2.0f * rt.m_scale.x / (w * 16); + float sy = 2.0f * rt.m_scale.y / (h * 16); + float ox = (float)(int)m_context->XYOFFSET.OFX; + float oy = (float)(int)m_context->XYOFFSET.OFY; + + mm.x = (mm.x - ox) * sx - 1; + mm.y = (mm.y - oy) * sy - 1; + mm.z = (mm.z - ox) * sx - 1; + mm.w = (mm.w - oy) * sy - 1; + + if(mm.x < -1) mm.x = -1; + if(mm.y < -1) mm.y = -1; + if(mm.z > +1) mm.z = +1; + if(mm.w > +1) mm.w = +1; +*/ + GSVector4 uv = (mm + 1.0f) / 2.0f; + + // + + m_dev.BeginScene(); + + // om + + GSTexture10 tmp; + + m_dev.CreateRenderTarget(tmp, rt.GetWidth(), rt.GetHeight()); + + m_dev.OMSetRenderTargets(tmp, ds); + m_dev.OMSetDepthStencilState(m_date.dss, 1); + m_dev.OMSetBlendState(m_date.bs, 0); + + m_dev->ClearDepthStencilView(ds, D3D10_CLEAR_STENCIL, 0, 0); + + // ia + + GSVertexPT1 vertices[] = + { + {GSVector4(mm.x, -mm.y, 0.5f, 1.0f), GSVector2(uv.x, uv.y)}, + {GSVector4(mm.z, -mm.y, 0.5f, 1.0f), GSVector2(uv.z, uv.y)}, + {GSVector4(mm.x, -mm.w, 0.5f, 1.0f), GSVector2(uv.x, uv.w)}, + {GSVector4(mm.z, -mm.w, 0.5f, 1.0f), GSVector2(uv.z, uv.w)}, + }; + + D3D10_BOX box = {0, 0, 0, sizeof(vertices), 1, 1}; + m_dev->UpdateSubresource(m_dev.m_convert.vb, 0, &box, vertices, 0, 0); + + m_dev.IASetVertexBuffer(m_dev.m_convert.vb, sizeof(vertices[0])); + m_dev.IASetInputLayout(m_dev.m_convert.il); + m_dev.IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + + // vs + + m_dev.VSSetShader(m_dev.m_convert.vs, NULL); + + // gs + + m_dev.GSSetShader(NULL); + + // ps + + m_dev.PSSetShaderResources(rt, NULL); + m_dev.PSSetShader(m_dev.m_convert.ps[m_context->TEST.DATM ? 2 : 3], NULL); + m_dev.PSSetSamplerState(m_dev.m_convert.pt, NULL); + + // rs + + m_dev.RSSet(tmp.GetWidth(), tmp.GetHeight()); + + // set + + m_dev.DrawPrimitive(countof(vertices)); + + // + + m_dev.EndScene(); + + m_dev.Recycle(tmp); +} diff --git a/plugins/GSdx/GSRendererHW10.h b/plugins/GSdx/GSRendererHW10.h new file mode 100644 index 0000000000..03ae5935af --- /dev/null +++ b/plugins/GSdx/GSRendererHW10.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRendererHW.h" +#include "GSVertexHW.h" +#include "GSTextureCache10.h" +#include "GSTextureFX10.h" + +class GSRendererHW10 : public GSRendererHW +{ + typedef GSDevice10 Device; + typedef GSVertexHW10 Vertex; + typedef GSTextureCache10 TextureCache; + + bool WrapZ(DWORD maxz); + +protected: + GSTextureFX10 m_tfx; + + void Draw(int prim, Texture& rt, Texture& ds, GSTextureCache::GSTexture* tex); + + struct + { + CComPtr dss; + CComPtr bs; + } m_date; + + void SetupDATE(Texture& rt, Texture& ds); + +public: + GSRendererHW10(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs); + + bool Create(LPCTSTR title); + + template void VertexKick(bool skip); +}; \ No newline at end of file diff --git a/plugins/GSdx/GSRendererHW9.cpp b/plugins/GSdx/GSRendererHW9.cpp new file mode 100644 index 0000000000..21950cdab3 --- /dev/null +++ b/plugins/GSdx/GSRendererHW9.cpp @@ -0,0 +1,594 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSRendererHW9.h" +#include "GSCrc.h" +#include "resource.h" + +GSRendererHW9::GSRendererHW9(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs) + : GSRendererHW(base, mt, irq, nloophack, rs, false) +{ + m_fba.enabled = !!AfxGetApp()->GetProfileInt(_T("Settings"), _T("fba"), TRUE); + m_logz = !!AfxGetApp()->GetProfileInt(_T("Settings"), _T("logz"), FALSE); + + InitVertexKick(); +} + +bool GSRendererHW9::Create(LPCTSTR title) +{ + if(!__super::Create(title)) + return false; + + if(!m_tfx.Create(&m_dev)) + return false; + + // + + memset(&m_date.dss, 0, sizeof(m_date.dss)); + + m_date.dss.StencilEnable = true; + m_date.dss.StencilReadMask = 1; + m_date.dss.StencilWriteMask = 1; + m_date.dss.StencilFunc = D3DCMP_ALWAYS; + m_date.dss.StencilPassOp = D3DSTENCILOP_REPLACE; + + memset(&m_date.bs, 0, sizeof(m_date.bs)); + + // + + memset(&m_fba.dss, 0, sizeof(m_fba.dss)); + + m_fba.dss.StencilEnable = true; + m_fba.dss.StencilReadMask = 2; + m_fba.dss.StencilWriteMask = 2; + m_fba.dss.StencilFunc = D3DCMP_EQUAL; + m_fba.dss.StencilPassOp = D3DSTENCILOP_ZERO; + m_fba.dss.StencilFailOp = D3DSTENCILOP_ZERO; + m_fba.dss.StencilDepthFailOp = D3DSTENCILOP_ZERO; + + memset(&m_fba.bs, 0, sizeof(m_fba.bs)); + + m_fba.bs.RenderTargetWriteMask = D3DCOLORWRITEENABLE_ALPHA; + + // + + return true; +} + +template +void GSRendererHW9::VertexKick(bool skip) +{ + Vertex& dst = m_vl.AddTail(); + + dst.p.x = (float)m_v.XYZ.X; + dst.p.y = (float)m_v.XYZ.Y; + dst.p.z = (float)m_v.XYZ.Z; + + dst.c0 = m_v.RGBAQ.ai32[0]; + dst.c1 = m_v.FOG.ai32[1]; + + if(tme) + { + if(fst) + { + GSVector4::storel(&dst.t, m_v.GetUV()); + } + else + { + dst.t.x = m_v.ST.S; + dst.t.y = m_v.ST.T; + dst.p.w = m_v.RGBAQ.Q; + } + } + + DWORD count = 0; + + if(Vertex* v = DrawingKick(skip, count)) + { + GSVector4 scissor = m_context->scissor.dx9; + + GSVector4 pmin, pmax; + + switch(prim) + { + case GS_POINTLIST: + pmin = v[0].p; + pmax = v[0].p; + break; + case GS_LINELIST: + case GS_LINESTRIP: + case GS_SPRITE: + pmin = v[0].p.minv(v[1].p); + pmax = v[0].p.maxv(v[1].p); + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + pmin = v[0].p.minv(v[1].p).minv(v[2].p); + pmax = v[0].p.maxv(v[1].p).maxv(v[2].p); + break; + } + + GSVector4 test = (pmax < scissor) | (pmin > scissor.zwxy()); + + if(test.mask() & 3) + { + return; + } + + switch(prim) + { + case GS_POINTLIST: + break; + case GS_LINELIST: + case GS_LINESTRIP: + if(PRIM->IIP == 0) {v[0].c0 = v[1].c0;} + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + if(PRIM->IIP == 0) {v[0].c0 = v[1].c0 = v[2].c0;} + break; + case GS_SPRITE: + if(PRIM->IIP == 0) {v[0].c0 = v[1].c0;} + v[0].p.z = v[1].p.z; + v[0].p.w = v[1].p.w; + v[0].c1 = v[1].c1; + v[2] = v[1]; + v[3] = v[1]; + v[1].p.y = v[0].p.y; + v[1].t.y = v[0].t.y; + v[2].p.x = v[0].p.x; + v[2].t.x = v[0].t.x; + v[4] = v[1]; + v[5] = v[2]; + count += 4; + break; + } + + m_count += count; + } +} + +void GSRendererHW9::Draw(int prim, Texture& rt, Texture& ds, GSTextureCache::GSTexture* tex) +{ + GSDrawingEnvironment& env = m_env; + GSDrawingContext* context = m_context; + + D3DPRIMITIVETYPE topology; + int prims = 0; + + switch(prim) + { + case GS_POINTLIST: + topology = D3DPT_POINTLIST; + prims = m_count; + break; + case GS_LINELIST: + case GS_LINESTRIP: + topology = D3DPT_LINELIST; + prims = m_count / 2; + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + case GS_SPRITE: + topology = D3DPT_TRIANGLELIST; + prims = m_count / 3; + break; + default: + __assume(0); + } + + m_perfmon.Put(GSPerfMon::Prim, prims); + m_perfmon.Put(GSPerfMon::Draw, 1); + + // date + + SetupDATE(rt, ds); + + // + + m_dev.BeginScene(); + + m_dev->SetRenderState(D3DRS_SHADEMODE, PRIM->IIP ? D3DSHADE_GOURAUD : D3DSHADE_FLAT); // TODO + + // om + + GSTextureFX9::OMDepthStencilSelector om_dssel; + + om_dssel.zte = context->TEST.ZTE; + om_dssel.ztst = context->TEST.ZTST; + om_dssel.zwe = !context->ZBUF.ZMSK; + om_dssel.date = context->FRAME.PSM != PSM_PSMCT24 ? context->TEST.DATE : 0; + om_dssel.fba = m_fba.enabled ? context->FBA.FBA : 0; + + GSTextureFX9::OMBlendSelector om_bsel; + + om_bsel.abe = PRIM->ABE || (prim == 1 || prim == 2) && PRIM->AA1; + om_bsel.a = context->ALPHA.A; + om_bsel.b = context->ALPHA.B; + om_bsel.c = context->ALPHA.C; + om_bsel.d = context->ALPHA.D; + om_bsel.wr = (context->FRAME.FBMSK & 0x000000ff) != 0x000000ff; + om_bsel.wg = (context->FRAME.FBMSK & 0x0000ff00) != 0x0000ff00; + om_bsel.wb = (context->FRAME.FBMSK & 0x00ff0000) != 0x00ff0000; + om_bsel.wa = (context->FRAME.FBMSK & 0xff000000) != 0xff000000; + + BYTE bf = context->ALPHA.FIX >= 0x80 ? 0xff : (BYTE)(context->ALPHA.FIX * 2); + + // vs + + GSTextureFX9::VSSelector vs_sel; + + vs_sel.bppz = 0; + vs_sel.tme = PRIM->TME; + vs_sel.fst = PRIM->FST; + vs_sel.logz = m_logz ? 1 : 0; + + if(om_dssel.zte && om_dssel.ztst > 0 && om_dssel.zwe) + { + if(context->ZBUF.PSM == PSM_PSMZ24) + { + if(WrapZ(0xffffff)) + { + vs_sel.bppz = 1; + om_dssel.ztst = 1; + } + } + else if(context->ZBUF.PSM == PSM_PSMZ16 || context->ZBUF.PSM == PSM_PSMZ16S) + { + if(WrapZ(0xffff)) + { + vs_sel.bppz = 2; + om_dssel.ztst = 1; + } + } + } + + GSTextureFX9::VSConstantBuffer vs_cb; + + float sx = 2.0f * rt.m_scale.x / (rt.GetWidth() * 16); + float sy = 2.0f * rt.m_scale.y / (rt.GetHeight() * 16); + float ox = (float)(int)context->XYOFFSET.OFX; + float oy = (float)(int)context->XYOFFSET.OFY; + + vs_cb.VertexScale = GSVector4(sx, -sy, 1.0f / UINT_MAX, 0.0f); + vs_cb.VertexOffset = GSVector4(ox * sx + 1, -(oy * sy + 1), 0.0f, -1.0f); + vs_cb.TextureScale = GSVector2(1.0f, 1.0f); + + if(PRIM->TME && PRIM->FST) + { + vs_cb.TextureScale.x = 1.0f / (16 << context->TEX0.TW); + vs_cb.TextureScale.y = 1.0f / (16 << context->TEX0.TH); + } + + // ps + + GSTextureFX9::PSSelector ps_sel; + + ps_sel.fst = PRIM->FST; + ps_sel.wms = context->CLAMP.WMS; + ps_sel.wmt = context->CLAMP.WMT; + ps_sel.bpp = 0; + ps_sel.aem = env.TEXA.AEM; + ps_sel.tfx = context->TEX0.TFX; + ps_sel.tcc = context->TEX0.TCC; + ps_sel.ate = context->TEST.ATE; + ps_sel.atst = context->TEST.ATST; + ps_sel.fog = PRIM->FGE; + ps_sel.clr1 = om_bsel.abe && om_bsel.a == 1 && om_bsel.b == 2 && om_bsel.d == 1; + ps_sel.rt = tex && tex->m_rendered; + + GSTextureFX9::PSSamplerSelector ps_ssel; + + ps_ssel.min = m_filter == 2 ? (context->TEX1.MMIN & 1) : m_filter; + ps_ssel.mag = m_filter == 2 ? (context->TEX1.MMAG & 1) : m_filter; + ps_ssel.tau = 0; + ps_ssel.tav = 0; + + GSTextureFX9::PSConstantBuffer ps_cb; + + ps_cb.FogColor = GSVector4(env.FOGCOL.FCR, env.FOGCOL.FCG, env.FOGCOL.FCB, 0) / 255.0f; + ps_cb.TA0 = (float)(int)env.TEXA.TA0 / 255; + ps_cb.TA1 = (float)(int)env.TEXA.TA1 / 255; + ps_cb.AREF = (float)(int)context->TEST.AREF / 255; + + if(context->TEST.ATST == 2 || context->TEST.ATST == 5) + { + ps_cb.AREF -= 0.9f/256; + } + else if(context->TEST.ATST == 3 || context->TEST.ATST == 6) + { + ps_cb.AREF += 0.9f/256; + } + + if(tex) + { + ps_sel.bpp = tex->m_bpp2; + + switch(context->CLAMP.WMS) + { + case 0: + ps_ssel.tau = 1; + break; + case 1: + ps_ssel.tau = 0; + break; + case 2: + ps_cb.MINU = ((float)(int)context->CLAMP.MINU + 0.5f) / (1 << context->TEX0.TW); + ps_cb.MAXU = ((float)(int)context->CLAMP.MAXU) / (1 << context->TEX0.TW); + ps_ssel.tau = 0; + break; + case 3: + ps_cb.UMSK = context->CLAMP.MINU; + ps_cb.UFIX = context->CLAMP.MAXU; + ps_ssel.tau = 1; + break; + default: + __assume(0); + } + + switch(context->CLAMP.WMT) + { + case 0: + ps_ssel.tav = 1; + break; + case 1: + ps_ssel.tav = 0; + break; + case 2: + ps_cb.MINV = ((float)(int)context->CLAMP.MINV + 0.5f) / (1 << context->TEX0.TH); + ps_cb.MAXV = ((float)(int)context->CLAMP.MAXV) / (1 << context->TEX0.TH); + ps_ssel.tav = 0; + break; + case 3: + ps_cb.VMSK = context->CLAMP.MINV; + ps_cb.VFIX = context->CLAMP.MAXV; + ps_ssel.tav = 1; + break; + default: + __assume(0); + } + + float w = (float)tex->m_texture.GetWidth(); + float h = (float)tex->m_texture.GetHeight(); + + ps_cb.WH = GSVector2(w, h); + ps_cb.rWrH = GSVector2(1.0f / w, 1.0f / h); + } + else + { + ps_sel.tfx = 4; + } + + // rs + + int w = rt.GetWidth(); + int h = rt.GetHeight(); + + CRect scissor = (CRect)GSVector4i(GSVector4(rt.m_scale).xyxy() * context->scissor.in) & CRect(0, 0, w, h); + + // + + m_tfx.SetupOM(om_dssel, om_bsel, bf, rt, ds); + m_tfx.SetupIA(m_vertices, m_count, topology); + m_tfx.SetupVS(vs_sel, &vs_cb); + m_tfx.SetupPS(ps_sel, &ps_cb, ps_ssel, + tex ? (IDirect3DTexture9*)tex->m_texture : NULL, + tex ? (IDirect3DTexture9*)tex->m_palette : NULL, + m_psrr); + m_tfx.SetupRS(w, h, scissor); + + // draw + + if(context->TEST.DoFirstPass()) + { + m_dev.DrawPrimitive(); + } + + if(context->TEST.DoSecondPass()) + { + ASSERT(!env.PABE.PABE); + + static const DWORD iatst[] = {1, 0, 5, 6, 7, 2, 3, 4}; + + ps_sel.atst = iatst[ps_sel.atst]; + + m_tfx.UpdatePS(ps_sel, &ps_cb, ps_ssel, m_psrr); + + bool z = om_dssel.zwe; + bool r = om_bsel.wr; + bool g = om_bsel.wg; + bool b = om_bsel.wb; + bool a = om_bsel.wa; + + switch(context->TEST.AFAIL) + { + case 0: z = r = g = b = a = false; break; // none + case 1: z = false; break; // rgba + case 2: r = g = b = a = false; break; // z + case 3: z = a = false; break; // rgb + default: __assume(0); + } + + if(z || r || g || b || a) + { + om_dssel.zwe = z; + om_bsel.wr = r; + om_bsel.wg = g; + om_bsel.wb = b; + om_bsel.wa = a; + + m_tfx.UpdateOM(om_dssel, om_bsel, bf); + + m_dev.DrawPrimitive(); + } + } + + m_dev.EndScene(); + + if(om_dssel.fba) UpdateFBA(rt); +} + +bool GSRendererHW9::WrapZ(float maxz) +{ + // should only run once if z values are in the z buffer range + + for(int i = 0, j = m_count; i < j; i++) + { + if(m_vertices[i].p.z <= maxz) + { + return false; + } + } + + return true; +} + +void GSRendererHW9::SetupDATE(Texture& rt, Texture& ds) +{ + if(!m_context->TEST.DATE) return; // || (::GetAsyncKeyState(VK_CONTROL) & 0x8000) + + // sfex3 (after the capcom logo), vf4 (first menu fading in), ffxii shadows, rumble roses shadows + + GSVector4 mm; + + // TODO + + mm = GSVector4(-1, -1, 1, 1); +/* + MinMaxXY(mm); + + int w = rt.GetWidth(); + int h = rt.GetHeight(); + + float sx = 2.0f * rt.m_scale.x / (w * 16); + float sy = 2.0f * rt.m_scale.y / (h * 16); + float ox = (float)(int)m_context->XYOFFSET.OFX; + float oy = (float)(int)m_context->XYOFFSET.OFY; + + mm.x = (mm.x - ox) * sx - 1; + mm.y = (mm.y - oy) * sy - 1; + mm.z = (mm.z - ox) * sx - 1; + mm.w = (mm.w - oy) * sy - 1; + + if(mm.x < -1) mm.x = -1; + if(mm.y < -1) mm.y = -1; + if(mm.z > +1) mm.z = +1; + if(mm.w > +1) mm.w = +1; +*/ + GSVector4 uv = (mm + 1.0f) / 2.0f; + + // + + m_dev.BeginScene(); + + // om + + GSTexture9 tmp; + + m_dev.CreateRenderTarget(tmp, rt.GetWidth(), rt.GetHeight()); + + m_dev.OMSetRenderTargets(tmp, ds); + m_dev.OMSetDepthStencilState(&m_date.dss, 1); + m_dev.OMSetBlendState(&m_date.bs, 0); + + m_dev->Clear(0, NULL, D3DCLEAR_STENCIL, 0, 0, 0); + + // ia + + GSVertexPT1 vertices[] = + { + {GSVector4(mm.x, -mm.y, 0.5f, 1.0f), GSVector2(uv.x, uv.y)}, + {GSVector4(mm.z, -mm.y, 0.5f, 1.0f), GSVector2(uv.z, uv.y)}, + {GSVector4(mm.x, -mm.w, 0.5f, 1.0f), GSVector2(uv.x, uv.w)}, + {GSVector4(mm.z, -mm.w, 0.5f, 1.0f), GSVector2(uv.z, uv.w)}, + }; + + m_dev.IASetVertexBuffer(4, vertices); + m_dev.IASetInputLayout(m_dev.m_convert.il); + m_dev.IASetPrimitiveTopology(D3DPT_TRIANGLESTRIP); + + // vs + + m_dev.VSSetShader(m_dev.m_convert.vs, NULL, 0); + + // ps + + m_dev.PSSetShaderResources(rt, NULL); + m_dev.PSSetShader(m_dev.m_convert.ps[m_context->TEST.DATM ? 2 : 3], NULL, 0); + m_dev.PSSetSamplerState(&m_dev.m_convert.pt); + + // rs + + m_dev.RSSet(tmp.GetWidth(), tmp.GetHeight()); + + // + + m_dev.DrawPrimitive(); + + // + + m_dev.EndScene(); + + m_dev.Recycle(tmp); +} + +void GSRendererHW9::UpdateFBA(Texture& rt) +{ + m_dev.BeginScene(); + + // om + + m_dev.OMSetDepthStencilState(&m_fba.dss, 2); + m_dev.OMSetBlendState(&m_fba.bs, 0); + + // vs + + m_dev.VSSetShader(NULL, NULL, 0); + + // ps + + m_dev.PSSetShader(m_dev.m_convert.ps[4], NULL, 0); + + // + + int w = rt.GetWidth(); + int h = rt.GetHeight(); + + GSVertexP vertices[] = + { + {GSVector4(0, 0, 0, 0)}, + {GSVector4(w, 0, 0, 0)}, + {GSVector4(0, h, 0, 0)}, + {GSVector4(w, h, 0, 0)}, + }; + + m_dev->SetFVF(D3DFVF_XYZRHW); + + m_dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(vertices[0])); + + // + + m_dev.EndScene(); +} diff --git a/plugins/GSdx/GSRendererHW9.h b/plugins/GSdx/GSRendererHW9.h new file mode 100644 index 0000000000..18fcf8fe3d --- /dev/null +++ b/plugins/GSdx/GSRendererHW9.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRendererHW.h" +#include "GSVertexHW.h" +#include "GSTextureCache9.h" +#include "GSTextureFX9.h" + +class GSRendererHW9 : public GSRendererHW +{ + typedef GSDevice9 Device; + typedef GSVertexHW9 Vertex; + typedef GSTextureCache9 TextureCache; + + bool WrapZ(float maxz); + +protected: + GSTextureFX9 m_tfx; + bool m_logz; + + void Draw(int prim, Texture& rt, Texture& ds, GSTextureCache::GSTexture* tex); + + struct + { + Direct3DDepthStencilState9 dss; + Direct3DBlendState9 bs; + } m_date; + + struct + { + bool enabled; + Direct3DDepthStencilState9 dss; + Direct3DBlendState9 bs; + } m_fba; + + void SetupDATE(Texture& rt, Texture& ds); + void UpdateFBA(Texture& rt); + +public: + GSRendererHW9(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs); + + bool Create(LPCTSTR title); + + template void VertexKick(bool skip); +}; diff --git a/plugins/GSdx/GSRendererNull.cpp b/plugins/GSdx/GSRendererNull.cpp new file mode 100644 index 0000000000..f51c3a7cfa --- /dev/null +++ b/plugins/GSdx/GSRendererNull.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSRendererNull.h" diff --git a/plugins/GSdx/GSRendererNull.h b/plugins/GSdx/GSRendererNull.h new file mode 100644 index 0000000000..a375cf963d --- /dev/null +++ b/plugins/GSdx/GSRendererNull.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRenderer.h" +#include "GSDeviceNull.h" + +template class GSRendererNull : public GSRendererT +{ +protected: + void Draw() + { + } + + bool GetOutput(int i, Texture& t) + { + return false; + } + +public: + GSRendererNull(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs) + : GSRendererT(base, mt, irq, nloophack, rs) + { + InitVertexKick >(); + } + + template void VertexKick(bool skip) + { + } +}; \ No newline at end of file diff --git a/plugins/GSdx/GSRendererSW.cpp b/plugins/GSdx/GSRendererSW.cpp new file mode 100644 index 0000000000..93eaa2336e --- /dev/null +++ b/plugins/GSdx/GSRendererSW.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSRendererSW.h" + +const GSVector4 g_pos_scale(1.0f / 16, 1.0f / 16, 1.0f, 128.0f); diff --git a/plugins/GSdx/GSRendererSW.h b/plugins/GSdx/GSRendererSW.h new file mode 100644 index 0000000000..b1bbd571d0 --- /dev/null +++ b/plugins/GSdx/GSRendererSW.h @@ -0,0 +1,858 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRenderer.h" +#include "GSTextureCacheSW.h" +#include "GSDrawScanline.h" + +extern const GSVector4 g_pos_scale; + +template +class GSRendererSW : public GSRendererT +{ +protected: + GSRasterizerList m_rl; + GSTextureCacheSW* m_tc; + GSVertexTrace m_vtrace; + Texture m_texture[2]; + bool m_reset; + + void Reset() + { + // TODO: GSreset can come from the main thread too => crash + // m_tc->RemoveAll(); + + m_reset = true; + + __super::Reset(); + } + + void VSync(int field) + { + __super::VSync(field); + + m_tc->IncAge(); + + if(m_reset) + { + m_tc->RemoveAll(); + + m_reset = false; + } + + // if((m_perfmon.GetFrame() & 255) == 0) m_rl.PrintStats(); + } + + void ResetDevice() + { + m_texture[0] = Texture(); + m_texture[1] = Texture(); + } + + bool GetOutput(int i, Texture& t) + { + CRect r(0, 0, DISPFB[i]->FBW * 64, GetFrameRect(i).bottom); + + // TODO: round up bottom + + if(m_texture[i].GetWidth() != r.Width() || m_texture[i].GetHeight() != r.Height()) + { + m_texture[i] = Texture(); + } + + if(!m_texture[i] && !m_dev.CreateTexture(m_texture[i], r.Width(), r.Height())) + { + return false; + } + + GIFRegTEX0 TEX0; + + TEX0.TBP0 = DISPFB[i]->Block(); + TEX0.TBW = DISPFB[i]->FBW; + TEX0.PSM = DISPFB[i]->PSM; + + GIFRegCLAMP CLAMP; + + CLAMP.WMS = CLAMP.WMT = 1; + + // TODO + static BYTE* buff = (BYTE*)_aligned_malloc(1024 * 1024 * 4, 16); + static int pitch = 1024 * 4; + + m_mem.ReadTexture(r, buff, pitch, TEX0, m_env.TEXA, CLAMP); + + m_texture[i].Update(r, buff, pitch); + + t = m_texture[i]; + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp1\\_%05d_f%I64d_fr%d_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), i, (int)TEX0.TBP0, (int)TEX0.PSM); + if(s_save) t.Save(str); + } + + return true; + } + + bool TryAlphaTest(DWORD& fm, DWORD& zm) + { + const GSDrawingEnvironment& env = m_env; + const GSDrawingContext* context = m_context; + + bool pass = true; + + if(context->TEST.ATST == ATST_NEVER) + { + pass = false; + } + else if(context->TEST.ATST != ATST_ALWAYS) + { + GSVector4i af = GSVector4i(m_vtrace.m_min.c.wwww(m_vtrace.m_max.c)) >> 7; + + int amin, amax; + + if(PRIM->TME && (context->TEX0.TCC || context->TEX0.TFX == TFX_DECAL)) + { + DWORD bpp = GSLocalMemory::m_psm[context->TEX0.PSM].trbpp; + DWORD cbpp = GSLocalMemory::m_psm[context->TEX0.CPSM].trbpp; + DWORD pal = GSLocalMemory::m_psm[context->TEX0.PSM].pal; + + if(bpp == 32) + { + return false; + } + else if(bpp == 24) + { + amin = env.TEXA.AEM ? 0 : env.TEXA.TA0; + amax = env.TEXA.TA0; + } + else if(bpp == 16) + { + amin = env.TEXA.AEM ? 0 : min(env.TEXA.TA0, env.TEXA.TA1); + amax = max(env.TEXA.TA0, env.TEXA.TA1); + } + else + { + m_mem.m_clut.GetAlphaMinMax32(amin, amax); + } + + switch(context->TEX0.TFX) + { + case TFX_MODULATE: + amin = (amin * af.x) >> 7; + amax = (amax * af.z) >> 7; + if(amin > 255) amin = 255; + if(amax > 255) amax = 255; + break; + case TFX_DECAL: + break; + case TFX_HIGHLIGHT: + amin = amin + af.x; + amax = amax + af.z; + if(amin > 255) amin = 255; + if(amax > 255) amax = 255; + break; + case TFX_HIGHLIGHT2: + break; + default: + __assume(0); + } + } + else + { + amin = af.x; + amax = af.z; + } + + int aref = context->TEST.AREF; + + switch(context->TEST.ATST) + { + case ATST_NEVER: + pass = false; + break; + case ATST_ALWAYS: + pass = true; + break; + case ATST_LESS: + if(amax < aref) pass = true; + else if(amin >= aref) pass = false; + else return false; + break; + case ATST_LEQUAL: + if(amax <= aref) pass = true; + else if(amin > aref) pass = false; + else return false; + break; + case ATST_EQUAL: + if(amin == aref && amax == aref) pass = true; + else if(amin > aref || amax < aref) pass = false; + else return false; + break; + case ATST_GEQUAL: + if(amin >= aref) pass = true; + else if(amax < aref) pass = false; + else return false; + break; + case ATST_GREATER: + if(amin > aref) pass = true; + else if(amax <= aref) pass = false; + else return false; + break; + case ATST_NOTEQUAL: + if(amin == aref && amax == aref) pass = false; + else if(amin > aref || amax < aref) pass = true; + else return false; + break; + default: + __assume(0); + } + } + + if(!pass) + { + switch(context->TEST.AFAIL) + { + case AFAIL_KEEP: fm = zm = 0xffffffff; break; + case AFAIL_FB_ONLY: zm = 0xffffffff; break; + case AFAIL_ZB_ONLY: fm = 0xffffffff; break; + case AFAIL_RGB_ONLY: fm |= 0xff000000; zm = 0xffffffff; break; + default: __assume(0); + } + } + + return true; + } + + void GetScanlineParam(GSScanlineParam& p, GS_PRIM_CLASS primclass) + { + const GSDrawingEnvironment& env = m_env; + const GSDrawingContext* context = m_context; + + p.vm = m_mem.m_vm32; + + p.fbo = m_mem.GetOffset(context->FRAME.Block(), context->FRAME.FBW, context->FRAME.PSM); + p.zbo = m_mem.GetOffset(context->ZBUF.Block(), context->FRAME.FBW, context->ZBUF.PSM); + p.fzbo = m_mem.GetOffset4(context->FRAME, context->ZBUF); + + p.sel.dw = 0; + + p.sel.fpsm = 3; + p.sel.zpsm = 3; + p.sel.atst = ATST_ALWAYS; + p.sel.tfx = TFX_NONE; + p.sel.abe = 255; + p.sel.sprite = primclass == GS_SPRITE_CLASS ? 1 : 0; + + p.fm = context->FRAME.FBMSK; + p.zm = context->ZBUF.ZMSK || context->TEST.ZTE == 0 ? 0xffffffff : 0; + + if(context->TEST.ZTE && context->TEST.ZTST == ZTST_NEVER) + { + p.fm = 0xffffffff; + p.zm = 0xffffffff; + } + + if(PRIM->TME) + { + m_mem.m_clut.Read32(context->TEX0, env.TEXA); + } + + if(context->TEST.ATE) + { + if(!TryAlphaTest(p.fm, p.zm)) + { + p.sel.atst = context->TEST.ATST; + p.sel.afail = context->TEST.AFAIL; + } + } + + bool fwrite = p.fm != 0xffffffff; + bool ftest = p.sel.atst != ATST_ALWAYS || context->TEST.DATE && context->FRAME.PSM != PSM_PSMCT24; + + if(fwrite || ftest) + { + p.sel.fpsm = GSUtil::EncodePSM(context->FRAME.PSM); + + if((primclass == GS_LINE_CLASS || primclass == GS_TRIANGLE_CLASS) && m_vtrace.m_eq.rgba != 15) + { + p.sel.iip = PRIM->IIP; + } + + if(PRIM->TME) + { + p.sel.tfx = context->TEX0.TFX; + p.sel.tcc = context->TEX0.TCC; + p.sel.fst = PRIM->FST; + p.sel.ltf = context->TEX1.IsLinear(); + p.sel.tlu = GSLocalMemory::m_psm[context->TEX0.PSM].pal > 0; + + if(p.sel.iip == 0 && p.sel.tfx == TFX_MODULATE && p.sel.tcc) + { + if(m_vtrace.m_eq.rgba == 15 && (m_vtrace.m_min.c == GSVector4(128.0f * 128.0f)).alltrue()) + { + // modulate does not do anything when vertex color is 0x80 + + p.sel.tfx = TFX_DECAL; + } + } + + if(p.sel.tfx == TFX_DECAL) + { + p.sel.tcc = 1; + } + + if(p.sel.fst == 0) + { + // skip per pixel division if q is constant + + GSVertexSW* v = m_vertices; + + if(m_vtrace.m_eq.q) + { + p.sel.fst = 1; + + if(v[0].t.z != 1.0f) + { + GSVector4 w = v[0].t.zzzz().rcpnr(); + + for(int i = 0, j = m_count; i < j; i++) + { + v[i].t *= w; + } + + m_vtrace.m_min.t *= w; + m_vtrace.m_max.t *= w; + } + } + else if(primclass == GS_SPRITE_CLASS) + { + p.sel.fst = 1; + + GSVector4 tmin = GSVector4(FLT_MAX); + GSVector4 tmax = GSVector4(-FLT_MAX); + + for(int i = 0, j = m_count; i < j; i += 2) + { + GSVector4 w = v[i + 1].t.zzzz().rcpnr(); + + GSVector4 v0 = v[i + 0].t * w; + GSVector4 v1 = v[i + 1].t * w; + + v[i + 0].t = v0; + v[i + 1].t = v1; + + tmin = tmin.minv(v0).minv(v1); + tmax = tmax.maxv(v0).maxv(v1); + } + + m_vtrace.m_max.t = tmax; + m_vtrace.m_min.t = tmin; + } + } + + if(p.sel.fst) + { + // if q is constant we can do the half pel shift for bilinear sampling on the vertices + + if(p.sel.ltf) + { + GSVector4 half(0x8000, 0x8000); + + GSVertexSW* v = m_vertices; + + for(int i = 0, j = m_count; i < j; i++) + { + v[i].t -= half; + } + + m_vtrace.m_min.t -= half; + m_vtrace.m_max.t += half; + } + } + /* + else + { + GSVector4 tmin = GSVector4(FLT_MAX); + GSVector4 tmax = GSVector4(-FLT_MAX); + + GSVertexSW* v = m_vertices; + + for(int i = 0, j = m_count; i < j; i++) + { + GSVector4 v0 = v[i].t * v[i].t.zzzz().rcpnr(); + + tmin = tmin.minv(v0); + tmax = tmax.maxv(v0); + } + + if(p.sel.ltf) + { + GSVector4 half(0x8000, 0x8000); + + tmin -= half; + tmax += half; + } + + m_vtrace.min.t = tmin; + m_vtrace.max.t = tmax; + } + */ + + CRect r; + + int w = 1 << context->TEX0.TW; + int h = 1 << context->TEX0.TH; + + MinMaxUV(w, h, r, p.sel.fst); + + const GSTextureCacheSW::GSTexture* t = m_tc->Lookup(context->TEX0, env.TEXA, &r); + + if(!t) {ASSERT(0); return;} + + p.tex = t->m_buff; + p.clut = m_mem.m_clut; + p.tw = t->m_tw; + } + + p.sel.fge = PRIM->FGE; + + if(context->FRAME.PSM != PSM_PSMCT24) + { + p.sel.date = context->TEST.DATE; + } + + if(PRIM->ABE) + { + if(!context->ALPHA.IsOpaque()) + { + p.sel.abe = context->ALPHA.ai32[0]; + p.sel.pabe = env.PABE.PABE; + } + } + + if(PRIM->AA1) + { + // TODO: automatic alpha blending (ABE=1, A=0 B=1 C=0 D=1) + } + + if(p.sel.date + || p.sel.abea == 1 || p.sel.abeb == 1 || p.sel.abec == 1 || p.sel.abed == 1 + || p.sel.atst != ATST_ALWAYS && p.sel.afail == AFAIL_RGB_ONLY + || p.sel.fpsm == 0 && p.fm != 0 && p.fm != 0xffffffff + || p.sel.fpsm == 1 && (p.fm & 0x00ffffff) != 0 && (p.fm & 0x00ffffff) != 0x00ffffff + || p.sel.fpsm == 2 && (p.fm & 0x80f8f8f8) != 0 && (p.fm & 0x80f8f8f8) != 0x80f8f8f8) + { + p.sel.rfb = 1; + } + } + + bool zwrite = p.zm != 0xffffffff; + bool ztest = context->TEST.ZTE && context->TEST.ZTST > 1; + + if(zwrite || ztest) + { + p.sel.zpsm = GSUtil::EncodePSM(context->ZBUF.PSM); + p.sel.ztst = ztest ? context->TEST.ZTST : 1; + } + } + + void Draw() + { + m_vtrace.Update(m_vertices, m_count); + + GS_PRIM_CLASS primclass = GSUtil::GetPrimClass(PRIM->PRIM); + + GSScanlineParam p; + + GetScanlineParam(p, primclass); + + if((p.fm & p.zm) == 0xffffffff) + { + return; + } + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp1\\_%05d_f%I64d_tex_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), (int)m_context->TEX0.TBP0, (int)m_context->TEX0.PSM); + if(PRIM->TME) if(s_save) {m_mem.SaveBMP(str, m_context->TEX0.TBP0, m_context->TEX0.TBW, m_context->TEX0.PSM, 1 << m_context->TEX0.TW, 1 << m_context->TEX0.TH);} + str.Format(_T("c:\\temp1\\_%05d_f%I64d_rt0_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), m_context->FRAME.Block(), m_context->FRAME.PSM); + if(s_save) {m_mem.SaveBMP(str, m_context->FRAME.Block(), m_context->FRAME.FBW, m_context->FRAME.PSM, GetFrameSize(1).cx, 512);}//GetFrameSize(1).cy); + str.Format(_T("c:\\temp1\\_%05d_f%I64d_rz0_%05x_%d.bmp"), s_n-1, m_perfmon.GetFrame(), m_context->ZBUF.Block(), m_context->ZBUF.PSM); + if(s_savez) {m_mem.SaveBMP(str, m_context->ZBUF.Block(), m_context->FRAME.FBW, m_context->ZBUF.PSM, GetFrameSize(1).cx, 512);} + } + + GSRasterizerData data; + + data.scissor = GSVector4i(m_context->scissor.in); + data.scissor.z = min(data.scissor.z, (int)m_context->FRAME.FBW * 64); // TODO: find a game that overflows and check which one is the right behaviour + data.primclass = primclass; + data.vertices = m_vertices; + data.count = m_count; + data.param = &p; + + m_rl.Draw(&data); + + GSRasterizerStats stats; + + m_rl.GetStats(stats); + + m_perfmon.Put(GSPerfMon::Draw, 1); + m_perfmon.Put(GSPerfMon::Prim, stats.prims); + m_perfmon.Put(GSPerfMon::Fillrate, stats.pixels); + + GSVector4i pos(m_vtrace.m_min.p.xyxy(m_vtrace.m_max.p)); + + GSVector4i scissor = data.scissor; + + CRect r; + + r.left = max(scissor.x, min(scissor.z, pos.x)); + r.top = max(scissor.y, min(scissor.w, pos.y)); + r.right = max(scissor.x, min(scissor.z, pos.z)); + r.bottom = max(scissor.y, min(scissor.w, pos.w)); + + GIFRegBITBLTBUF BITBLTBUF; + + BITBLTBUF.DBW = m_context->FRAME.FBW; + + if(p.fm != 0xffffffff) + { + BITBLTBUF.DBP = m_context->FRAME.Block(); + BITBLTBUF.DPSM = m_context->FRAME.PSM; + + m_tc->InvalidateVideoMem(BITBLTBUF, r); + } + + if(p.zm != 0xffffffff) + { + BITBLTBUF.DBP = m_context->ZBUF.Block(); + BITBLTBUF.DPSM = m_context->ZBUF.PSM; + + m_tc->InvalidateVideoMem(BITBLTBUF, r); + } + + if(s_dump) + { + CString str; + str.Format(_T("c:\\temp1\\_%05d_f%I64d_rt1_%05x_%d.bmp"), s_n++, m_perfmon.GetFrame(), m_context->FRAME.Block(), m_context->FRAME.PSM); + if(s_save) {m_mem.SaveBMP(str, m_context->FRAME.Block(), m_context->FRAME.FBW, m_context->FRAME.PSM, GetFrameSize(1).cx, 512);}//GetFrameSize(1).cy); + str.Format(_T("c:\\temp1\\_%05d_f%I64d_rz1_%05x_%d.bmp"), s_n-1, m_perfmon.GetFrame(), m_context->ZBUF.Block(), m_context->ZBUF.PSM); + if(s_savez) {m_mem.SaveBMP(str, m_context->ZBUF.Block(), m_context->FRAME.FBW, m_context->ZBUF.PSM, GetFrameSize(1).cx, 512);} + } + } + + void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, CRect r) + { + m_tc->InvalidateVideoMem(BITBLTBUF, r); + } + + void MinMaxUV(int w, int h, CRect& r, DWORD fst) + { + const GSDrawingContext* context = m_context; + + int wms = context->CLAMP.WMS; + int wmt = context->CLAMP.WMT; + + int minu = (int)context->CLAMP.MINU; + int minv = (int)context->CLAMP.MINV; + int maxu = (int)context->CLAMP.MAXU; + int maxv = (int)context->CLAMP.MAXV; + + GSVector4i vr(0, 0, w, h); + + switch(wms) + { + case CLAMP_REPEAT: + break; + case CLAMP_CLAMP: + break; + case CLAMP_REGION_CLAMP: + if(vr.x < minu) vr.x = minu; + if(vr.z > maxu + 1) vr.z = maxu + 1; + break; + case CLAMP_REGION_REPEAT: + vr.x = maxu; + vr.z = vr.x + (minu + 1); + break; + default: + __assume(0); + } + + switch(wmt) + { + case CLAMP_REPEAT: + break; + case CLAMP_CLAMP: + break; + case CLAMP_REGION_CLAMP: + if(vr.y < minv) vr.y = minv; + if(vr.w > maxv + 1) vr.w = maxv + 1; + break; + case CLAMP_REGION_REPEAT: + vr.y = maxv; + vr.w = vr.y + (minv + 1); + break; + default: + __assume(0); + } + + if(fst) + { + GSVector4i uv = GSVector4i(m_vtrace.m_min.t.xyxy(m_vtrace.m_max.t)).sra32(16); +/* + int tw = context->TEX0.TW; + int th = context->TEX0.TH; + + GSVector4i u = uv & GSVector4i::xffffffff().srl32(32 - tw); + GSVector4i v = uv & GSVector4i::xffffffff().srl32(32 - th); + + GSVector4i uu = uv.sra32(tw); + GSVector4i vv = uv.sra32(th); + + int mask = (uu.upl32(vv) == uu.uph32(vv)).mask(); +*/ + switch(wms) + { + case CLAMP_REPEAT: +/* + if(mask & 0x000f) + { + if(vr.x < u.x) vr.x = u.x; + if(vr.z > u.z + 1) vr.z = u.z + 1; + } +*/ + break; + case CLAMP_CLAMP: + case CLAMP_REGION_CLAMP: + if(vr.x < uv.x) vr.x = uv.x; + if(vr.z > uv.z + 1) vr.z = uv.z + 1; + break; + case CLAMP_REGION_REPEAT: // TODO + break; + default: + __assume(0); + } + + switch(wmt) + { + case CLAMP_REPEAT: +/* + if(mask & 0xf000) + { + if(vr.y < v.y) vr.y = v.y; + if(vr.w > v.w + 1) vr.w = v.w + 1; + } +*/ + break; + case CLAMP_CLAMP: + case CLAMP_REGION_CLAMP: + if(vr.y < uv.y) vr.y = uv.y; + if(vr.w > uv.w + 1) vr.w = uv.w + 1; + break; + case CLAMP_REGION_REPEAT: // TODO + break; + default: + __assume(0); + } + } + + r = vr; + + r &= CRect(0, 0, w, h); + } + +public: + GSRendererSW(BYTE* base, bool mt, void (*irq)(), int nloophack, const GSRendererSettings& rs, int threads) + : GSRendererT(base, mt, irq, nloophack, rs) + { + m_rl.Create(this, threads); + + m_tc = new GSTextureCacheSW(this); + + InitVertexKick >(); + } + + virtual ~GSRendererSW() + { + delete m_tc; + } + + template + void VertexKick(bool skip) + { + const GSDrawingContext* context = m_context; + + GSVector4i xy = GSVector4i::load((int)m_v.XYZ.ai32[0]); + + xy = xy.insert16<3>(m_v.FOG.F); + xy = xy.upl16(); + xy -= context->XYOFFSET; + + GSVertexSW v; + + v.p = GSVector4(xy) * g_pos_scale; + + v.c = GSVector4(GSVector4i::load((int)m_v.RGBAQ.ai32[0]).u8to32() << 7); + + if(tme) + { + float q; + + if(fst) + { + v.t = GSVector4(((GSVector4i)m_v.UV).upl16() << (16 - 4)); + q = 1.0f; + } + else + { + v.t = GSVector4(m_v.ST.S, m_v.ST.T); + v.t *= GSVector4(0x10000 << context->TEX0.TW, 0x10000 << context->TEX0.TH); + q = m_v.RGBAQ.Q; + } + + v.t = v.t.xyxy(GSVector4::load(q)); + } + + GSVertexSW& dst = m_vl.AddTail(); + + dst = v; + + dst.p.z = (float)min(m_v.XYZ.Z, 0xffffff00); // max value which can survive the DWORD => float => DWORD conversion + + DWORD count = 0; + + if(GSVertexSW* v = DrawingKick(skip, count)) + { + GSVector4 pmin, pmax; + + switch(prim) + { + case GS_POINTLIST: + pmin = v[0].p; + pmax = v[0].p; + break; + case GS_LINELIST: + case GS_LINESTRIP: + case GS_SPRITE: + pmin = v[0].p.minv(v[1].p); + pmax = v[0].p.maxv(v[1].p); + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + pmin = v[0].p.minv(v[1].p).minv(v[2].p); + pmax = v[0].p.maxv(v[1].p).maxv(v[2].p); + break; + } + + GSVector4 scissor = context->scissor.ex; + + GSVector4 test = (pmax < scissor) | (pmin > scissor.zwxy()); + + switch(prim) + { + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + case GS_SPRITE: + test |= pmin.ceil() == pmax.ceil(); + break; + } + + if(test.mask() & 3) + { + return; + } + + switch(prim) + { + case GS_POINTLIST: + break; + case GS_LINELIST: + case GS_LINESTRIP: + if(PRIM->IIP == 0) {v[0].c = v[1].c;} + break; + case GS_TRIANGLELIST: + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + if(PRIM->IIP == 0) {v[0].c = v[2].c; v[1].c = v[2].c;} + break; + case GS_SPRITE: + break; + } + + if(m_count >= 3 && m_count < 30) + { + GSVertexSW* v = &m_vertices[m_count - 3]; + + int tl = 0; + int br = 0; + + bool isquad = false; + + switch(prim) + { + case GS_TRIANGLESTRIP: + case GS_TRIANGLEFAN: + case GS_TRIANGLELIST: + isquad = GSVertexSW::IsQuad(v, tl, br); + break; + } + + if(isquad) + { + m_count -= 3; + + if(m_count > 0) + { + tl += m_count; + br += m_count; + + Flush(); + } + + if(tl != 0) m_vertices[0] = m_vertices[tl]; + if(br != 1) m_vertices[1] = m_vertices[br]; + + m_count = 2; + + UINT32 tmp = PRIM->PRIM; + PRIM->PRIM = GS_SPRITE; + + Flush(); + + PRIM->PRIM = tmp; + + m_perfmon.Put(GSPerfMon::Quad, 1); + + return; + } + } + + m_count += count; + } + } +}; diff --git a/plugins/GSdx/GSSetting.cpp b/plugins/GSdx/GSSetting.cpp new file mode 100644 index 0000000000..0221e7b54f --- /dev/null +++ b/plugins/GSdx/GSSetting.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSSetting.h" + diff --git a/plugins/GSdx/GSSetting.h b/plugins/GSdx/GSSetting.h new file mode 100644 index 0000000000..c771fb2243 --- /dev/null +++ b/plugins/GSdx/GSSetting.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +struct GSSetting +{ + DWORD id; + const TCHAR* name; + const TCHAR* note; + + static void InitComboBox(const GSSetting* settings, int count, CComboBox& combobox, DWORD sel, DWORD maxid = ~0) + { + for(int i = 0; i < count; i++) + { + if(settings[i].id <= maxid) + { + CString str = settings[i].name; + if(settings[i].note != NULL) str = str + _T(" (") + settings[i].note + _T(")"); + int item = combobox.AddString(str); + combobox.SetItemData(item, settings[i].id); + if(settings[i].id == sel) combobox.SetCurSel(item); + } + } + } + +}; diff --git a/plugins/GSdx/GSSettingsDlg.cpp b/plugins/GSdx/GSSettingsDlg.cpp new file mode 100644 index 0000000000..903108cf52 --- /dev/null +++ b/plugins/GSdx/GSSettingsDlg.cpp @@ -0,0 +1,324 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSdx.h" +#include "GSSettingsDlg.h" +#include "GSUtil.h" +#include +#include + +GSSetting GSSettingsDlg::g_renderers[] = +{ + {0, _T("Direct3D9 (Hardware)"), NULL}, + {1, _T("Direct3D9 (Software)"), NULL}, + {2, _T("Direct3D9 (Null)"), NULL}, + {3, _T("Direct3D10 (Hardware)"), NULL}, + {4, _T("Direct3D10 (Software)"), NULL}, + {5, _T("Direct3D10 (Null)"), NULL}, + {6, _T("Null (Software)"), NULL}, + {7, _T("Null (Null)"), NULL}, +}; + +GSSetting GSSettingsDlg::g_psversion[] = +{ + {D3DPS_VERSION(3, 0), _T("Pixel Shader 3.0"), NULL}, + {D3DPS_VERSION(2, 0), _T("Pixel Shader 2.0"), NULL}, + //{D3DPS_VERSION(1, 4), _T("Pixel Shader 1.4"), NULL}, + //{D3DPS_VERSION(1, 1), _T("Pixel Shader 1.1"), NULL}, + //{D3DPS_VERSION(0, 0), _T("Fixed Pipeline (bogus)"), NULL}, +}; + +GSSetting GSSettingsDlg::g_interlace[] = +{ + {0, _T("None"), NULL}, + {1, _T("Weave tff"), _T("saw-tooth")}, + {2, _T("Weave bff"), _T("saw-tooth")}, + {3, _T("Bob tff"), _T("use blend if shaking")}, + {4, _T("Bob bff"), _T("use blend if shaking")}, + {5, _T("Blend tff"), _T("slight blur, 1/2 fps")}, + {6, _T("Blend bff"), _T("slight blur, 1/2 fps")}, +}; + +GSSetting GSSettingsDlg::g_aspectratio[] = +{ + {0, _T("Stretch"), NULL}, + {1, _T("4:3"), NULL}, + {2, _T("16:9"), NULL}, +}; + +IMPLEMENT_DYNAMIC(GSSettingsDlg, CDialog) +GSSettingsDlg::GSSettingsDlg(CWnd* pParent /*=NULL*/) + : CDialog(GSSettingsDlg::IDD, pParent) + , m_tvout(FALSE) + , m_filter(1) + , m_nloophack(2) + , m_nativeres(FALSE) + , m_vsync(FALSE) + , m_logz(FALSE) + , m_fba(TRUE) +{ +} + +GSSettingsDlg::~GSSettingsDlg() +{ +} + +LRESULT GSSettingsDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) +{ + LRESULT ret = __super::DefWindowProc(message, wParam, lParam); + + if(message == WM_INITDIALOG) + { + SendMessage(WM_KICKIDLE); + } + + return ret; +} + +void GSSettingsDlg::DoDataExchange(CDataExchange* pDX) +{ + __super::DoDataExchange(pDX); + DDX_Control(pDX, IDC_COMBO3, m_resolution); + DDX_Control(pDX, IDC_COMBO1, m_renderer); + DDX_Control(pDX, IDC_COMBO4, m_psversion); + DDX_Control(pDX, IDC_COMBO2, m_interlace); + DDX_Control(pDX, IDC_COMBO5, m_aspectratio); + DDX_Check(pDX, IDC_CHECK3, m_tvout); + DDX_Check(pDX, IDC_CHECK4, m_filter); + DDX_Check(pDX, IDC_CHECK6, m_nloophack); + DDX_Control(pDX, IDC_SPIN1, m_resx); + DDX_Control(pDX, IDC_SPIN2, m_resy); + DDX_Control(pDX, IDC_SPIN3, m_swthreads); + DDX_Check(pDX, IDC_CHECK1, m_nativeres); + DDX_Control(pDX, IDC_EDIT1, m_resxedit); + DDX_Control(pDX, IDC_EDIT2, m_resyedit); + DDX_Control(pDX, IDC_EDIT3, m_swthreadsedit); + DDX_Check(pDX, IDC_CHECK2, m_vsync); + DDX_Check(pDX, IDC_CHECK5, m_logz); + DDX_Check(pDX, IDC_CHECK7, m_fba); +} + +BEGIN_MESSAGE_MAP(GSSettingsDlg, CDialog) + ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle) + ON_UPDATE_COMMAND_UI(IDC_SPIN1, OnUpdateResolution) + ON_UPDATE_COMMAND_UI(IDC_SPIN2, OnUpdateResolution) + ON_UPDATE_COMMAND_UI(IDC_EDIT1, OnUpdateResolution) + ON_UPDATE_COMMAND_UI(IDC_EDIT2, OnUpdateResolution) + ON_UPDATE_COMMAND_UI(IDC_COMBO4, OnUpdateD3D9Options) + ON_UPDATE_COMMAND_UI(IDC_CHECK3, OnUpdateD3D9Options) + ON_UPDATE_COMMAND_UI(IDC_CHECK5, OnUpdateD3D9Options) + ON_UPDATE_COMMAND_UI(IDC_CHECK7, OnUpdateD3D9Options) + ON_UPDATE_COMMAND_UI(IDC_SPIN3, OnUpdateSWOptions) + ON_UPDATE_COMMAND_UI(IDC_EDIT3, OnUpdateSWOptions) + ON_CBN_SELCHANGE(IDC_COMBO1, &GSSettingsDlg::OnCbnSelchangeCombo1) +END_MESSAGE_MAP() + +void GSSettingsDlg::OnKickIdle() +{ + UpdateDialogControls(this, false); +} + +BOOL GSSettingsDlg::OnInitDialog() +{ + __super::OnInitDialog(); + + CWinApp* pApp = AfxGetApp(); + + D3DCAPS9 caps; + memset(&caps, 0, sizeof(caps)); + caps.PixelShaderVersion = D3DPS_VERSION(0, 0); + + m_modes.RemoveAll(); + + // windowed + + { + D3DDISPLAYMODE mode; + memset(&mode, 0, sizeof(mode)); + m_modes.AddTail(mode); + + int iItem = m_resolution.AddString(_T("Windowed")); + m_resolution.SetItemDataPtr(iItem, m_modes.GetTailPosition()); + m_resolution.SetCurSel(iItem); + } + + // fullscreen + + if(CComPtr d3d = Direct3DCreate9(D3D_SDK_VERSION)) + { + UINT ModeWidth = pApp->GetProfileInt(_T("Settings"), _T("ModeWidth"), 0); + UINT ModeHeight = pApp->GetProfileInt(_T("Settings"), _T("ModeHeight"), 0); + UINT ModeRefreshRate = pApp->GetProfileInt(_T("Settings"), _T("ModeRefreshRate"), 0); + + UINT nModes = d3d->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8); + + for(UINT i = 0; i < nModes; i++) + { + D3DDISPLAYMODE mode; + + if(S_OK == d3d->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &mode)) + { + CString str; + str.Format(_T("%dx%d %dHz"), mode.Width, mode.Height, mode.RefreshRate); + int iItem = m_resolution.AddString(str); + + m_modes.AddTail(mode); + m_resolution.SetItemDataPtr(iItem, m_modes.GetTailPosition()); + + if(ModeWidth == mode.Width && ModeHeight == mode.Height && ModeRefreshRate == mode.RefreshRate) + { + m_resolution.SetCurSel(iItem); + } + } + } + + d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); + } + + bool isdx10avail = GSUtil::IsDirect3D10Available(); + + CAtlArray renderers; + + for(size_t i = 0; i < countof(g_renderers); i++) + { + if(i >= 3 && i <= 5 && !isdx10avail) continue; + + renderers.Add(g_renderers[i]); + } + + GSSetting::InitComboBox(renderers.GetData(), renderers.GetCount(), m_renderer, pApp->GetProfileInt(_T("Settings"), _T("Renderer"), 0)); + GSSetting::InitComboBox(g_psversion, countof(g_psversion), m_psversion, pApp->GetProfileInt(_T("Settings"), _T("PixelShaderVersion2"), D3DPS_VERSION(2, 0)), caps.PixelShaderVersion); + GSSetting::InitComboBox(g_interlace, countof(g_interlace), m_interlace, pApp->GetProfileInt(_T("Settings"), _T("Interlace"), 0)); + GSSetting::InitComboBox(g_aspectratio, countof(g_aspectratio), m_aspectratio, pApp->GetProfileInt(_T("Settings"), _T("AspectRatio"), 1)); + + OnCbnSelchangeCombo1(); + + // + + m_filter = pApp->GetProfileInt(_T("Settings"), _T("filter"), 1); + m_tvout = pApp->GetProfileInt(_T("Settings"), _T("tvout"), FALSE); + m_nloophack = pApp->GetProfileInt(_T("Settings"), _T("nloophack"), 2); + m_vsync = !!pApp->GetProfileInt(_T("Settings"), _T("vsync"), FALSE); + m_logz = !!pApp->GetProfileInt(_T("Settings"), _T("logz"), FALSE); + m_fba = !!pApp->GetProfileInt(_T("Settings"), _T("fba"), TRUE); + + m_resx.SetRange(512, 4096); + m_resy.SetRange(512, 4096); + m_resx.SetPos(pApp->GetProfileInt(_T("Settings"), _T("resx"), 1024)); + m_resy.SetPos(pApp->GetProfileInt(_T("Settings"), _T("resy"), 1024)); + m_nativeres = !!pApp->GetProfileInt(_T("Settings"), _T("nativeres"), FALSE); + + m_resx.EnableWindow(!m_nativeres); + m_resy.EnableWindow(!m_nativeres); + m_resxedit.EnableWindow(!m_nativeres); + m_resyedit.EnableWindow(!m_nativeres); + + m_swthreads.SetRange(1, 16); + m_swthreads.SetPos(pApp->GetProfileInt(_T("Settings"), _T("swthreads"), 1)); + + // + + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GSSettingsDlg::OnOK() +{ + CWinApp* pApp = AfxGetApp(); + + UpdateData(); + + if(m_resolution.GetCurSel() >= 0) + { + D3DDISPLAYMODE& mode = m_modes.GetAt((POSITION)m_resolution.GetItemData(m_resolution.GetCurSel())); + pApp->WriteProfileInt(_T("Settings"), _T("ModeWidth"), mode.Width); + pApp->WriteProfileInt(_T("Settings"), _T("ModeHeight"), mode.Height); + pApp->WriteProfileInt(_T("Settings"), _T("ModeRefreshRate"), mode.RefreshRate); + } + + if(m_renderer.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("Settings"), _T("Renderer"), (DWORD)m_renderer.GetItemData(m_renderer.GetCurSel())); + } + + if(m_psversion.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("Settings"), _T("PixelShaderVersion2"), (DWORD)m_psversion.GetItemData(m_psversion.GetCurSel())); + } + + if(m_interlace.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("Settings"), _T("Interlace"), (DWORD)m_interlace.GetItemData(m_interlace.GetCurSel())); + } + + if(m_aspectratio.GetCurSel() >= 0) + { + pApp->WriteProfileInt(_T("Settings"), _T("AspectRatio"), (DWORD)m_aspectratio.GetItemData(m_aspectratio.GetCurSel())); + } + + pApp->WriteProfileInt(_T("Settings"), _T("filter"), m_filter); + pApp->WriteProfileInt(_T("Settings"), _T("tvout"), m_tvout); + pApp->WriteProfileInt(_T("Settings"), _T("nloophack"), m_nloophack); + pApp->WriteProfileInt(_T("Settings"), _T("vsync"), m_vsync); + pApp->WriteProfileInt(_T("Settings"), _T("logz"), m_logz); + pApp->WriteProfileInt(_T("Settings"), _T("fba"), m_fba); + + pApp->WriteProfileInt(_T("Settings"), _T("resx"), m_resx.GetPos()); + pApp->WriteProfileInt(_T("Settings"), _T("resy"), m_resy.GetPos()); + pApp->WriteProfileInt(_T("Settings"), _T("swthreads"), m_swthreads.GetPos()); + pApp->WriteProfileInt(_T("Settings"), _T("nativeres"), m_nativeres); + + __super::OnOK(); +} + +void GSSettingsDlg::OnUpdateResolution(CCmdUI* pCmdUI) +{ + UpdateData(); + + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(!m_nativeres && (i == 0 || i == 3)); +} + +void GSSettingsDlg::OnUpdateD3D9Options(CCmdUI* pCmdUI) +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(i >= 0 && i <= 2); +} + +void GSSettingsDlg::OnUpdateSWOptions(CCmdUI* pCmdUI) +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + pCmdUI->Enable(i == 1 || i == 4 || i == 6); +} + +void GSSettingsDlg::OnCbnSelchangeCombo1() +{ + int i = (int)m_renderer.GetItemData(m_renderer.GetCurSel()); + + GetDlgItem(IDC_LOGO9)->ShowWindow(i >= 0 && i <= 2 ? SW_SHOW : SW_HIDE); + GetDlgItem(IDC_LOGO10)->ShowWindow(i >= 3 && i <= 5 ? SW_SHOW : SW_HIDE); +} diff --git a/plugins/GSdx/GSSettingsDlg.h b/plugins/GSdx/GSSettingsDlg.h new file mode 100644 index 0000000000..18c2e7ddd0 --- /dev/null +++ b/plugins/GSdx/GSSettingsDlg.h @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSSetting.h" +#include "resource.h" + +class GSSettingsDlg : public CDialog +{ + DECLARE_DYNAMIC(GSSettingsDlg) + +private: + CAtlList m_modes; + +public: + GSSettingsDlg(CWnd* pParent = NULL); // standard constructor + virtual ~GSSettingsDlg(); + + static GSSetting g_renderers[]; + static GSSetting g_psversion[]; + static GSSetting g_interlace[]; + static GSSetting g_aspectratio[]; + +// Dialog Data + enum { IDD = IDD_CONFIG }; + CComboBox m_resolution; + CComboBox m_renderer; + CComboBox m_psversion; + CComboBox m_interlace; + CComboBox m_aspectratio; + BOOL m_tvout; + int m_filter; + int m_nloophack; + CSpinButtonCtrl m_resx; + CSpinButtonCtrl m_resy; + CSpinButtonCtrl m_swthreads; + BOOL m_nativeres; + CEdit m_resxedit; + CEdit m_resyedit; + CEdit m_swthreadsedit; + BOOL m_vsync; + BOOL m_logz; + BOOL m_fba; + +protected: + virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual BOOL OnInitDialog(); + virtual void OnOK(); + + DECLARE_MESSAGE_MAP() + +public: + afx_msg void OnKickIdle(); + afx_msg void OnUpdateResolution(CCmdUI* pCmdUI); + afx_msg void OnUpdateD3D9Options(CCmdUI* pCmdUI); + afx_msg void OnUpdateSWOptions(CCmdUI* pCmdUI); + afx_msg void OnCbnSelchangeCombo1(); +}; + diff --git a/plugins/GSdx/GSState.cpp b/plugins/GSdx/GSState.cpp new file mode 100644 index 0000000000..12439b8880 --- /dev/null +++ b/plugins/GSdx/GSState.cpp @@ -0,0 +1,2179 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSState.h" + +GSState::GSState(BYTE* base, bool mt, void (*irq)(), int nloophack) + : m_mt(mt) + , m_irq(irq) + , m_nloophack_org(nloophack) + , m_nloophack(nloophack == 1) + , m_crc(0) + , m_options(0) + , m_path3hack(0) + , m_q(1.0f) + , m_vprim(1) + , m_version(5) + , m_frameskip(0) + , m_vkf(NULL) +{ + m_sssize = 0; + + m_sssize += sizeof(m_version); + m_sssize += sizeof(m_env.PRIM); + m_sssize += sizeof(m_env.PRMODE); + m_sssize += sizeof(m_env.PRMODECONT); + m_sssize += sizeof(m_env.TEXCLUT); + m_sssize += sizeof(m_env.SCANMSK); + m_sssize += sizeof(m_env.TEXA); + m_sssize += sizeof(m_env.FOGCOL); + m_sssize += sizeof(m_env.DIMX); + m_sssize += sizeof(m_env.DTHE); + m_sssize += sizeof(m_env.COLCLAMP); + m_sssize += sizeof(m_env.PABE); + m_sssize += sizeof(m_env.BITBLTBUF); + m_sssize += sizeof(m_env.TRXDIR); + m_sssize += sizeof(m_env.TRXPOS); + m_sssize += sizeof(m_env.TRXREG); + m_sssize += sizeof(m_env.TRXREG2); + + for(int i = 0; i < 2; i++) + { + m_sssize += sizeof(m_env.CTXT[i].XYOFFSET); + m_sssize += sizeof(m_env.CTXT[i].TEX0); + m_sssize += sizeof(m_env.CTXT[i].TEX1); + m_sssize += sizeof(m_env.CTXT[i].TEX2); + m_sssize += sizeof(m_env.CTXT[i].CLAMP); + m_sssize += sizeof(m_env.CTXT[i].MIPTBP1); + m_sssize += sizeof(m_env.CTXT[i].MIPTBP2); + m_sssize += sizeof(m_env.CTXT[i].SCISSOR); + m_sssize += sizeof(m_env.CTXT[i].ALPHA); + m_sssize += sizeof(m_env.CTXT[i].TEST); + m_sssize += sizeof(m_env.CTXT[i].FBA); + m_sssize += sizeof(m_env.CTXT[i].FRAME); + m_sssize += sizeof(m_env.CTXT[i].ZBUF); + } + + m_sssize += sizeof(m_v.RGBAQ); + m_sssize += sizeof(m_v.ST); + m_sssize += sizeof(m_v.UV); + m_sssize += sizeof(m_v.XYZ); + m_sssize += sizeof(m_v.FOG); + + m_sssize += sizeof(m_x); + m_sssize += sizeof(m_y); + m_sssize += m_mem.m_vmsize; + m_sssize += (sizeof(m_path[0].tag) + sizeof(m_path[0].nreg)) * 3; + m_sssize += sizeof(m_q); + + ASSERT(base); + + PMODE = (GSRegPMODE*)(base + GS_PMODE); + SMODE1 = (GSRegSMODE1*)(base + GS_SMODE1); + SMODE2 = (GSRegSMODE2*)(base + GS_SMODE2); + // SRFSH = (GSRegPMODE*)(base + GS_SRFSH); + // SYNCH1 = (GSRegPMODE*)(base + GS_SYNCH1); + // SYNCH2 = (GSRegPMODE*)(base + GS_SYNCH2); + // SYNCV = (GSRegPMODE*)(base + GS_SYNCV); + DISPFB[0] = (GSRegDISPFB*)(base + GS_DISPFB1); + DISPFB[1] = (GSRegDISPFB*)(base + GS_DISPFB2); + DISPLAY[0] = (GSRegDISPLAY*)(base + GS_DISPLAY1); + DISPLAY[1] = (GSRegDISPLAY*)(base + GS_DISPLAY2); + EXTBUF = (GSRegEXTBUF*)(base + GS_EXTBUF); + EXTDATA = (GSRegEXTDATA*)(base + GS_EXTDATA); + EXTWRITE = (GSRegEXTWRITE*)(base + GS_EXTWRITE); + BGCOLOR = (GSRegBGCOLOR*)(base + GS_BGCOLOR); + CSR = (GSRegCSR*)(base + GS_CSR); + IMR = (GSRegIMR*)(base + GS_IMR); + BUSDIR = (GSRegBUSDIR*)(base + GS_BUSDIR); + SIGLBLID = (GSRegSIGLBLID*)(base + GS_SIGLBLID); + + PRIM = &m_env.PRIM; +// CSR->rREV = 0x20; + m_env.PRMODECONT.AC = 1; + + m_x = m_y = 0; + m_bytes = 0; + m_maxbytes = 1024 * 1024 * 4; + m_buff = (BYTE*)_aligned_malloc(m_maxbytes, 16); + + Reset(); + + ResetHandlers(); +} + +GSState::~GSState() +{ + _aligned_free(m_buff); +} + +void GSState::Reset() +{ + memset(&m_path[0], 0, sizeof(m_path[0]) * 3); + memset(&m_v, 0, sizeof(m_v)); + +// PRIM = &m_env.PRIM; +// m_env.PRMODECONT.AC = 1; + + m_env.Reset(); + + m_context = &m_env.CTXT[0]; + + InvalidateTextureCache(); +} + +void GSState::ResetHandlers() +{ + for(int i = 0; i < countof(m_fpGIFPackedRegHandlers); i++) + { + m_fpGIFPackedRegHandlers[i] = &GSState::GIFPackedRegHandlerNull; + } + + m_fpGIFPackedRegHandlers[GIF_REG_PRIM] = &GSState::GIFPackedRegHandlerPRIM; + m_fpGIFPackedRegHandlers[GIF_REG_RGBA] = &GSState::GIFPackedRegHandlerRGBA; + m_fpGIFPackedRegHandlers[GIF_REG_STQ] = &GSState::GIFPackedRegHandlerSTQ; + m_fpGIFPackedRegHandlers[GIF_REG_UV] = &GSState::GIFPackedRegHandlerUV; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF2] = &GSState::GIFPackedRegHandlerXYZF2; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ2] = &GSState::GIFPackedRegHandlerXYZ2; + m_fpGIFPackedRegHandlers[GIF_REG_TEX0_1] = &GSState::GIFPackedRegHandlerTEX0<0>; + m_fpGIFPackedRegHandlers[GIF_REG_TEX0_2] = &GSState::GIFPackedRegHandlerTEX0<1>; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_1] = &GSState::GIFPackedRegHandlerCLAMP<0>; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_2] = &GSState::GIFPackedRegHandlerCLAMP<1>; + m_fpGIFPackedRegHandlers[GIF_REG_FOG] = &GSState::GIFPackedRegHandlerFOG; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF3] = &GSState::GIFPackedRegHandlerXYZF3; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ3] = &GSState::GIFPackedRegHandlerXYZ3; + m_fpGIFPackedRegHandlers[GIF_REG_A_D] = &GSState::GIFPackedRegHandlerA_D; + m_fpGIFPackedRegHandlers[GIF_REG_NOP] = &GSState::GIFPackedRegHandlerNOP; + + for(int i = 0; i < countof(m_fpGIFRegHandlers); i++) + { + m_fpGIFRegHandlers[i] = &GSState::GIFRegHandlerNull; + } + + m_fpGIFRegHandlers[GIF_A_D_REG_PRIM] = &GSState::GIFRegHandlerPRIM; + m_fpGIFRegHandlers[GIF_A_D_REG_RGBAQ] = &GSState::GIFRegHandlerRGBAQ; + m_fpGIFRegHandlers[GIF_A_D_REG_ST] = &GSState::GIFRegHandlerST; + m_fpGIFRegHandlers[GIF_A_D_REG_UV] = &GSState::GIFRegHandlerUV; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF2] = &GSState::GIFRegHandlerXYZF2; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ2] = &GSState::GIFRegHandlerXYZ2; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX0_1] = &GSState::GIFRegHandlerTEX0<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX0_2] = &GSState::GIFRegHandlerTEX0<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_CLAMP_1] = &GSState::GIFRegHandlerCLAMP<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_CLAMP_2] = &GSState::GIFRegHandlerCLAMP<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_FOG] = &GSState::GIFRegHandlerFOG; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF3] = &GSState::GIFRegHandlerXYZF3; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ3] = &GSState::GIFRegHandlerXYZ3; + m_fpGIFRegHandlers[GIF_A_D_REG_NOP] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX1_1] = &GSState::GIFRegHandlerTEX1<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX1_2] = &GSState::GIFRegHandlerTEX1<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX2_1] = &GSState::GIFRegHandlerTEX2<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEX2_2] = &GSState::GIFRegHandlerTEX2<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_XYOFFSET_1] = &GSState::GIFRegHandlerXYOFFSET<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_XYOFFSET_2] = &GSState::GIFRegHandlerXYOFFSET<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODECONT] = &GSState::GIFRegHandlerPRMODECONT; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODE] = &GSState::GIFRegHandlerPRMODE; + m_fpGIFRegHandlers[GIF_A_D_REG_TEXCLUT] = &GSState::GIFRegHandlerTEXCLUT; + m_fpGIFRegHandlers[GIF_A_D_REG_SCANMSK] = &GSState::GIFRegHandlerSCANMSK; + m_fpGIFRegHandlers[GIF_A_D_REG_MIPTBP1_1] = &GSState::GIFRegHandlerMIPTBP1<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_MIPTBP1_2] = &GSState::GIFRegHandlerMIPTBP1<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_MIPTBP2_1] = &GSState::GIFRegHandlerMIPTBP2<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_MIPTBP2_2] = &GSState::GIFRegHandlerMIPTBP2<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEXA] = &GSState::GIFRegHandlerTEXA; + m_fpGIFRegHandlers[GIF_A_D_REG_FOGCOL] = &GSState::GIFRegHandlerFOGCOL; + m_fpGIFRegHandlers[GIF_A_D_REG_TEXFLUSH] = &GSState::GIFRegHandlerTEXFLUSH; + m_fpGIFRegHandlers[GIF_A_D_REG_SCISSOR_1] = &GSState::GIFRegHandlerSCISSOR<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_SCISSOR_2] = &GSState::GIFRegHandlerSCISSOR<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_ALPHA_1] = &GSState::GIFRegHandlerALPHA<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_ALPHA_2] = &GSState::GIFRegHandlerALPHA<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_DIMX] = &GSState::GIFRegHandlerDIMX; + m_fpGIFRegHandlers[GIF_A_D_REG_DTHE] = &GSState::GIFRegHandlerDTHE; + m_fpGIFRegHandlers[GIF_A_D_REG_COLCLAMP] = &GSState::GIFRegHandlerCOLCLAMP; + m_fpGIFRegHandlers[GIF_A_D_REG_TEST_1] = &GSState::GIFRegHandlerTEST<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_TEST_2] = &GSState::GIFRegHandlerTEST<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_PABE] = &GSState::GIFRegHandlerPABE; + m_fpGIFRegHandlers[GIF_A_D_REG_FBA_1] = &GSState::GIFRegHandlerFBA<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_FBA_2] = &GSState::GIFRegHandlerFBA<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_FRAME_1] = &GSState::GIFRegHandlerFRAME<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_FRAME_2] = &GSState::GIFRegHandlerFRAME<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_ZBUF_1] = &GSState::GIFRegHandlerZBUF<0>; + m_fpGIFRegHandlers[GIF_A_D_REG_ZBUF_2] = &GSState::GIFRegHandlerZBUF<1>; + m_fpGIFRegHandlers[GIF_A_D_REG_BITBLTBUF] = &GSState::GIFRegHandlerBITBLTBUF; + m_fpGIFRegHandlers[GIF_A_D_REG_TRXPOS] = &GSState::GIFRegHandlerTRXPOS; + m_fpGIFRegHandlers[GIF_A_D_REG_TRXREG] = &GSState::GIFRegHandlerTRXREG; + m_fpGIFRegHandlers[GIF_A_D_REG_TRXDIR] = &GSState::GIFRegHandlerTRXDIR; + m_fpGIFRegHandlers[GIF_A_D_REG_HWREG] = &GSState::GIFRegHandlerHWREG; + m_fpGIFRegHandlers[GIF_A_D_REG_SIGNAL] = &GSState::GIFRegHandlerSIGNAL; + m_fpGIFRegHandlers[GIF_A_D_REG_FINISH] = &GSState::GIFRegHandlerFINISH; + m_fpGIFRegHandlers[GIF_A_D_REG_LABEL] = &GSState::GIFRegHandlerLABEL; +} + +CPoint GSState::GetDisplayPos(int i) +{ + ASSERT(i >= 0 && i < 2); + + CPoint p; + + p.x = DISPLAY[i]->DX / (DISPLAY[i]->MAGH + 1); + p.y = DISPLAY[i]->DY / (DISPLAY[i]->MAGV + 1); + + return p; +} + +CSize GSState::GetDisplaySize(int i) +{ + ASSERT(i >= 0 && i < 2); + + CSize s; + + s.cx = (DISPLAY[i]->DW + 1) / (DISPLAY[i]->MAGH + 1); + s.cy = (DISPLAY[i]->DH + 1) / (DISPLAY[i]->MAGV + 1); + + return s; +} + +CRect GSState::GetDisplayRect(int i) +{ + return CRect(GetDisplayPos(i), GetDisplaySize(i)); +} + +CSize GSState::GetDisplayPos() +{ + return GetDisplayPos(IsEnabled(1) ? 1 : 0); +} + +CSize GSState::GetDisplaySize() +{ + return GetDisplaySize(IsEnabled(1) ? 1 : 0); +} + +CRect GSState::GetDisplayRect() +{ + return GetDisplayRect(IsEnabled(1) ? 1 : 0); +} + +CPoint GSState::GetFramePos(int i) +{ + ASSERT(i >= 0 && i < 2); + + return CPoint(DISPFB[i]->DBX, DISPFB[i]->DBY); +} + +CSize GSState::GetFrameSize(int i) +{ + CSize s = GetDisplaySize(i); + + if(SMODE2->INT && SMODE2->FFMD && s.cy > 1) s.cy >>= 1; + + return s; +} + +CRect GSState::GetFrameRect(int i) +{ + return CRect(GetFramePos(i), GetFrameSize(i)); +} + +CSize GSState::GetFramePos() +{ + return GetFramePos(IsEnabled(1) ? 1 : 0); +} + +CSize GSState::GetFrameSize() +{ + return GetFrameSize(IsEnabled(1) ? 1 : 0); +} + +CRect GSState::GetFrameRect() +{ + return GetFrameRect(IsEnabled(1) ? 1 : 0); +} + +CSize GSState::GetDeviceSize(int i) +{ + // TODO: other params of SMODE1 should affect the true device display size + + // TODO2: pal games at 60Hz + + CSize s = GetDisplaySize(i); + + if(s.cy == 2*416 || s.cy == 2*448 || s.cy == 2*512) + { + s.cy /= 2; + } + else + { + s.cy = (SMODE1->CMOD & 1) ? 512 : 448; + } + + return s; + +} + +CSize GSState::GetDeviceSize() +{ + return GetDeviceSize(IsEnabled(1) ? 1 : 0); +} + +bool GSState::IsEnabled(int i) +{ + ASSERT(i >= 0 && i < 2); + + if(i == 0 && PMODE->EN1) + { + return DISPLAY[0]->DW || DISPLAY[0]->DH; + } + else if(i == 1 && PMODE->EN2) + { + return DISPLAY[1]->DW || DISPLAY[1]->DH; + } + + return false; +} + +int GSState::GetFPS() +{ + return ((SMODE1->CMOD & 1) ? 50 : 60) / (SMODE2->INT ? 1 : 2); +} + +// GIFPackedRegHandler* + +void GSState::GIFPackedRegHandlerNull(GIFPackedReg* r) +{ + // ASSERT(0); +} + +void GSState::GIFPackedRegHandlerPRIM(GIFPackedReg* r) +{ + // ASSERT(r->r.PRIM.PRIM < 7); + + GIFRegHandlerPRIM(&r->r); +} + +void GSState::GIFPackedRegHandlerRGBA(GIFPackedReg* r) +{ + #if _M_SSE >= 0x301 + + GSVector4i mask = GSVector4i::load(0x0c080400); + GSVector4i v = GSVector4i::load(r).shuffle8(mask); + m_v.RGBAQ.ai32[0] = (UINT32)GSVector4i::store(v); + + #elif _M_SSE >= 0x200 + + GSVector4i v = GSVector4i::load(r) & GSVector4i::x000000ff(); + m_v.RGBAQ.ai32[0] = v.rgba32(); + + #else + + m_v.RGBAQ.R = r->RGBA.R; + m_v.RGBAQ.G = r->RGBA.G; + m_v.RGBAQ.B = r->RGBA.B; + m_v.RGBAQ.A = r->RGBA.A; + + #endif + + m_v.RGBAQ.Q = m_q; +} + +void GSState::GIFPackedRegHandlerSTQ(GIFPackedReg* r) +{ + #if defined(_M_AMD64) + + m_v.ST.i64 = r->ai64[0]; + + #elif _M_SSE >= 0x200 + + GSVector4i v = GSVector4i::loadl(r); + GSVector4i::storel(&m_v.ST.i64, v); + + #else + + m_v.ST.S = r->STQ.S; + m_v.ST.T = r->STQ.T; + + #endif + + m_q = r->STQ.Q; +} + +void GSState::GIFPackedRegHandlerUV(GIFPackedReg* r) +{ + #if _M_SSE >= 0x200 + + GSVector4i v = GSVector4i::loadl(r) & GSVector4i::x00003fff(); + m_v.UV.ai32[0] = (UINT32)GSVector4i::store(v.ps32(v)); + + #else + + m_v.UV.U = r->UV.U; + m_v.UV.V = r->UV.V; + + #endif +} + +void GSState::GIFPackedRegHandlerXYZF2(GIFPackedReg* r) +{ + m_v.XYZ.X = r->XYZF2.X; + m_v.XYZ.Y = r->XYZF2.Y; + m_v.XYZ.Z = r->XYZF2.Z; + m_v.FOG.F = r->XYZF2.F; + + VertexKick(r->XYZF2.ADC); +} + +void GSState::GIFPackedRegHandlerXYZ2(GIFPackedReg* r) +{ + m_v.XYZ.X = r->XYZ2.X; + m_v.XYZ.Y = r->XYZ2.Y; + m_v.XYZ.Z = r->XYZ2.Z; + + VertexKick(r->XYZ2.ADC); +} + +template void GSState::GIFPackedRegHandlerTEX0(GIFPackedReg* r) +{ + GIFRegHandlerTEX0((GIFReg*)&r->ai64[0]); +} + +template void GSState::GIFPackedRegHandlerCLAMP(GIFPackedReg* r) +{ + GIFRegHandlerCLAMP((GIFReg*)&r->ai64[0]); +} + +void GSState::GIFPackedRegHandlerFOG(GIFPackedReg* r) +{ + m_v.FOG.F = r->FOG.F; +} + +void GSState::GIFPackedRegHandlerXYZF3(GIFPackedReg* r) +{ + GIFRegHandlerXYZF3((GIFReg*)&r->ai64[0]); +} + +void GSState::GIFPackedRegHandlerXYZ3(GIFPackedReg* r) +{ + GIFRegHandlerXYZ3((GIFReg*)&r->ai64[0]); +} + +void GSState::GIFPackedRegHandlerA_D(GIFPackedReg* r) +{ + (this->*m_fpGIFRegHandlers[(BYTE)r->A_D.ADDR])(&r->r); +} + +void GSState::GIFPackedRegHandlerA_D(GIFPackedReg* r, int size) +{ + for(int i = 0; i < size; i++) + { + (this->*m_fpGIFRegHandlers[(BYTE)r[i].A_D.ADDR])(&r[i].r); + } +} + +void GSState::GIFPackedRegHandlerNOP(GIFPackedReg* r) +{ +} + +// GIFRegHandler* + +void GSState::GIFRegHandlerNull(GIFReg* r) +{ + // ASSERT(0); +} + +void GSState::GIFRegHandlerPRIM(GIFReg* r) +{ + // ASSERT(r->PRIM.PRIM < 7); + + if(GSUtil::GetPrimClass(m_env.PRIM.PRIM) == GSUtil::GetPrimClass(r->PRIM.PRIM)) + { + if(((m_env.PRIM.i64 ^ r->PRIM.i64) & ~7) != 0) + { + Flush(); + } + } + else + { + Flush(); + } + + m_env.PRIM = (GSVector4i)r->PRIM; + m_env.PRMODE._PRIM = r->PRIM.PRIM; + + m_context = &m_env.CTXT[PRIM->CTXT]; + + UpdateVertexKick(); + + ResetPrim(); +} + +void GSState::GIFRegHandlerRGBAQ(GIFReg* r) +{ + m_v.RGBAQ = (GSVector4i)r->RGBAQ; +} + +void GSState::GIFRegHandlerST(GIFReg* r) +{ + m_v.ST = (GSVector4i)r->ST; +} + +void GSState::GIFRegHandlerUV(GIFReg* r) +{ + m_v.UV.ai32[0] = r->UV.ai32[0] & 0x3fff3fff; +} + +void GSState::GIFRegHandlerXYZF2(GIFReg* r) +{ +/* + m_v.XYZ.X = r->XYZF.X; + m_v.XYZ.Y = r->XYZF.Y; + m_v.XYZ.Z = r->XYZF.Z; + m_v.FOG.F = r->XYZF.F; +*/ + m_v.XYZ.ai32[0] = r->XYZF.ai32[0]; + m_v.XYZ.ai32[1] = r->XYZF.ai32[1] & 0x00ffffff; + m_v.FOG.ai32[1] = r->XYZF.ai32[1] & 0xff000000; + + VertexKick(false); +} + +void GSState::GIFRegHandlerXYZ2(GIFReg* r) +{ + m_v.XYZ = (GSVector4i)r->XYZ; + + VertexKick(false); +} + +template void GSState::GIFRegHandlerTEX0(GIFReg* r) +{ + // even if TEX0 did not change, a new palette may have been uploaded and will overwrite the currently queued for drawing + + bool wt = m_mem.m_clut.WriteTest(r->TEX0, m_env.TEXCLUT); + + if(wt || PRIM->CTXT == i && !(m_env.CTXT[i].TEX0 == (GSVector4i)r->TEX0).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].TEX0 = (GSVector4i)r->TEX0; + + if(m_env.CTXT[i].TEX0.TW > 10) m_env.CTXT[i].TEX0.TW = 10; + if(m_env.CTXT[i].TEX0.TH > 10) m_env.CTXT[i].TEX0.TH = 10; + + m_env.CTXT[i].TEX0.CPSM &= 0xa; // 1010b + + if((m_env.CTXT[i].TEX0.TBW & 1) && (m_env.CTXT[i].TEX0.PSM == PSM_PSMT8 || m_env.CTXT[i].TEX0.PSM == PSM_PSMT4)) + { + m_env.CTXT[i].TEX0.TBW &= ~1; // GS User 2.6 + } + + if(wt) + { + m_mem.m_clut.Write(m_env.CTXT[i].TEX0, m_env.TEXCLUT); + } +} + +template void GSState::GIFRegHandlerCLAMP(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].CLAMP == (GSVector4i)r->CLAMP).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].CLAMP = (GSVector4i)r->CLAMP; +} + +void GSState::GIFRegHandlerFOG(GIFReg* r) +{ + m_v.FOG = (GSVector4i)r->FOG; +} + +void GSState::GIFRegHandlerXYZF3(GIFReg* r) +{ +/* + m_v.XYZ.X = r->XYZF.X; + m_v.XYZ.Y = r->XYZF.Y; + m_v.XYZ.Z = r->XYZF.Z; + m_v.FOG.F = r->XYZF.F; +*/ + m_v.XYZ.ai32[0] = r->XYZF.ai32[0]; + m_v.XYZ.ai32[1] = r->XYZF.ai32[1] & 0x00ffffff; + m_v.FOG.ai32[1] = r->XYZF.ai32[1] & 0xff000000; + + VertexKick(true); +} + +void GSState::GIFRegHandlerXYZ3(GIFReg* r) +{ + m_v.XYZ = (GSVector4i)r->XYZ; + + VertexKick(true); +} + +void GSState::GIFRegHandlerNOP(GIFReg* r) +{ +} + +template void GSState::GIFRegHandlerTEX1(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].TEX1 == (GSVector4i)r->TEX1).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].TEX1 = (GSVector4i)r->TEX1; +} + +template void GSState::GIFRegHandlerTEX2(GIFReg* r) +{ + // m_env.CTXT[i].TEX2 = r->TEX2; // not used + + UINT64 mask = 0xFFFFFFE003F00000ui64; // TEX2 bits + + r->i64 = (r->i64 & mask) | (m_env.CTXT[i].TEX0.i64 & ~mask); + + GIFRegHandlerTEX0(r); +} + +template void GSState::GIFRegHandlerXYOFFSET(GIFReg* r) +{ + GSVector4i o = (GSVector4i)r->XYOFFSET & GSVector4i::x0000ffff(); + + if(!(m_env.CTXT[i].XYOFFSET == o).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].XYOFFSET = o; + + m_env.CTXT[i].UpdateScissor(); +} + +void GSState::GIFRegHandlerPRMODECONT(GIFReg* r) +{ + if(!(m_env.PRMODECONT == (GSVector4i)r->PRMODECONT).alltrue()) + { + Flush(); + } + + m_env.PRMODECONT.AC = r->PRMODECONT.AC; + + PRIM = m_env.PRMODECONT.AC ? &m_env.PRIM : (GIFRegPRIM*)&m_env.PRMODE; + + if(PRIM->PRIM == 7) TRACE(_T("Invalid PRMODECONT/PRIM\n")); + + m_context = &m_env.CTXT[PRIM->CTXT]; + + UpdateVertexKick(); +} + +void GSState::GIFRegHandlerPRMODE(GIFReg* r) +{ + if(!m_env.PRMODECONT.AC) + { + Flush(); + } + + UINT32 _PRIM = m_env.PRMODE._PRIM; + m_env.PRMODE = (GSVector4i)r->PRMODE; + m_env.PRMODE._PRIM = _PRIM; + + m_context = &m_env.CTXT[PRIM->CTXT]; + + UpdateVertexKick(); +} + +void GSState::GIFRegHandlerTEXCLUT(GIFReg* r) +{ + if(!(m_env.TEXCLUT == (GSVector4i)r->TEXCLUT).alltrue()) + { + Flush(); + } + + m_env.TEXCLUT = (GSVector4i)r->TEXCLUT; +} + +void GSState::GIFRegHandlerSCANMSK(GIFReg* r) +{ + if(!(m_env.SCANMSK == (GSVector4i)r->SCANMSK).alltrue()) + { + Flush(); + } + + m_env.SCANMSK = (GSVector4i)r->SCANMSK; +} + +template void GSState::GIFRegHandlerMIPTBP1(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].MIPTBP1 == (GSVector4i)r->MIPTBP1).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].MIPTBP1 = (GSVector4i)r->MIPTBP1; +} + +template void GSState::GIFRegHandlerMIPTBP2(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].MIPTBP2 == (GSVector4i)r->MIPTBP2).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].MIPTBP2 = (GSVector4i)r->MIPTBP2; +} + +void GSState::GIFRegHandlerTEXA(GIFReg* r) +{ + if(!(m_env.TEXA == (GSVector4i)r->TEXA).alltrue()) + { + Flush(); + } + + m_env.TEXA = (GSVector4i)r->TEXA; +} + +void GSState::GIFRegHandlerFOGCOL(GIFReg* r) +{ + if(!(m_env.FOGCOL == (GSVector4i)r->FOGCOL).alltrue()) + { + Flush(); + } + + m_env.FOGCOL = (GSVector4i)r->FOGCOL; +} + +void GSState::GIFRegHandlerTEXFLUSH(GIFReg* r) +{ + // TRACE(_T("TEXFLUSH\n")); + + // InvalidateTextureCache(); +} + +template void GSState::GIFRegHandlerSCISSOR(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].SCISSOR == (GSVector4i)r->SCISSOR).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].SCISSOR = (GSVector4i)r->SCISSOR; + + m_env.CTXT[i].UpdateScissor(); +} + +template void GSState::GIFRegHandlerALPHA(GIFReg* r) +{ + ASSERT(r->ALPHA.A != 3); + ASSERT(r->ALPHA.B != 3); + ASSERT(r->ALPHA.C != 3); + ASSERT(r->ALPHA.D != 3); + + if(PRIM->CTXT == i && !(m_env.CTXT[i].ALPHA == (GSVector4i)r->ALPHA).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].ALPHA = (GSVector4i)r->ALPHA; + + // A/B/C/D == 3? => 2 + + m_env.CTXT[i].ALPHA.ai32[0] = ((~m_env.CTXT[i].ALPHA.ai32[0] >> 1) | 0xAA) & m_env.CTXT[i].ALPHA.ai32[0]; +} + +void GSState::GIFRegHandlerDIMX(GIFReg* r) +{ + if(!(m_env.DIMX == (GSVector4i)r->DIMX).alltrue()) + { + Flush(); + } + + m_env.DIMX = (GSVector4i)r->DIMX; +} + +void GSState::GIFRegHandlerDTHE(GIFReg* r) +{ + if(!(m_env.DTHE == (GSVector4i)r->DTHE).alltrue()) + { + Flush(); + } + + m_env.DTHE = (GSVector4i)r->DTHE; +} + +void GSState::GIFRegHandlerCOLCLAMP(GIFReg* r) +{ + if(!(m_env.COLCLAMP == (GSVector4i)r->COLCLAMP).alltrue()) + { + Flush(); + } + + m_env.COLCLAMP = (GSVector4i)r->COLCLAMP; +} + +template void GSState::GIFRegHandlerTEST(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].TEST == (GSVector4i)r->TEST).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].TEST = (GSVector4i)r->TEST; +} + +void GSState::GIFRegHandlerPABE(GIFReg* r) +{ + if(!(m_env.PABE == (GSVector4i)r->PABE).alltrue()) + { + Flush(); + } + + m_env.PABE = (GSVector4i)r->PABE; +} + +template void GSState::GIFRegHandlerFBA(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].FBA == (GSVector4i)r->FBA).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].FBA = (GSVector4i)r->FBA; +} + +template void GSState::GIFRegHandlerFRAME(GIFReg* r) +{ + if(PRIM->CTXT == i && !(m_env.CTXT[i].FRAME == (GSVector4i)r->FRAME).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].FRAME = (GSVector4i)r->FRAME; +} + +template void GSState::GIFRegHandlerZBUF(GIFReg* r) +{ + if(r->ZBUF.ai32[0] == 0) + { + // during startup all regs are cleared to 0 (by the bios or something), so we mask z until this register becomes valid + + r->ZBUF.ZMSK = 1; + } + + r->ZBUF.PSM |= 0x30; + + if(PRIM->CTXT == i && !(m_env.CTXT[i].ZBUF == (GSVector4i)r->ZBUF).alltrue()) + { + Flush(); + } + + m_env.CTXT[i].ZBUF = (GSVector4i)r->ZBUF; + + if(m_env.CTXT[i].ZBUF.PSM != PSM_PSMZ32 + && m_env.CTXT[i].ZBUF.PSM != PSM_PSMZ24 + && m_env.CTXT[i].ZBUF.PSM != PSM_PSMZ16 + && m_env.CTXT[i].ZBUF.PSM != PSM_PSMZ16S) + { + m_env.CTXT[i].ZBUF.PSM = PSM_PSMZ32; + } +} + +void GSState::GIFRegHandlerBITBLTBUF(GIFReg* r) +{ + if(!(m_env.BITBLTBUF == (GSVector4i)r->BITBLTBUF).alltrue()) + { + FlushWrite(); + } + + m_env.BITBLTBUF = (GSVector4i)r->BITBLTBUF; + + if((m_env.BITBLTBUF.SBW & 1) && (m_env.BITBLTBUF.SPSM == PSM_PSMT8 || m_env.BITBLTBUF.SPSM == PSM_PSMT4)) + { + m_env.BITBLTBUF.SBW &= ~1; + } + + if((m_env.BITBLTBUF.DBW & 1) && (m_env.BITBLTBUF.DPSM == PSM_PSMT8 || m_env.BITBLTBUF.DPSM == PSM_PSMT4)) + { + m_env.BITBLTBUF.DBW &= ~1; // namcoXcapcom: 5, 11, refered to as 4, 10 in TEX0.TBW later + } +} + +void GSState::GIFRegHandlerTRXPOS(GIFReg* r) +{ + if(!(m_env.TRXPOS == (GSVector4i)r->TRXPOS).alltrue()) + { + FlushWrite(); + } + + m_env.TRXPOS = (GSVector4i)r->TRXPOS; +} + +void GSState::GIFRegHandlerTRXREG(GIFReg* r) +{ + if(!(m_env.TRXREG == (GSVector4i)r->TRXREG).alltrue() || !(m_env.TRXREG2 == (GSVector4i)r->TRXREG).alltrue()) + { + FlushWrite(); + } + + m_env.TRXREG = (GSVector4i)r->TRXREG; + m_env.TRXREG2 = (GSVector4i)r->TRXREG; +} + +void GSState::GIFRegHandlerTRXDIR(GIFReg* r) +{ + Flush(); + + m_env.TRXDIR = (GSVector4i)r->TRXDIR; + + switch(m_env.TRXDIR.XDIR) + { + case 0: // host -> local + m_x = m_env.TRXPOS.DSAX; + m_y = m_env.TRXPOS.DSAY; + m_env.TRXREG.RRW = m_x + m_env.TRXREG2.RRW; + m_env.TRXREG.RRH = m_y + m_env.TRXREG2.RRH; + break; + case 1: // local -> host + m_x = m_env.TRXPOS.SSAX; + m_y = m_env.TRXPOS.SSAY; + m_env.TRXREG.RRW = m_x + m_env.TRXREG2.RRW; + m_env.TRXREG.RRH = m_y + m_env.TRXREG2.RRH; + break; + case 2: // local -> local + Move(); + break; + case 3: + ASSERT(0); + break; + } +} + +void GSState::GIFRegHandlerHWREG(GIFReg* r) +{ + ASSERT(m_env.TRXDIR.XDIR == 0); // host => local + + Write((BYTE*)r, 8); // hunting ground +} + +void GSState::GIFRegHandlerSIGNAL(GIFReg* r) +{ + if(m_mt) return; + + SIGLBLID->SIGID = (SIGLBLID->SIGID & ~r->SIGNAL.IDMSK) | (r->SIGNAL.ID & r->SIGNAL.IDMSK); + + if(CSR->wSIGNAL) CSR->rSIGNAL = 1; + if(!IMR->SIGMSK && m_irq) m_irq(); +} + +void GSState::GIFRegHandlerFINISH(GIFReg* r) +{ + if(m_mt) return; + + if(CSR->wFINISH) CSR->rFINISH = 1; + if(!IMR->FINISHMSK && m_irq) m_irq(); +} + +void GSState::GIFRegHandlerLABEL(GIFReg* r) +{ + if(m_mt) return; + + SIGLBLID->LBLID = (SIGLBLID->LBLID & ~r->LABEL.IDMSK) | (r->LABEL.ID & r->LABEL.IDMSK); +} + +// + +void GSState::Flush() +{ + FlushWrite(); + + FlushPrim(); +} + +void GSState::FlushWrite() +{ + FlushWrite(m_buff, m_bytes); + + m_bytes = 0; +} + +void GSState::FlushWrite(BYTE* mem, int len) +{ + if(len > 0) + { +/* +CSize bs = GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].bs; + +if((m_x & (bs.cx - 1)) || (m_env.TRXREG.RRW & (bs.cx - 1)) +|| (m_y & (bs.cy - 1)) || (m_env.TRXREG.RRH & (bs.cy - 1)) +|| m_x != m_env.TRXPOS.DSAX) +{ + printf("*** [%d]: %d %d, %d %d %d %d\n", m_env.BITBLTBUF.DPSM, m_env.TRXPOS.DSAX, m_env.TRXPOS.DSAY, m_x, m_y, m_env.TRXREG.RRW, m_env.TRXREG.RRH); +} + +if((len % ((m_env.TRXREG.RRW - m_x) * GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].trbpp / 8)) != 0) +{ + printf("*** [%d]: %d %d\n", m_env.BITBLTBUF.DPSM, len, ((m_env.TRXREG.RRW - m_x) * GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].trbpp / 8)); +} +*/ + int y = m_y; + + GSLocalMemory::writeImage wi = GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].wi; + + (m_mem.*wi)(m_x, m_y, mem, len, m_env.BITBLTBUF, m_env.TRXPOS, m_env.TRXREG); + + m_perfmon.Put(GSPerfMon::Swizzle, len); + + //ASSERT(m_env.TRXREG.RRH >= m_y - y); + + CRect r; + + r.left = m_env.TRXPOS.DSAX; + r.top = y; + r.right = m_env.TRXREG.RRW; + r.bottom = min(m_x == m_env.TRXPOS.DSAX ? m_y : m_y + 1, m_env.TRXREG.RRH); + + InvalidateVideoMem(m_env.BITBLTBUF, r); +/* + static int n = 0; + CString str; + str.Format(_T("c:\\temp1\\[%04d]_%05x_%d_%d_%d_%d_%d_%d.bmp"), + n++, (int)m_env.BITBLTBUF.DBP, (int)m_env.BITBLTBUF.DBW, (int)m_env.BITBLTBUF.DPSM, + r.left, r.top, r.right, r.bottom); + m_mem.SaveBMP(str, m_env.BITBLTBUF.DBP, m_env.BITBLTBUF.DBW, m_env.BITBLTBUF.DPSM, r.Width(), r.Height()); +*/ + } +} + +// + +void GSState::Write(BYTE* mem, int len) +{ +/* + TRACE(_T("Write len=%d DBP=%05x DBW=%d DPSM=%d DSAX=%d DSAY=%d RRW=%d RRH=%d\n"), + len, (int)m_env.BITBLTBUF.DBP, (int)m_env.BITBLTBUF.DBW, (int)m_env.BITBLTBUF.DPSM, + (int)m_env.TRXPOS.DSAX, (int)m_env.TRXPOS.DSAY, + (int)m_env.TRXREG.RRW, (int)m_env.TRXREG.RRH); +*/ + if(len == 0) return; + + if(m_y >= m_env.TRXREG.RRH) return; // TODO: handle overflow during writing data too (just chop len below somewhere) + + // TODO: hmmmm + + if(PRIM->TME && (m_env.BITBLTBUF.DBP == m_context->TEX0.TBP0 || m_env.BITBLTBUF.DBP == m_context->TEX0.CBP)) + { + FlushPrim(); + } + + int bpp = GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].trbpp; + + int pitch = (m_env.TRXREG.RRW - m_env.TRXPOS.DSAX) * bpp >> 3; + + if(pitch <= 0) {ASSERT(0); return;} + + int height = len / pitch; + + if(height > m_env.TRXREG.RRH - m_env.TRXPOS.DSAY) + { + height = m_env.TRXREG.RRH - m_env.TRXPOS.DSAY; + + len = height * pitch; + } + + if(m_bytes > 0 || height < m_env.TRXREG.RRH - m_env.TRXPOS.DSAY) + { + ASSERT(len <= m_maxbytes); // more than 4mb into a 4mb local mem doesn't make sense + + len = min(m_maxbytes, len); + + if(m_bytes + len > m_maxbytes) + { + FlushWrite(); + } + + memcpy(&m_buff[m_bytes], mem, len); + + m_bytes += len; + } + else + { + FlushWrite(mem, len); + } + + m_mem.m_clut.Invalidate(); +} + +void GSState::Read(BYTE* mem, int len) +{ + /* + TRACE(_T("Read len=%d SBP=%05x SBW=%d SPSM=%d SSAX=%d SSAY=%d RRW=%d RRH=%d\n"), + len, (int)m_env.BITBLTBUF.SBP, (int)m_env.BITBLTBUF.SBW, (int)m_env.BITBLTBUF.SPSM, + (int)m_env.TRXPOS.SSAX, (int)m_env.TRXPOS.SSAY, + (int)m_env.TRXREG.RRW, (int)m_env.TRXREG.RRH); + */ + + if(m_y >= (int)m_env.TRXREG.RRH) {ASSERT(0); return;} + + if(m_x == m_env.TRXPOS.SSAX && m_y == m_env.TRXPOS.SSAY) + { + CRect r(m_env.TRXPOS.SSAX, m_env.TRXPOS.SSAY, m_env.TRXREG.RRW, m_env.TRXREG.RRH); + + InvalidateLocalMem(m_env.BITBLTBUF, r); + } + + // TODO + + m_mem.ReadImageX(m_x, m_y, mem, len, m_env.BITBLTBUF, m_env.TRXPOS, m_env.TRXREG); +} + +void GSState::Move() +{ + // ffxii uses this to move the top/bottom of the scrolling menus offscreen and then blends them back over the text to create a shading effect + // guitar hero copies the far end of the board to do a similar blend too + + GSLocalMemory::readPixel rp = GSLocalMemory::m_psm[m_env.BITBLTBUF.SPSM].rp; + GSLocalMemory::writePixel wp = GSLocalMemory::m_psm[m_env.BITBLTBUF.DPSM].wp; + + int sx = m_env.TRXPOS.SSAX; + int dx = m_env.TRXPOS.DSAX; + int sy = m_env.TRXPOS.SSAY; + int dy = m_env.TRXPOS.DSAY; + int w = m_env.TRXREG.RRW; + int h = m_env.TRXREG.RRH; + int xinc = 1; + int yinc = 1; + + if(sx < dx) sx += w-1, dx += w-1, xinc = -1; + if(sy < dy) sy += h-1, dy += h-1, yinc = -1; + + InvalidateLocalMem(m_env.BITBLTBUF, CRect(CPoint(sx, sy), CSize(w, h))); + InvalidateVideoMem(m_env.BITBLTBUF, CRect(CPoint(dx, dy), CSize(w, h))); + + // TODO: use rowOffset + + for(int y = 0; y < h; y++, sy += yinc, dy += yinc, sx -= xinc*w, dx -= xinc*w) + for(int x = 0; x < w; x++, sx += xinc, dx += xinc) + (m_mem.*wp)(dx, dy, (m_mem.*rp)(sx, sy, m_env.BITBLTBUF.SBP, m_env.BITBLTBUF.SBW), m_env.BITBLTBUF.DBP, m_env.BITBLTBUF.DBW); +} + +void GSState::SoftReset(BYTE mask) +{ + if(mask & 1) memset(&m_path[0], 0, sizeof(GIFPath)); + if(mask & 2) memset(&m_path[1], 0, sizeof(GIFPath)); + if(mask & 4) memset(&m_path[2], 0, sizeof(GIFPath)); + + m_env.TRXDIR.XDIR = 3; //-1 ; set it to invalid value + + m_q = 1; +} + +void GSState::ReadFIFO(BYTE* mem, int size) +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + Flush(); + + size *= 16; + + Read(mem, size); + + if(m_dump) + { + m_dump.ReadFIFO(size); + } +} + +template void GSState::Transfer<0>(BYTE* mem, UINT32 size); +template void GSState::Transfer<1>(BYTE* mem, UINT32 size); +template void GSState::Transfer<2>(BYTE* mem, UINT32 size); + +template void GSState::Transfer(BYTE* mem, UINT32 size) +{ + GSPerfMonAutoTimer pmat(m_perfmon); + + BYTE* start = mem; + + GIFPath& path = m_path[index]; + + while(size > 0) + { + bool eop = false; + + if(path.tag.NLOOP == 0) + { + path.SetTag(mem); + + mem += sizeof(GIFTag); + size--; + + m_q = 1.0f; + + if(index == 2 && path.tag.EOP) + { + m_path3hack = 1; + } + + if(path.tag.PRE) + { + ASSERT(path.tag.FLG != GIF_FLG_IMAGE); // kingdom hearts, ffxii, tales of abyss, berserk + + if((path.tag.FLG & 2) == 0) + { + GIFReg r; + r.i64 = path.tag.PRIM; + (this->*m_fpGIFRegHandlers[GIF_A_D_REG_PRIM])(&r); + } + } + + if(path.tag.EOP) + { + eop = true; + } + else if(path.tag.NLOOP == 0) + { + if(index == 0 && m_nloophack) + { + continue; + } + + eop = true; + } + } + + if(path.tag.NLOOP > 0) + { + switch(path.tag.FLG) + { + case GIF_FLG_PACKED: + + // first try a shortcut for a very common case + + if(path.nreg == 0 && path.tag.NREG == 1 && size >= path.tag.NLOOP && path.GetReg() == GIF_REG_A_D) + { + int n = path.tag.NLOOP; + + GIFPackedRegHandlerA_D((GIFPackedReg*)mem, n); + + mem += n * sizeof(GIFPackedReg); + size -= n; + + path.tag.NLOOP = 0; + } + else + { + while(size > 0) + { + (this->*m_fpGIFPackedRegHandlers[path.GetReg()])((GIFPackedReg*)mem); + + size--; + mem += sizeof(GIFPackedReg); + + if((++path.nreg & 0xf) == path.tag.NREG) + { + path.nreg = 0; + path.tag.NLOOP--; + + if(path.tag.NLOOP == 0) + { + break; + } + } + } + } + + break; + + case GIF_FLG_REGLIST: + + size *= 2; + + while(size > 0) + { + (this->*m_fpGIFRegHandlers[path.GetReg()])((GIFReg*)mem); + + size--; + mem += sizeof(GIFReg); + + if((++path.nreg & 0xf) == path.tag.NREG) + { + path.nreg = 0; + path.tag.NLOOP--; + + if(path.tag.NLOOP == 0) + { + break; + } + } + } + + if(size & 1) mem += sizeof(GIFReg); + + size /= 2; + + break; + + case GIF_FLG_IMAGE2: // hmmm + + ASSERT(0); + + path.tag.NLOOP = 0; + + break; + + case GIF_FLG_IMAGE: + { + int len = (int)min(size, path.tag.NLOOP); + + //ASSERT(!(len&3)); + + switch(m_env.TRXDIR.XDIR) + { + case 0: + Write(mem, len * 16); + break; + case 1: + Read(mem, len * 16); + break; + case 2: + Move(); + break; + case 3: + ASSERT(0); + break; + default: + __assume(0); + } + + mem += len * 16; + path.tag.NLOOP -= len; + size -= len; + } + + break; + + default: + __assume(0); + } + } + + if(eop && ((int)size <= 0 || index == 0)) + { + break; + } + } + + // FIXME: dq8, pcsx2 error probably + + if(index == 0) + { + if(!path.tag.EOP && path.tag.NLOOP > 0) + { + path.tag.NLOOP = 0; + + TRACE(_T("path1 hack\n")); + } + } + + if(m_dump && mem > start) + { + m_dump.Transfer(index, start, mem - start); + } +} + +template static void WriteState(BYTE*& dst, T* src, size_t len = sizeof(T)) +{ + memcpy(dst, src, len); + dst += len; +} + +template static void ReadState(T* dst, BYTE*& src, size_t len = sizeof(T)) +{ + memcpy(dst, src, len); + src += len; +} + +int GSState::Freeze(GSFreezeData* fd, bool sizeonly) +{ + if(sizeonly) + { + fd->size = m_sssize; + return 0; + } + + if(!fd->data || fd->size < m_sssize) + { + return -1; + } + + Flush(); + + BYTE* data = fd->data; + + WriteState(data, &m_version); + WriteState(data, &m_env.PRIM); + WriteState(data, &m_env.PRMODE); + WriteState(data, &m_env.PRMODECONT); + WriteState(data, &m_env.TEXCLUT); + WriteState(data, &m_env.SCANMSK); + WriteState(data, &m_env.TEXA); + WriteState(data, &m_env.FOGCOL); + WriteState(data, &m_env.DIMX); + WriteState(data, &m_env.DTHE); + WriteState(data, &m_env.COLCLAMP); + WriteState(data, &m_env.PABE); + WriteState(data, &m_env.BITBLTBUF); + WriteState(data, &m_env.TRXDIR); + WriteState(data, &m_env.TRXPOS); + WriteState(data, &m_env.TRXREG); + WriteState(data, &m_env.TRXREG2); + + for(int i = 0; i < 2; i++) + { + WriteState(data, &m_env.CTXT[i].XYOFFSET); + WriteState(data, &m_env.CTXT[i].TEX0); + WriteState(data, &m_env.CTXT[i].TEX1); + WriteState(data, &m_env.CTXT[i].TEX2); + WriteState(data, &m_env.CTXT[i].CLAMP); + WriteState(data, &m_env.CTXT[i].MIPTBP1); + WriteState(data, &m_env.CTXT[i].MIPTBP2); + WriteState(data, &m_env.CTXT[i].SCISSOR); + WriteState(data, &m_env.CTXT[i].ALPHA); + WriteState(data, &m_env.CTXT[i].TEST); + WriteState(data, &m_env.CTXT[i].FBA); + WriteState(data, &m_env.CTXT[i].FRAME); + WriteState(data, &m_env.CTXT[i].ZBUF); + } + + WriteState(data, &m_v.RGBAQ); + WriteState(data, &m_v.ST); + WriteState(data, &m_v.UV); + WriteState(data, &m_v.XYZ); + WriteState(data, &m_v.FOG); + WriteState(data, &m_x); + WriteState(data, &m_y); + WriteState(data, m_mem.m_vm8, m_mem.m_vmsize); + + for(int i = 0; i < 3; i++) + { + WriteState(data, &m_path[i].tag); + WriteState(data, &m_path[i].nreg); + } + + WriteState(data, &m_q); + + return 0; +} + +int GSState::Defrost(const GSFreezeData* fd) +{ + if(!fd || !fd->data || fd->size == 0) + { + return -1; + } + + if(fd->size < m_sssize) + { + return -1; + } + + BYTE* data = fd->data; + + int version; + + ReadState(&version, data); + + if(version > m_version) + { + return -1; + } + + Flush(); + + Reset(); + + ReadState(&m_env.PRIM, data); + ReadState(&m_env.PRMODE, data); + ReadState(&m_env.PRMODECONT, data); + ReadState(&m_env.TEXCLUT, data); + ReadState(&m_env.SCANMSK, data); + ReadState(&m_env.TEXA, data); + ReadState(&m_env.FOGCOL, data); + ReadState(&m_env.DIMX, data); + ReadState(&m_env.DTHE, data); + ReadState(&m_env.COLCLAMP, data); + ReadState(&m_env.PABE, data); + ReadState(&m_env.BITBLTBUF, data); + ReadState(&m_env.TRXDIR, data); + ReadState(&m_env.TRXPOS, data); + ReadState(&m_env.TRXREG, data); + ReadState(&m_env.TRXREG2, data); + + for(int i = 0; i < 2; i++) + { + ReadState(&m_env.CTXT[i].XYOFFSET, data); + ReadState(&m_env.CTXT[i].TEX0, data); + ReadState(&m_env.CTXT[i].TEX1, data); + ReadState(&m_env.CTXT[i].TEX2, data); + ReadState(&m_env.CTXT[i].CLAMP, data); + ReadState(&m_env.CTXT[i].MIPTBP1, data); + ReadState(&m_env.CTXT[i].MIPTBP2, data); + ReadState(&m_env.CTXT[i].SCISSOR, data); + ReadState(&m_env.CTXT[i].ALPHA, data); + ReadState(&m_env.CTXT[i].TEST, data); + ReadState(&m_env.CTXT[i].FBA, data); + ReadState(&m_env.CTXT[i].FRAME, data); + ReadState(&m_env.CTXT[i].ZBUF, data); + + m_env.CTXT[i].XYOFFSET.OFX &= 0xffff; + m_env.CTXT[i].XYOFFSET.OFY &= 0xffff; + + if(version <= 4) + { + data += sizeof(DWORD) * 7; // skip + } + } + + ReadState(&m_v.RGBAQ, data); + ReadState(&m_v.ST, data); + ReadState(&m_v.UV, data); + ReadState(&m_v.XYZ, data); + ReadState(&m_v.FOG, data); + ReadState(&m_x, data); + ReadState(&m_y, data); + ReadState(m_mem.m_vm8, data, m_mem.m_vmsize); + + for(int i = 0; i < 3; i++) + { + ReadState(&m_path[i].tag, data); + ReadState(&m_path[i].nreg, data); + + m_path[i].SetTag(&m_path[i].tag); // expand regs + } + + ReadState(&m_q, data); + + PRIM = !m_env.PRMODECONT.AC ? (GIFRegPRIM*)&m_env.PRMODE : &m_env.PRIM; + + m_context = &m_env.CTXT[PRIM->CTXT]; + + UpdateVertexKick(); + + m_env.CTXT[0].UpdateScissor(); + m_env.CTXT[1].UpdateScissor(); + +m_perfmon.SetFrame(5000); + + return 0; +} + +void GSState::SetGameCRC(DWORD crc, int options) +{ + m_crc = crc; + m_options = options; + m_game = CRC::Lookup(crc); + + if(m_nloophack_org == 2) + { + m_nloophack = m_game.nloophack; + } +} + +void GSState::SetFrameSkip(int frameskip) +{ + if(m_frameskip != frameskip) + { + m_frameskip = frameskip; + + if(frameskip) + { + m_fpGIFPackedRegHandlers[GIF_REG_PRIM] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_RGBA] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_STQ] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_UV] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF2] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ2] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_1] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_2] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_FOG] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF3] = &GSState::GIFPackedRegHandlerNOP; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ3] = &GSState::GIFPackedRegHandlerNOP; + + m_fpGIFRegHandlers[GIF_A_D_REG_PRIM] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_RGBAQ] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_ST] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_UV] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF2] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ2] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF3] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ3] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODECONT] = &GSState::GIFRegHandlerNOP; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODE] = &GSState::GIFRegHandlerNOP; + } + else + { + m_fpGIFPackedRegHandlers[GIF_REG_PRIM] = &GSState::GIFPackedRegHandlerPRIM; + m_fpGIFPackedRegHandlers[GIF_REG_RGBA] = &GSState::GIFPackedRegHandlerRGBA; + m_fpGIFPackedRegHandlers[GIF_REG_STQ] = &GSState::GIFPackedRegHandlerSTQ; + m_fpGIFPackedRegHandlers[GIF_REG_UV] = &GSState::GIFPackedRegHandlerUV; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF2] = &GSState::GIFPackedRegHandlerXYZF2; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ2] = &GSState::GIFPackedRegHandlerXYZ2; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_1] = &GSState::GIFPackedRegHandlerCLAMP<0>; + m_fpGIFPackedRegHandlers[GIF_REG_CLAMP_2] = &GSState::GIFPackedRegHandlerCLAMP<1>; + m_fpGIFPackedRegHandlers[GIF_REG_FOG] = &GSState::GIFPackedRegHandlerFOG; + m_fpGIFPackedRegHandlers[GIF_REG_XYZF3] = &GSState::GIFPackedRegHandlerXYZF3; + m_fpGIFPackedRegHandlers[GIF_REG_XYZ3] = &GSState::GIFPackedRegHandlerXYZ3; + + m_fpGIFRegHandlers[GIF_A_D_REG_PRIM] = &GSState::GIFRegHandlerPRIM; + m_fpGIFRegHandlers[GIF_A_D_REG_RGBAQ] = &GSState::GIFRegHandlerRGBAQ; + m_fpGIFRegHandlers[GIF_A_D_REG_ST] = &GSState::GIFRegHandlerST; + m_fpGIFRegHandlers[GIF_A_D_REG_UV] = &GSState::GIFRegHandlerUV; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF2] = &GSState::GIFRegHandlerXYZF2; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ2] = &GSState::GIFRegHandlerXYZ2; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZF3] = &GSState::GIFRegHandlerXYZF3; + m_fpGIFRegHandlers[GIF_A_D_REG_XYZ3] = &GSState::GIFRegHandlerXYZ3; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODECONT] = &GSState::GIFRegHandlerPRMODECONT; + m_fpGIFRegHandlers[GIF_A_D_REG_PRMODE] = &GSState::GIFRegHandlerPRMODE; + } + } +} + +// hacks + +struct GSFrameInfo +{ + DWORD FBP; + DWORD FPSM; + bool TME; + DWORD TBP0; + DWORD TPSM; +}; + +typedef bool (*GetSkipCount)(const GSFrameInfo& fi, int& skip); + +bool GSC_Okami(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x00e00 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x00000 && fi.TPSM == PSM_PSMCT32) + { + skip = 1000; + } + } + else + { + if(fi.TME && fi.FBP == 0x00e00 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x03800 && fi.TPSM == PSM_PSMT4) + { + skip = 0; + } + } + + return true; +} + +bool GSC_MetalGearSolid3(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02000 && fi.FPSM == PSM_PSMCT32 && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x01000) && fi.TPSM == PSM_PSMCT24) + { + skip = 1000; // 76, 79 + } + else if(fi.TME && fi.FBP == 0x02800 && fi.FPSM == PSM_PSMCT24 && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x01000) && fi.TPSM == PSM_PSMCT32) + { + skip = 1000; // 69 + } + } + else + { + if(!fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01000) && fi.FPSM == PSM_PSMCT32) + { + skip = 0; + } + } + + return true; +} + +bool GSC_DBZBT2(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && /*fi.FBP == 0x00000 && fi.FPSM == PSM_PSMCT16 &&*/ fi.TBP0 == 0x02000 && fi.TPSM == PSM_PSMZ16) + { + skip = 27; + } + else if(!fi.TME && fi.FBP == 0x03000 && fi.FPSM == PSM_PSMCT16) + { + skip = 10; + } + } + + return true; +} + +bool GSC_DBZBT3(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x01c00 && fi.FPSM == PSM_PSMCT32 && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x00e00) && fi.TPSM == PSM_PSMT8H) + { + skip = 24; // blur + } + else if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00e00) && fi.FPSM == PSM_PSMCT32 && fi.TPSM == PSM_PSMT8H) + { + skip = 28; // outline + } + } + + return true; +} + +bool GSC_SFEX3(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x00f00 && fi.FPSM == PSM_PSMCT16 && (fi.TBP0 == 0x00500 || fi.TBP0 == 0x00000) && fi.TPSM == PSM_PSMCT32) + { + skip = 4; + } + } + + return true; +} + +bool GSC_Bully(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01180) && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x01180) && fi.FBP == fi.TBP0 && fi.FPSM == PSM_PSMCT32 && fi.FPSM == fi.TPSM) + { + return false; // allowed + } + + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01180) && fi.FPSM == PSM_PSMCT16S && fi.TBP0 == 0x02300 && fi.TPSM == PSM_PSMZ16S) + { + skip = 6; + } + } + else + { + if(!fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01180) && fi.FPSM == PSM_PSMCT32) + { + skip = 0; + } + } + + return true; +} + +bool GSC_BullyCC(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01180) && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x01180) && fi.FBP == fi.TBP0 && fi.FPSM == PSM_PSMCT32 && fi.FPSM == fi.TPSM) + { + return false; // allowed + } + + if(!fi.TME && fi.FBP == 0x02800 && fi.FPSM == PSM_PSMCT24) + { + skip = 9; + } + } + + return true; +} +bool GSC_SoTC(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02b80 && fi.FPSM == PSM_PSMCT24 && fi.TBP0 == 0x01e80 && fi.TPSM == PSM_PSMCT24) + { + skip = 9; + } + else if(fi.TME && fi.FBP == 0x01c00 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x03800 && fi.TPSM == PSM_PSMCT32) + { + skip = 8; + } + else if(fi.TME && fi.FBP == 0x01e80 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x03880 && fi.TPSM == PSM_PSMCT32) + { + skip = 8; + } + } + + return true; +} + +bool GSC_OnePieceGrandAdventure(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02d00 && fi.FPSM == PSM_PSMCT16 && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x00e00) && fi.TPSM == PSM_PSMCT16) + { + skip = 3; + } + } + + return true; +} + +bool GSC_ICO(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x00800 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x03d00 && fi.TPSM == PSM_PSMCT32) + { + skip = 3; + } + else if(fi.TME && fi.FBP == 0x00800 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x02800 && fi.TPSM == PSM_PSMT8H) + { + skip = 1; + } + } + else + { + if(fi.TME && fi.TBP0 == 0x00800 && fi.TPSM == PSM_PSMCT32) + { + skip = 0; + } + } + + return true; +} + +bool GSC_GT4(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && (fi.FBP == 0x03440 || fi.FBP >= 0x03e00) && fi.FPSM == PSM_PSMCT32 && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x01400) && fi.TPSM == PSM_PSMT8) + { + skip = 880; + } + else if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x01400) && fi.FPSM == PSM_PSMCT24 && fi.TBP0 >= 0x03420 && fi.TPSM == PSM_PSMT8) + { + // TODO: removes gfx from where it is not supposed to (garage) + // skip = 58; + } + } + + return true; +} + +bool GSC_WildArms5(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x03100 && fi.FPSM == PSM_PSMZ32 && fi.TBP0 == 0x01c00 && fi.TPSM == PSM_PSMZ32) + { + skip = 100; + } + } + else + { + if(fi.TME && fi.FBP == 0x00e00 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x02a00 && fi.TPSM == PSM_PSMCT32) + { + skip = 1; + } + } + + return true; +} + +bool GSC_Manhunt2(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x03c20 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x01400 && fi.TPSM == PSM_PSMT8) + { + skip = 640; + } + } + + return true; +} + +bool GSC_CrashBandicootWoC(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00a00) && (fi.TBP0 == 0x00000 || fi.TBP0 == 0x00a00) && fi.FBP == fi.TBP0 && fi.FPSM == PSM_PSMCT32 && fi.FPSM == fi.TPSM) + { + return false; // allowed + } + + if(fi.TME && fi.FBP == 0x02200 && fi.FPSM == PSM_PSMZ24 && fi.TBP0 == 0x01400 && fi.TPSM == PSM_PSMZ24) + { + skip = 41; + } + } + else + { + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00a00) && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x03c00 && fi.TPSM == PSM_PSMCT32) + { + skip = 0; + } + else if(!fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00a00)) + { + skip = 0; + } + } + + return true; +} + +bool GSC_ResidentEvil4(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x03100 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x01c00 && fi.TPSM == PSM_PSMZ24) + { + skip = 176; + } + } + + return true; +} + +bool GSC_Spartan(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02000 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x00000 && fi.TPSM == PSM_PSMCT32) + { + skip = 107; + } + } + + return true; +} + +bool GSC_AceCombat4(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02a00 && fi.FPSM == PSM_PSMZ24 && fi.TBP0 == 0x01600 && fi.TPSM == PSM_PSMZ24) + { + skip = 71; // clouds (z, 16-bit) + } + else if(fi.TME && fi.FBP == 0x02900 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x00000 && fi.TPSM == PSM_PSMCT24) + { + skip = 28; // blur + } + } + + return true; +} + +bool GSC_Drakengard2(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x026c0 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x00a00 && fi.TPSM == PSM_PSMCT32) + { + skip = 64; + } + } + + return true; +} + +bool GSC_Tekken5(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x02ea0 && fi.FPSM == PSM_PSMCT32 && fi.TBP0 == 0x00000 && fi.TPSM == PSM_PSMCT32) + { + skip = 95; + } + } + + return true; +} + +bool GSC_IkkiTousen(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x00a80 && fi.FPSM == PSM_PSMZ24 && fi.TBP0 == 0x01180 && fi.TPSM == PSM_PSMZ24) + { + skip = 1000; // shadow (result is broken without depth copy, also includes 16 bit) + } + else if(fi.TME && fi.FBP == 0x00700 && fi.FPSM == PSM_PSMZ24 && fi.TBP0 == 0x01180 && fi.TPSM == PSM_PSMZ24) + { + skip = 11; // blur + } + } + else if(skip > 7) + { + if(fi.TME && fi.FBP == 0x00700 && fi.FPSM == PSM_PSMCT16 && fi.TBP0 == 0x00700 && fi.TPSM == PSM_PSMCT16) + { + skip = 7; // the last steps of shadow drawing + } + } + + return true; +} + +bool GSC_GodOfWar(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x00000 && fi.FPSM == PSM_PSMCT16 && fi.TBP0 == 0x00000 && fi.TPSM == PSM_PSMCT16) + { + skip = 30; + } + } + else + { + } + + return true; +} + +bool GSC_GiTS(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && fi.FBP == 0x01400 && fi.FPSM == PSM_PSMCT16 && fi.TBP0 == 0x02e40 && fi.TPSM == PSM_PSMCT16) + { + skip = 1315; + } + } + else + { + } + + return true; +} + +bool GSC_Onimusha3(const GSFrameInfo& fi, int& skip) +{ + if(fi.TME /*&& (fi.FBP == 0x00000 || fi.FBP == 0x00700)*/ && (fi.TBP0 == 0x01180 || fi.TBP0 == 0x00e00 || fi.TBP0 == 0x01000 || fi.TBP0 == 0x01200) && (fi.TPSM == PSM_PSMCT32 || fi.TPSM == PSM_PSMCT24)) + { + skip = 1; + } + + return true; +} + +bool GSC_TalesOfAbyss(const GSFrameInfo& fi, int& skip) +{ + if(skip == 0) + { + if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00e00) && fi.TBP0 == 0x01c00 && fi.TPSM == PSM_PSMT8) // copies the z buffer to the alpha channel of the fb + { + skip = 1000; + } + else if(fi.TME && (fi.FBP == 0x00000 || fi.FBP == 0x00e00) && (fi.TBP0 == 0x03560 || fi.TBP0 == 0x038e0) && fi.TPSM == PSM_PSMCT32) + { + skip = 1; + } + } + else + { + if(fi.TME && fi.TPSM != PSM_PSMT8) + { + skip = 0; + } + } + + return true; +} + +bool GSState::IsBadFrame(int& skip) +{ + GSFrameInfo fi; + + fi.FBP = m_context->FRAME.Block(); + fi.FPSM = m_context->FRAME.PSM; + fi.TME = PRIM->TME; + fi.TBP0 = m_context->TEX0.TBP0; + fi.TPSM = m_context->TEX0.PSM; + + static GetSkipCount map[CRC::TitleCount]; + static bool inited = false; + + if(!inited) + { + inited = true; + + memset(map, 0, sizeof(map)); + + map[CRC::Okami] = GSC_Okami; + map[CRC::MetalGearSolid3] = GSC_MetalGearSolid3; + map[CRC::DBZBT2] = GSC_DBZBT2; + map[CRC::DBZBT3] = GSC_DBZBT3; + map[CRC::SFEX3] = GSC_SFEX3; + map[CRC::Bully] = GSC_Bully; + map[CRC::BullyCC] = GSC_BullyCC; + map[CRC::SoTC] = GSC_SoTC; + map[CRC::OnePieceGrandAdventure] = GSC_OnePieceGrandAdventure; + map[CRC::ICO] = GSC_ICO; + map[CRC::GT4] = GSC_GT4; + map[CRC::WildArms5] = GSC_WildArms5; + map[CRC::Manhunt2] = GSC_Manhunt2; + map[CRC::CrashBandicootWoC] = GSC_CrashBandicootWoC; + map[CRC::ResidentEvil4] = GSC_ResidentEvil4; + map[CRC::Spartan] = GSC_Spartan; + map[CRC::AceCombat4] = GSC_AceCombat4; + map[CRC::Drakengard2] = GSC_Drakengard2; + map[CRC::Tekken5] = GSC_Tekken5; + map[CRC::IkkiTousen] = GSC_IkkiTousen; + map[CRC::GodOfWar] = GSC_GodOfWar; + map[CRC::GodOfWar2] = GSC_GodOfWar; + map[CRC::GiTS] = GSC_GiTS; + map[CRC::Onimusha3] = GSC_Onimusha3; + map[CRC::TalesOfAbyss] = GSC_TalesOfAbyss; + } + + // TODO: just set gsc in SetGameCRC once + + GetSkipCount gsc = map[m_game.title]; + + if(gsc && !gsc(fi, skip)) + { + return false; + } + + if(skip == 0) + { + if(fi.TME) + { + if(GSUtil::HasSharedBits(fi.FBP, fi.FPSM, fi.TBP0, fi.TPSM)) + { + // skip = 1; + } + + // depth textures (bully, mgs3s1 intro) + + if(fi.TPSM == PSM_PSMZ32 || fi.TPSM == PSM_PSMZ24 || fi.TPSM == PSM_PSMZ16 || fi.TPSM == PSM_PSMZ16S) + { + skip = 1; + } + } + } + + if(skip > 0) + { + skip--; + + return true; + } + + return false; +} diff --git a/plugins/GSdx/GSState.h b/plugins/GSdx/GSState.h new file mode 100644 index 0000000000..8b5f662980 --- /dev/null +++ b/plugins/GSdx/GSState.h @@ -0,0 +1,268 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSLocalMemory.h" +#include "GSDrawingContext.h" +#include "GSDrawingEnvironment.h" +#include "GSVertex.h" +#include "GSVertexList.h" +#include "GSUtil.h" +#include "GSDirtyRect.h" +#include "GSPerfMon.h" +#include "GSVector.h" +#include "GSDevice.h" +#include "GSCrc.h" +#include "GSAlignedClass.h" +#include "GSDump.h" + +class GSState : public GSAlignedClass<16> +{ + typedef void (GSState::*GIFPackedRegHandler)(GIFPackedReg* r); + + GIFPackedRegHandler m_fpGIFPackedRegHandlers[16]; + + void GIFPackedRegHandlerNull(GIFPackedReg* r); + void GIFPackedRegHandlerPRIM(GIFPackedReg* r); + void GIFPackedRegHandlerRGBA(GIFPackedReg* r); + void GIFPackedRegHandlerSTQ(GIFPackedReg* r); + void GIFPackedRegHandlerUV(GIFPackedReg* r); + void GIFPackedRegHandlerXYZF2(GIFPackedReg* r); + void GIFPackedRegHandlerXYZ2(GIFPackedReg* r); + template void GIFPackedRegHandlerTEX0(GIFPackedReg* r); + template void GIFPackedRegHandlerCLAMP(GIFPackedReg* r); + void GIFPackedRegHandlerFOG(GIFPackedReg* r); + void GIFPackedRegHandlerXYZF3(GIFPackedReg* r); + void GIFPackedRegHandlerXYZ3(GIFPackedReg* r); + void GIFPackedRegHandlerA_D(GIFPackedReg* r); + void GIFPackedRegHandlerA_D(GIFPackedReg* r, int size); + void GIFPackedRegHandlerNOP(GIFPackedReg* r); + + typedef void (GSState::*GIFRegHandler)(GIFReg* r); + + GIFRegHandler m_fpGIFRegHandlers[256]; + + void GIFRegHandlerNull(GIFReg* r); + void GIFRegHandlerPRIM(GIFReg* r); + void GIFRegHandlerRGBAQ(GIFReg* r); + void GIFRegHandlerST(GIFReg* r); + void GIFRegHandlerUV(GIFReg* r); + void GIFRegHandlerXYZF2(GIFReg* r); + void GIFRegHandlerXYZ2(GIFReg* r); + template void GIFRegHandlerTEX0(GIFReg* r); + template void GIFRegHandlerCLAMP(GIFReg* r); + void GIFRegHandlerFOG(GIFReg* r); + void GIFRegHandlerXYZF3(GIFReg* r); + void GIFRegHandlerXYZ3(GIFReg* r); + void GIFRegHandlerNOP(GIFReg* r); + template void GIFRegHandlerTEX1(GIFReg* r); + template void GIFRegHandlerTEX2(GIFReg* r); + template void GIFRegHandlerXYOFFSET(GIFReg* r); + void GIFRegHandlerPRMODECONT(GIFReg* r); + void GIFRegHandlerPRMODE(GIFReg* r); + void GIFRegHandlerTEXCLUT(GIFReg* r); + void GIFRegHandlerSCANMSK(GIFReg* r); + template void GIFRegHandlerMIPTBP1(GIFReg* r); + template void GIFRegHandlerMIPTBP2(GIFReg* r); + void GIFRegHandlerTEXA(GIFReg* r); + void GIFRegHandlerFOGCOL(GIFReg* r); + void GIFRegHandlerTEXFLUSH(GIFReg* r); + template void GIFRegHandlerSCISSOR(GIFReg* r); + template void GIFRegHandlerALPHA(GIFReg* r); + void GIFRegHandlerDIMX(GIFReg* r); + void GIFRegHandlerDTHE(GIFReg* r); + void GIFRegHandlerCOLCLAMP(GIFReg* r); + template void GIFRegHandlerTEST(GIFReg* r); + void GIFRegHandlerPABE(GIFReg* r); + template void GIFRegHandlerFBA(GIFReg* r); + template void GIFRegHandlerFRAME(GIFReg* r); + template void GIFRegHandlerZBUF(GIFReg* r); + void GIFRegHandlerBITBLTBUF(GIFReg* r); + void GIFRegHandlerTRXPOS(GIFReg* r); + void GIFRegHandlerTRXREG(GIFReg* r); + void GIFRegHandlerTRXDIR(GIFReg* r); + void GIFRegHandlerHWREG(GIFReg* r); + void GIFRegHandlerSIGNAL(GIFReg* r); + void GIFRegHandlerFINISH(GIFReg* r); + void GIFRegHandlerLABEL(GIFReg* r); + + int m_version; + int m_sssize; + + bool m_mt; + void (*m_irq)(); + bool m_path3hack; + int m_nloophack_org; + + int m_x, m_y; + int m_bytes; + int m_maxbytes; + BYTE* m_buff; + + void FlushWrite(); + void FlushWrite(BYTE* mem, int len); + +protected: + bool IsBadFrame(int& skip); + + typedef void (GSState::*VertexKickPtr)(bool skip); + + VertexKickPtr m_vk[8][2][2]; + VertexKickPtr m_vkf; + + template void InitVertexKick() + { + m_vk[GS_POINTLIST][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_POINTLIST][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_POINTLIST][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_POINTLIST][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_LINELIST][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINELIST][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINELIST][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINELIST][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_LINESTRIP][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINESTRIP][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINESTRIP][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_LINESTRIP][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_TRIANGLELIST][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLELIST][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLELIST][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLELIST][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_TRIANGLESTRIP][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLESTRIP][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLESTRIP][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLESTRIP][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_TRIANGLEFAN][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLEFAN][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLEFAN][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_TRIANGLEFAN][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_SPRITE][0][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_SPRITE][0][1] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_SPRITE][1][0] = (VertexKickPtr)&T::VertexKick; + m_vk[GS_SPRITE][1][1] = (VertexKickPtr)&T::VertexKick; + + m_vk[GS_INVALID][0][0] = &GSState::VertexKickNull; + m_vk[GS_INVALID][0][1] = &GSState::VertexKickNull; + m_vk[GS_INVALID][1][0] = &GSState::VertexKickNull; + m_vk[GS_INVALID][1][1] = &GSState::VertexKickNull; + } + + void UpdateVertexKick() + { + m_vkf = m_vk[PRIM->PRIM][PRIM->TME][PRIM->FST]; + } + + void VertexKickNull(bool skip) + { + ASSERT(0); + } + + void VertexKick(bool skip) + { + (this->*m_vkf)(skip); + } + +public: + GIFRegPRIM* PRIM; + GSRegPMODE* PMODE; + GSRegSMODE1* SMODE1; + GSRegSMODE2* SMODE2; + GSRegDISPFB* DISPFB[2]; + GSRegDISPLAY* DISPLAY[2]; + GSRegEXTBUF* EXTBUF; + GSRegEXTDATA* EXTDATA; + GSRegEXTWRITE* EXTWRITE; + GSRegBGCOLOR* BGCOLOR; + GSRegCSR* CSR; + GSRegIMR* IMR; + GSRegBUSDIR* BUSDIR; + GSRegSIGLBLID* SIGLBLID; + + GIFPath m_path[3]; + GSLocalMemory m_mem; + GSDrawingEnvironment m_env; + GSDrawingContext* m_context; + GSVertex m_v; + float m_q; + DWORD m_vprim; + + GSPerfMon m_perfmon; + bool m_nloophack; + DWORD m_crc; + int m_options; + int m_frameskip; + CRC::Game m_game; + GSDump m_dump; + +public: + GSState(BYTE* base, bool mt, void (*irq)(), int nloophack); + virtual ~GSState(); + + void ResetHandlers(); + + CPoint GetDisplayPos(int i); + CSize GetDisplaySize(int i); + CRect GetDisplayRect(int i); + CSize GetDisplayPos(); + CSize GetDisplaySize(); + CRect GetDisplayRect(); + CPoint GetFramePos(int i); + CSize GetFrameSize(int i); + CRect GetFrameRect(int i); + CSize GetFramePos(); + CSize GetFrameSize(); + CRect GetFrameRect(); + CSize GetDeviceSize(int i); + CSize GetDeviceSize(); + bool IsEnabled(int i); + int GetFPS(); + + virtual void Reset(); + virtual void Flush(); + virtual void FlushPrim() = 0; + virtual void ResetPrim() = 0; + virtual void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, CRect r) {} + virtual void InvalidateLocalMem(const GIFRegBITBLTBUF& BITBLTBUF, CRect r) {} + virtual void InvalidateTextureCache() {} + + void Move(); + void Write(BYTE* mem, int len); + void Read(BYTE* mem, int len); + + void SoftReset(BYTE mask); + void WriteCSR(UINT32 csr) {CSR->ai32[1] = csr;} + void ReadFIFO(BYTE* mem, int size); + template void Transfer(BYTE* mem, UINT32 size); + int Freeze(GSFreezeData* fd, bool sizeonly); + int Defrost(const GSFreezeData* fd); + void GetLastTag(UINT32* tag) {*tag = m_path3hack; m_path3hack = 0;} + virtual void SetGameCRC(DWORD crc, int options); + void SetFrameSkip(int frameskip); +}; + diff --git a/plugins/GSdx/GSTables.cpp b/plugins/GSdx/GSTables.cpp new file mode 100644 index 0000000000..ce5ef0e62f --- /dev/null +++ b/plugins/GSdx/GSTables.cpp @@ -0,0 +1,263 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTables.h" + +const BYTE blockTable32[4][8] = { + { 0, 1, 4, 5, 16, 17, 20, 21}, + { 2, 3, 6, 7, 18, 19, 22, 23}, + { 8, 9, 12, 13, 24, 25, 28, 29}, + { 10, 11, 14, 15, 26, 27, 30, 31} +}; + +const BYTE blockTable32Z[4][8] = { + { 24, 25, 28, 29, 8, 9, 12, 13}, + { 26, 27, 30, 31, 10, 11, 14, 15}, + { 16, 17, 20, 21, 0, 1, 4, 5}, + { 18, 19, 22, 23, 2, 3, 6, 7} +}; + +const BYTE blockTable16[8][4] = { + { 0, 2, 8, 10 }, + { 1, 3, 9, 11 }, + { 4, 6, 12, 14 }, + { 5, 7, 13, 15 }, + { 16, 18, 24, 26 }, + { 17, 19, 25, 27 }, + { 20, 22, 28, 30 }, + { 21, 23, 29, 31 } +}; + +const BYTE blockTable16S[8][4] = { + { 0, 2, 16, 18 }, + { 1, 3, 17, 19 }, + { 8, 10, 24, 26 }, + { 9, 11, 25, 27 }, + { 4, 6, 20, 22 }, + { 5, 7, 21, 23 }, + { 12, 14, 28, 30 }, + { 13, 15, 29, 31 } +}; + +const BYTE blockTable16Z[8][4] = { + { 24, 26, 16, 18 }, + { 25, 27, 17, 19 }, + { 28, 30, 20, 22 }, + { 29, 31, 21, 23 }, + { 8, 10, 0, 2 }, + { 9, 11, 1, 3 }, + { 12, 14, 4, 6 }, + { 13, 15, 5, 7 } +}; + +const BYTE blockTable16SZ[8][4] = { + { 24, 26, 8, 10 }, + { 25, 27, 9, 11 }, + { 16, 18, 0, 2 }, + { 17, 19, 1, 3 }, + { 28, 30, 12, 14 }, + { 29, 31, 13, 15 }, + { 20, 22, 4, 6 }, + { 21, 23, 5, 7 } +}; + +const BYTE blockTable8[4][8] = { + { 0, 1, 4, 5, 16, 17, 20, 21}, + { 2, 3, 6, 7, 18, 19, 22, 23}, + { 8, 9, 12, 13, 24, 25, 28, 29}, + { 10, 11, 14, 15, 26, 27, 30, 31} +}; + +const BYTE blockTable4[8][4] = { + { 0, 2, 8, 10 }, + { 1, 3, 9, 11 }, + { 4, 6, 12, 14 }, + { 5, 7, 13, 15 }, + { 16, 18, 24, 26 }, + { 17, 19, 25, 27 }, + { 20, 22, 28, 30 }, + { 21, 23, 29, 31 } +}; + +const BYTE columnTable32[8][8] = { + { 0, 1, 4, 5, 8, 9, 12, 13 }, + { 2, 3, 6, 7, 10, 11, 14, 15 }, + { 16, 17, 20, 21, 24, 25, 28, 29 }, + { 18, 19, 22, 23, 26, 27, 30, 31 }, + { 32, 33, 36, 37, 40, 41, 44, 45 }, + { 34, 35, 38, 39, 42, 43, 46, 47 }, + { 48, 49, 52, 53, 56, 57, 60, 61 }, + { 50, 51, 54, 55, 58, 59, 62, 63 }, +}; + +const BYTE columnTable16[8][16] = { + { 0, 2, 8, 10, 16, 18, 24, 26, + 1, 3, 9, 11, 17, 19, 25, 27 }, + { 4, 6, 12, 14, 20, 22, 28, 30, + 5, 7, 13, 15, 21, 23, 29, 31 }, + { 32, 34, 40, 42, 48, 50, 56, 58, + 33, 35, 41, 43, 49, 51, 57, 59 }, + { 36, 38, 44, 46, 52, 54, 60, 62, + 37, 39, 45, 47, 53, 55, 61, 63 }, + { 64, 66, 72, 74, 80, 82, 88, 90, + 65, 67, 73, 75, 81, 83, 89, 91 }, + { 68, 70, 76, 78, 84, 86, 92, 94, + 69, 71, 77, 79, 85, 87, 93, 95 }, + { 96, 98, 104, 106, 112, 114, 120, 122, + 97, 99, 105, 107, 113, 115, 121, 123 }, + { 100, 102, 108, 110, 116, 118, 124, 126, + 101, 103, 109, 111, 117, 119, 125, 127 }, +}; + +const BYTE columnTable8[16][16] = { + { 0, 4, 16, 20, 32, 36, 48, 52, // column 0 + 2, 6, 18, 22, 34, 38, 50, 54 }, + { 8, 12, 24, 28, 40, 44, 56, 60, + 10, 14, 26, 30, 42, 46, 58, 62 }, + { 33, 37, 49, 53, 1, 5, 17, 21, + 35, 39, 51, 55, 3, 7, 19, 23 }, + { 41, 45, 57, 61, 9, 13, 25, 29, + 43, 47, 59, 63, 11, 15, 27, 31 }, + { 96, 100, 112, 116, 64, 68, 80, 84, // column 1 + 98, 102, 114, 118, 66, 70, 82, 86 }, + { 104, 108, 120, 124, 72, 76, 88, 92, + 106, 110, 122, 126, 74, 78, 90, 94 }, + { 65, 69, 81, 85, 97, 101, 113, 117, + 67, 71, 83, 87, 99, 103, 115, 119 }, + { 73, 77, 89, 93, 105, 109, 121, 125, + 75, 79, 91, 95, 107, 111, 123, 127 }, + { 128, 132, 144, 148, 160, 164, 176, 180, // column 2 + 130, 134, 146, 150, 162, 166, 178, 182 }, + { 136, 140, 152, 156, 168, 172, 184, 188, + 138, 142, 154, 158, 170, 174, 186, 190 }, + { 161, 165, 177, 181, 129, 133, 145, 149, + 163, 167, 179, 183, 131, 135, 147, 151 }, + { 169, 173, 185, 189, 137, 141, 153, 157, + 171, 175, 187, 191, 139, 143, 155, 159 }, + { 224, 228, 240, 244, 192, 196, 208, 212, // column 3 + 226, 230, 242, 246, 194, 198, 210, 214 }, + { 232, 236, 248, 252, 200, 204, 216, 220, + 234, 238, 250, 254, 202, 206, 218, 222 }, + { 193, 197, 209, 213, 225, 229, 241, 245, + 195, 199, 211, 215, 227, 231, 243, 247 }, + { 201, 205, 217, 221, 233, 237, 249, 253, + 203, 207, 219, 223, 235, 239, 251, 255 }, +}; + +const WORD columnTable4[16][32] = { + { 0, 8, 32, 40, 64, 72, 96, 104, // column 0 + 2, 10, 34, 42, 66, 74, 98, 106, + 4, 12, 36, 44, 68, 76, 100, 108, + 6, 14, 38, 46, 70, 78, 102, 110 }, + { 16, 24, 48, 56, 80, 88, 112, 120, + 18, 26, 50, 58, 82, 90, 114, 122, + 20, 28, 52, 60, 84, 92, 116, 124, + 22, 30, 54, 62, 86, 94, 118, 126 }, + { 65, 73, 97, 105, 1, 9, 33, 41, + 67, 75, 99, 107, 3, 11, 35, 43, + 69, 77, 101, 109, 5, 13, 37, 45, + 71, 79, 103, 111, 7, 15, 39, 47 }, + { 81, 89, 113, 121, 17, 25, 49, 57, + 83, 91, 115, 123, 19, 27, 51, 59, + 85, 93, 117, 125, 21, 29, 53, 61, + 87, 95, 119, 127, 23, 31, 55, 63 }, + { 192, 200, 224, 232, 128, 136, 160, 168, // column 1 + 194, 202, 226, 234, 130, 138, 162, 170, + 196, 204, 228, 236, 132, 140, 164, 172, + 198, 206, 230, 238, 134, 142, 166, 174 }, + { 208, 216, 240, 248, 144, 152, 176, 184, + 210, 218, 242, 250, 146, 154, 178, 186, + 212, 220, 244, 252, 148, 156, 180, 188, + 214, 222, 246, 254, 150, 158, 182, 190 }, + { 129, 137, 161, 169, 193, 201, 225, 233, + 131, 139, 163, 171, 195, 203, 227, 235, + 133, 141, 165, 173, 197, 205, 229, 237, + 135, 143, 167, 175, 199, 207, 231, 239 }, + { 145, 153, 177, 185, 209, 217, 241, 249, + 147, 155, 179, 187, 211, 219, 243, 251, + 149, 157, 181, 189, 213, 221, 245, 253, + 151, 159, 183, 191, 215, 223, 247, 255 }, + { 256, 264, 288, 296, 320, 328, 352, 360, // column 2 + 258, 266, 290, 298, 322, 330, 354, 362, + 260, 268, 292, 300, 324, 332, 356, 364, + 262, 270, 294, 302, 326, 334, 358, 366 }, + { 272, 280, 304, 312, 336, 344, 368, 376, + 274, 282, 306, 314, 338, 346, 370, 378, + 276, 284, 308, 316, 340, 348, 372, 380, + 278, 286, 310, 318, 342, 350, 374, 382 }, + { 321, 329, 353, 361, 257, 265, 289, 297, + 323, 331, 355, 363, 259, 267, 291, 299, + 325, 333, 357, 365, 261, 269, 293, 301, + 327, 335, 359, 367, 263, 271, 295, 303 }, + { 337, 345, 369, 377, 273, 281, 305, 313, + 339, 347, 371, 379, 275, 283, 307, 315, + 341, 349, 373, 381, 277, 285, 309, 317, + 343, 351, 375, 383, 279, 287, 311, 319 }, + { 448, 456, 480, 488, 384, 392, 416, 424, // column 3 + 450, 458, 482, 490, 386, 394, 418, 426, + 452, 460, 484, 492, 388, 396, 420, 428, + 454, 462, 486, 494, 390, 398, 422, 430 }, + { 464, 472, 496, 504, 400, 408, 432, 440, + 466, 474, 498, 506, 402, 410, 434, 442, + 468, 476, 500, 508, 404, 412, 436, 444, + 470, 478, 502, 510, 406, 414, 438, 446 }, + { 385, 393, 417, 425, 449, 457, 481, 489, + 387, 395, 419, 427, 451, 459, 483, 491, + 389, 397, 421, 429, 453, 461, 485, 493, + 391, 399, 423, 431, 455, 463, 487, 495 }, + { 401, 409, 433, 441, 465, 473, 497, 505, + 403, 411, 435, 443, 467, 475, 499, 507, + 405, 413, 437, 445, 469, 477, 501, 509, + 407, 415, 439, 447, 471, 479, 503, 511 }, +}; + +const BYTE clutTableT32I8[128] = +{ + 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15, + 64, 65, 68, 69, 72, 73, 76, 77, 66, 67, 70, 71, 74, 75, 78, 79, + 16, 17, 20, 21, 24, 25, 28, 29, 18, 19, 22, 23, 26, 27, 30, 31, + 80, 81, 84, 85, 88, 89, 92, 93, 82, 83, 86, 87, 90, 91, 94, 95, + 32, 33, 36, 37, 40, 41, 44, 45, 34, 35, 38, 39, 42, 43, 46, 47, + 96, 97, 100, 101, 104, 105, 108, 109, 98, 99, 102, 103, 106, 107, 110, 111, + 48, 49, 52, 53, 56, 57, 60, 61, 50, 51, 54, 55, 58, 59, 62, 63, + 112, 113, 116, 117, 120, 121, 124, 125, 114, 115, 118, 119, 122, 123, 126, 127 +}; + +const BYTE clutTableT32I4[16] = +{ + 0, 1, 4, 5, 8, 9, 12, 13, + 2, 3, 6, 7, 10, 11, 14, 15 +}; + +const BYTE clutTableT16I8[32] = +{ + 0, 2, 8, 10, 16, 18, 24, 26, + 4, 6, 12, 14, 20, 22, 28, 30, + 1, 3, 9, 11, 17, 19, 25, 27, + 5, 7, 13, 15, 21, 23, 29, 31 +}; + +const BYTE clutTableT16I4[16] = +{ + 0, 2, 8, 10, 16, 18, 24, 26, + 4, 6, 12, 14, 20, 22, 28, 30 +}; diff --git a/plugins/GSdx/GSTables.h b/plugins/GSdx/GSTables.h new file mode 100644 index 0000000000..90808e3e91 --- /dev/null +++ b/plugins/GSdx/GSTables.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +extern const BYTE blockTable32[4][8]; +extern const BYTE blockTable32Z[4][8]; +extern const BYTE blockTable16[8][4]; +extern const BYTE blockTable16S[8][4]; +extern const BYTE blockTable16Z[8][4]; +extern const BYTE blockTable16SZ[8][4]; +extern const BYTE blockTable8[4][8]; +extern const BYTE blockTable4[8][4]; +extern const BYTE columnTable32[8][8]; +extern const BYTE columnTable16[8][16]; +extern const BYTE columnTable8[16][16]; +extern const WORD columnTable4[16][32]; +extern const BYTE clutTableT32I8[128]; +extern const BYTE clutTableT32I4[16]; +extern const BYTE clutTableT16I8[32]; +extern const BYTE clutTableT16I4[16]; diff --git a/plugins/GSdx/GSTexture.cpp b/plugins/GSdx/GSTexture.cpp new file mode 100644 index 0000000000..f87836f610 --- /dev/null +++ b/plugins/GSdx/GSTexture.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTexture.h" diff --git a/plugins/GSdx/GSTexture.h b/plugins/GSdx/GSTexture.h new file mode 100644 index 0000000000..46747022b1 --- /dev/null +++ b/plugins/GSdx/GSTexture.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSVector.h" + +class GSTexture +{ +public: + GSVector2 m_scale; + +public: + GSTexture() : m_scale(1, 1) {} + virtual ~GSTexture() {} + + enum {None, RenderTarget, DepthStencil, Texture, Offscreen}; + + virtual operator bool() = 0; + virtual int GetType() const = 0; + virtual int GetWidth() const = 0; + virtual int GetHeight() const = 0; + virtual int GetFormat() const = 0; + virtual bool Update(const CRect& r, const void* data, int pitch) = 0; + virtual bool Map(BYTE** bits, int& pitch, const RECT* r = NULL) = 0; + virtual void Unmap() = 0; + virtual bool Save(CString fn, bool dds = false) = 0; + + CSize GetSize() {return CSize(GetWidth(), GetHeight());} +}; diff --git a/plugins/GSdx/GSTexture10.cpp b/plugins/GSdx/GSTexture10.cpp new file mode 100644 index 0000000000..de5daf89fe --- /dev/null +++ b/plugins/GSdx/GSTexture10.cpp @@ -0,0 +1,210 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTexture10.h" + +GSTexture10::GSTexture10() +{ + memset(&m_desc, 0, sizeof(m_desc)); +} + +GSTexture10::GSTexture10(ID3D10Texture2D* texture) + : m_texture(texture) +{ + ASSERT(m_texture); + + m_texture->GetDevice(&m_dev); + m_texture->GetDesc(&m_desc); +} + +GSTexture10::~GSTexture10() +{ +} + +GSTexture10::operator bool() +{ + return !!m_texture; +} + +int GSTexture10::GetType() const +{ + if(m_desc.BindFlags & D3D10_BIND_RENDER_TARGET) return GSTexture::RenderTarget; + if(m_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL) return GSTexture::DepthStencil; + if(m_desc.BindFlags & D3D10_BIND_SHADER_RESOURCE) return GSTexture::Texture; + if(m_desc.Usage == D3D10_USAGE_STAGING) return GSTexture::Offscreen; + return GSTexture::None; +} + +int GSTexture10::GetWidth() const +{ + return m_desc.Width; +} + +int GSTexture10::GetHeight() const +{ + return m_desc.Height; +} + +int GSTexture10::GetFormat() const +{ + return m_desc.Format; +} + +bool GSTexture10::Update(const CRect& r, const void* data, int pitch) +{ + if(m_dev && m_texture) + { + D3D10_BOX box = {r.left, r.top, 0, r.right, r.bottom, 1}; + + m_dev->UpdateSubresource(m_texture, 0, &box, data, pitch, 0); + + return true; + } + + return false; +} + +bool GSTexture10::Map(BYTE** bits, int& pitch, const RECT* r) +{ + if(m_texture) + { + D3D10_MAPPED_TEXTURE2D map; + + if(SUCCEEDED(m_texture->Map(0, D3D10_MAP_READ_WRITE, 0, &map))) + { + *bits = (BYTE*)map.pData; + pitch = (int)map.RowPitch; + + return true; + } + } + + return false; +} + +void GSTexture10::Unmap() +{ + if(m_texture) + { + m_texture->Unmap(0); + } +} + +bool GSTexture10::Save(CString fn, bool dds) +{ + CComPtr res; + + if(m_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL) + { + HRESULT hr; + + D3D10_TEXTURE2D_DESC desc; + + memset(&desc, 0, sizeof(desc)); + + m_texture->GetDesc(&desc); + + desc.Usage = D3D10_USAGE_STAGING; + desc.BindFlags = 0; + desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; + + CComPtr src, dst; + + hr = m_dev->CreateTexture2D(&desc, NULL, &src); + + m_dev->CopyResource(src, m_texture); + + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + + hr = m_dev->CreateTexture2D(&desc, NULL, &dst); + + D3D10_MAPPED_TEXTURE2D sm, dm; + + hr = src->Map(0, D3D10_MAP_READ, 0, &sm); + hr = dst->Map(0, D3D10_MAP_WRITE, 0, &dm); + + BYTE* s = (BYTE*)sm.pData; + BYTE* d = (BYTE*)dm.pData; + + for(UINT y = 0; y < desc.Height; y++, s += sm.RowPitch, d += dm.RowPitch) + { + for(UINT x = 0; x < desc.Width; x++) + { + ((UINT*)d)[x] = (UINT)(((float*)s)[x*2] * UINT_MAX); + } + } + + src->Unmap(0); + dst->Unmap(0); + + res = dst; + } + else + { + res = m_texture; + } + + return SUCCEEDED(D3DX10SaveTextureToFile(res, dds ? D3DX10_IFF_DDS : D3DX10_IFF_BMP, fn)); +} + +ID3D10Texture2D* GSTexture10::operator->() +{ + return m_texture; +} + +GSTexture10::operator ID3D10Texture2D*() +{ + return m_texture; +} + +GSTexture10::operator ID3D10ShaderResourceView*() +{ + if(!m_srv && m_dev && m_texture) + { + m_dev->CreateShaderResourceView(m_texture, NULL, &m_srv); + } + + return m_srv; +} + +GSTexture10::operator ID3D10RenderTargetView*() +{ + ASSERT(m_dev); + + if(!m_rtv && m_dev && m_texture) + { + m_dev->CreateRenderTargetView(m_texture, NULL, &m_rtv); + } + + return m_rtv; +} + +GSTexture10::operator ID3D10DepthStencilView*() +{ + if(!m_dsv && m_dev && m_texture) + { + m_dev->CreateDepthStencilView(m_texture, NULL, &m_dsv); + } + + return m_dsv; +} diff --git a/plugins/GSdx/GSTexture10.h b/plugins/GSdx/GSTexture10.h new file mode 100644 index 0000000000..b8c6c78279 --- /dev/null +++ b/plugins/GSdx/GSTexture10.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTexture.h" + +class GSTexture10 : public GSTexture +{ + CComPtr m_dev; + CComPtr m_texture; + D3D10_TEXTURE2D_DESC m_desc; + CComPtr m_srv; + CComPtr m_rtv; + CComPtr m_dsv; + +public: + GSTexture10(); + explicit GSTexture10(ID3D10Texture2D* texture); + virtual ~GSTexture10(); + + operator bool(); + + int GetType() const; + int GetWidth() const; + int GetHeight() const; + int GetFormat() const; + bool Update(const CRect& r, const void* data, int pitch); + bool Map(BYTE** bits, int& pitch, const RECT* r = NULL); + void Unmap(); + bool Save(CString fn, bool dds = false); + + ID3D10Texture2D* operator->(); // TODO: remove direct access + + operator ID3D10Texture2D*(); + operator ID3D10ShaderResourceView*(); + operator ID3D10RenderTargetView*(); + operator ID3D10DepthStencilView*(); +}; diff --git a/plugins/GSdx/GSTexture7.cpp b/plugins/GSdx/GSTexture7.cpp new file mode 100644 index 0000000000..dd3426494c --- /dev/null +++ b/plugins/GSdx/GSTexture7.cpp @@ -0,0 +1,184 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTexture7.h" + +GSTexture7::GSTexture7() + : m_type(GSTexture::None) +{ + memset(&m_desc, 0, sizeof(m_desc)); +} + +GSTexture7::GSTexture7(int type, IDirectDrawSurface7* system) + : m_type(type) + , m_system(system) +{ + memset(&m_desc, 0, sizeof(m_desc)); + + m_desc.dwSize = sizeof(m_desc); + + system->GetSurfaceDesc(&m_desc); +} + +GSTexture7::GSTexture7(int type, IDirectDrawSurface7* system, IDirectDrawSurface7* video) + : m_type(type) + , m_system(system) + , m_video(video) +{ + memset(&m_desc, 0, sizeof(m_desc)); + + m_desc.dwSize = sizeof(m_desc); + + video->GetSurfaceDesc(&m_desc); +} + +GSTexture7::~GSTexture7() +{ +} + +GSTexture7::operator bool() +{ + return !!m_system; +} + +int GSTexture7::GetType() const +{ + return m_type; +} + +int GSTexture7::GetWidth() const +{ + return m_desc.dwWidth; +} + +int GSTexture7::GetHeight() const +{ + return m_desc.dwHeight; +} + +int GSTexture7::GetFormat() const +{ + return (int)m_desc.ddpfPixelFormat.dwFourCC; +} + +bool GSTexture7::Update(const CRect& r, const void* data, int pitch) +{ + HRESULT hr; + + CRect r2 = r; + + DDSURFACEDESC2 desc; + + memset(&desc, 0, sizeof(desc)); + + desc.dwSize = sizeof(desc); + + if(SUCCEEDED(hr = m_system->Lock(&r2, &desc, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY, NULL))) + { + BYTE* src = (BYTE*)data; + BYTE* dst = (BYTE*)desc.lpSurface; + + int bytes = min(pitch, desc.lPitch); + + for(int i = 0, j = r.Height(); i < j; i++, src += pitch, dst += desc.lPitch) + { + // memcpy(dst, src, bytes); + + // HACK!!! + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + int w = bytes >> 4; + + for(int x = 0; x < w; x++) + { + GSVector4i c = s[x]; + + c = (c & 0xff00ff00) | ((c & 0x00ff0000) >> 16) | ((c & 0x000000ff) << 16); + + d[x] = c; + } + } + + hr = m_system->Unlock(&r2); + + if(m_video) + { + hr = m_video->Blt(&r2, m_system, &r2, DDBLT_WAIT, NULL); + } + + return true; + } + + return false; +} + +bool GSTexture7::Map(BYTE** bits, int& pitch, const RECT* r) +{ + HRESULT hr; + + CRect r2 = r; + + DDSURFACEDESC2 desc; + + if(SUCCEEDED(hr = m_system->Lock(&r2, &desc, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL))) + { + *bits = (BYTE*)desc.lpSurface; + pitch = (int)desc.lPitch; + + m_lr = r; + + return true; + } + + return false; +} + +void GSTexture7::Unmap() +{ + HRESULT hr; + + hr = m_system->Unlock(NULL); + + if(m_video) + { + hr = m_video->Blt(&m_lr, m_system, &m_lr, DDBLT_WAIT, NULL); + } +} + +bool GSTexture7::Save(CString fn, bool dds) +{ + // TODO + + return false; +} + +IDirectDrawSurface7* GSTexture7::operator->() +{ + return m_video ? m_video : m_system; +} + +GSTexture7::operator IDirectDrawSurface7*() +{ + return m_video ? m_video : m_system; +} diff --git a/plugins/GSdx/GSTexture7.h b/plugins/GSdx/GSTexture7.h new file mode 100644 index 0000000000..2c326dc028 --- /dev/null +++ b/plugins/GSdx/GSTexture7.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTexture.h" +#include + +class GSTexture7 : public GSTexture +{ + int m_type; + CComPtr m_system; + CComPtr m_video; + DDSURFACEDESC2 m_desc; + CRect m_lr; + +public: + GSTexture7(); + explicit GSTexture7(int type, IDirectDrawSurface7* system); + GSTexture7(int type, IDirectDrawSurface7* system, IDirectDrawSurface7* video); + virtual ~GSTexture7(); + + operator bool(); + + int GetType() const; + int GetWidth() const; + int GetHeight() const; + int GetFormat() const; + bool Update(const CRect& r, const void* data, int pitch); + bool Map(BYTE** bits, int& pitch, const RECT* r = NULL); + void Unmap(); + bool Save(CString fn, bool dds = false); + + IDirectDrawSurface7* operator->(); // TODO: remove direct access + + operator IDirectDrawSurface7*(); +}; diff --git a/plugins/GSdx/GSTexture9.cpp b/plugins/GSdx/GSTexture9.cpp new file mode 100644 index 0000000000..6c020bf902 --- /dev/null +++ b/plugins/GSdx/GSTexture9.cpp @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTexture9.h" + +GSTexture9::GSTexture9() +{ + memset(&m_desc, 0, sizeof(m_desc)); +} + +GSTexture9::GSTexture9(IDirect3DSurface9* surface) +{ + m_surface = surface; + + surface->GetDevice(&m_dev); + surface->GetDesc(&m_desc); + + if(m_desc.Type != D3DRTYPE_SURFACE) + { + HRESULT hr = surface->GetContainer(__uuidof(IDirect3DTexture9), (void**)&m_texture); + ASSERT(SUCCEEDED(hr)); + } +} + +GSTexture9::GSTexture9(IDirect3DTexture9* texture) +{ + m_texture = texture; + + texture->GetDevice(&m_dev); + texture->GetLevelDesc(0, &m_desc); + texture->GetSurfaceLevel(0, &m_surface); +} + +GSTexture9::~GSTexture9() +{ +} + +GSTexture9::operator bool() +{ + return !!m_surface; +} + +int GSTexture9::GetType() const +{ + if(m_desc.Usage & D3DUSAGE_RENDERTARGET) return GSTexture::RenderTarget; + if(m_desc.Usage & D3DUSAGE_DEPTHSTENCIL) return GSTexture::DepthStencil; + if(m_desc.Pool == D3DPOOL_MANAGED) return GSTexture::Texture; + if(m_desc.Pool == D3DPOOL_SYSTEMMEM) return GSTexture::Offscreen; + return GSTexture::None; +} + +int GSTexture9::GetWidth() const +{ + return m_desc.Width; +} + +int GSTexture9::GetHeight() const +{ + return m_desc.Height; +} + +int GSTexture9::GetFormat() const +{ + return m_desc.Format; +} + +bool GSTexture9::Update(const CRect& r, const void* data, int pitch) +{ + if(CComPtr surface = *this) + { + D3DLOCKED_RECT lr; + + if(SUCCEEDED(surface->LockRect(&lr, r, 0))) + { + BYTE* src = (BYTE*)data; + BYTE* dst = (BYTE*)lr.pBits; + + int bytes = min(pitch, lr.Pitch); + + for(int i = 0, j = r.Height(); i < j; i++, src += pitch, dst += lr.Pitch) + { + memcpy(dst, src, bytes); + } + + surface->UnlockRect(); + + return true; + } + } + + return false; +} + +bool GSTexture9::Map(BYTE** bits, int& pitch, const RECT* r) +{ + HRESULT hr; + + if(CComPtr surface = *this) + { + D3DLOCKED_RECT lr; + + if(SUCCEEDED(hr = surface->LockRect(&lr, r, 0))) + { + *bits = (BYTE*)lr.pBits; + pitch = (int)lr.Pitch; + + return true; + } + } + + return false; +} + +void GSTexture9::Unmap() +{ + if(CComPtr surface = *this) + { + surface->UnlockRect(); + } +} + +bool GSTexture9::Save(CString fn, bool dds) +{ + CComPtr res; + + if(m_desc.Usage & D3DUSAGE_DEPTHSTENCIL) + { + HRESULT hr; + + D3DSURFACE_DESC desc; + + m_surface->GetDesc(&desc); + + if(desc.Format != D3DFMT_D32F_LOCKABLE) + return false; + + CComPtr surface; + + hr = m_dev->CreateOffscreenPlainSurface(desc.Width, desc.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL); + + D3DLOCKED_RECT slr, dlr; + + hr = m_surface->LockRect(&slr, NULL, 0); + hr = surface->LockRect(&dlr, NULL, 0); + + BYTE* s = (BYTE*)slr.pBits; + BYTE* d = (BYTE*)dlr.pBits; + + for(UINT y = 0; y < desc.Height; y++, s += slr.Pitch, d += dlr.Pitch) + { + for(UINT x = 0; x < desc.Width; x++) + { + ((float*)d)[x] = ((float*)s)[x]; + } + } + + m_surface->UnlockRect(); + surface->UnlockRect(); + + res = surface; + } + else + { + res = m_surface; + } + + if(CComQIPtr surface = res) + { + return SUCCEEDED(D3DXSaveSurfaceToFile(fn, dds ? D3DXIFF_DDS : D3DXIFF_BMP, surface, NULL, NULL)); + } + + if(CComQIPtr texture = res) + { + return SUCCEEDED(D3DXSaveTextureToFile(fn, dds ? D3DXIFF_DDS : D3DXIFF_BMP, texture, NULL)); + } + + return false; +} + +IDirect3DTexture9* GSTexture9::operator->() +{ + return m_texture; +} + +GSTexture9::operator IDirect3DSurface9*() +{ + if(m_texture && !m_surface) + { + m_texture->GetSurfaceLevel(0, &m_surface); + } + + return m_surface; +} + +GSTexture9::operator IDirect3DTexture9*() +{ + if(m_surface && !m_texture) + { + m_surface->GetContainer(__uuidof(IDirect3DTexture9), (void**)&m_texture); + + ASSERT(m_texture); + } + + return m_texture; +} diff --git a/plugins/GSdx/GSTexture9.h b/plugins/GSdx/GSTexture9.h new file mode 100644 index 0000000000..eb7a2e8bf1 --- /dev/null +++ b/plugins/GSdx/GSTexture9.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTexture.h" + +class GSTexture9 : public GSTexture +{ + CComPtr m_dev; + CComPtr m_surface; + CComPtr m_texture; + D3DSURFACE_DESC m_desc; + +public: + GSTexture9(); + explicit GSTexture9(IDirect3DSurface9* surface); + explicit GSTexture9(IDirect3DTexture9* texture); + virtual ~GSTexture9(); + + operator bool(); + + int GetType() const; + int GetWidth() const; + int GetHeight() const; + int GetFormat() const; + bool Update(const CRect& r, const void* data, int pitch); + bool Map(BYTE** bits, int& pitch, const RECT* r = NULL); + void Unmap(); + bool Save(CString fn, bool dds = false); + + IDirect3DTexture9* operator->(); // TODO: remove direct access + + operator IDirect3DSurface9*(); + operator IDirect3DTexture9*(); +}; diff --git a/plugins/GSdx/GSTextureCache.cpp b/plugins/GSdx/GSTextureCache.cpp new file mode 100644 index 0000000000..89addcc544 --- /dev/null +++ b/plugins/GSdx/GSTextureCache.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTextureCache.h" diff --git a/plugins/GSdx/GSTextureCache.h b/plugins/GSdx/GSTextureCache.h new file mode 100644 index 0000000000..59e18bdd46 --- /dev/null +++ b/plugins/GSdx/GSTextureCache.h @@ -0,0 +1,816 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRenderer.h" + +template class GSTextureCache +{ + typedef typename Device::Texture Texture; + +public: + class GSSurface + { + protected: + GSRenderer* m_renderer; + + typedef typename Device::Texture Texture; + + public: + Texture m_texture; + Texture m_palette; + bool m_initpalette; + int m_age; + GSDirtyRectList m_dirty; + GIFRegTEX0 m_TEX0; + + explicit GSSurface(GSRenderer* renderer) + : m_renderer(renderer) + , m_age(0) + , m_initpalette(false) + { + m_TEX0.TBP0 = (UINT32)~0; + } + + virtual ~GSSurface() + { + m_renderer->m_dev.Recycle(m_texture); + m_renderer->m_dev.Recycle(m_palette); + } + + virtual void Update() + { + m_age = 0; + } + }; + + class GSRenderTarget : public GSSurface + { + public: + bool m_used; + + explicit GSRenderTarget(GSRenderer* renderer) + : GSSurface(renderer) + , m_used(true) + { + } + + virtual bool Create(int w, int h) + { + // FIXME: initial data should be unswizzled from local mem in Update() if dirty + + return m_renderer->m_dev.CreateRenderTarget(m_texture, w, h); + } + + virtual void Read(CRect r) = 0; + }; + + class GSDepthStencil : public GSSurface + { + public: + bool m_used; + + explicit GSDepthStencil(GSRenderer* renderer) + : GSSurface(renderer) + , m_used(false) + { + } + + virtual bool Create(int w, int h) + { + // FIXME: initial data should be unswizzled from local mem in Update() if dirty + + return m_renderer->m_dev.CreateDepthStencil(m_texture, w, h); + } + + }; + + class GSTexture : public GSSurface + { + protected: + bool GetDirtyRect(CRect& rr) + { + int w = 1 << m_TEX0.TW; + int h = 1 << m_TEX0.TH; + + CRect r(0, 0, w, h); + + POSITION pos = m_dirty.GetHeadPosition(); + + while(pos) + { + const CRect& dirty = m_dirty.GetNext(pos).GetDirtyRect(m_TEX0) & r; + + if(!(m_valid & dirty).IsRectEmpty()) + { + // find the rect having the largest area, outside dirty, inside m_valid + + CRect left(m_valid.left, m_valid.top, min(m_valid.right, dirty.left), m_valid.bottom); + CRect top(m_valid.left, m_valid.top, m_valid.right, min(m_valid.bottom, dirty.top)); + CRect right(max(m_valid.left, dirty.right), m_valid.top, m_valid.right, m_valid.bottom); + CRect bottom(m_valid.left, max(m_valid.top, dirty.bottom), m_valid.right, m_valid.bottom); + + int leftsize = !left.IsRectEmpty() ? left.Width() * left.Height() : 0; + int topsize = !top.IsRectEmpty() ? top.Width() * top.Height() : 0; + int rightsize = !right.IsRectEmpty() ? right.Width() * right.Height() : 0; + int bottomsize = !bottom.IsRectEmpty() ? bottom.Width() * bottom.Height() : 0; + + // TODO: sort + + m_valid = + leftsize > 0 ? left : + topsize > 0 ? top : + rightsize > 0 ? right : + bottomsize > 0 ? bottom : + CRect(0, 0, 0, 0); + } + } + + m_dirty.RemoveAll(); + + m_renderer->MinMaxUV(w, h, r); + + if(GSUtil::IsRectInRect(r, m_valid)) + { + return false; + } + else if(GSUtil::IsRectInRectH(r, m_valid) && (r.left >= m_valid.left || r.right <= m_valid.right)) + { + r.top = m_valid.top; + r.bottom = m_valid.bottom; + if(r.left < m_valid.left) r.right = m_valid.left; + else r.left = m_valid.right; // if(r.right > m_valid.right) + } + else if(GSUtil::IsRectInRectV(r, m_valid) && (r.top >= m_valid.top || r.bottom <= m_valid.bottom)) + { + r.left = m_valid.left; + r.right = m_valid.right; + if(r.top < m_valid.top) r.bottom = m_valid.top; + else r.top = m_valid.bottom; // if(r.bottom > m_valid.bottom) + } + else + { + r |= m_valid; + } + + if(r.IsRectEmpty()) + { + return false; + } + + rr = r; + + return true; + } + + public: + GIFRegCLAMP m_CLAMP; + DWORD* m_clut; // * + CRect m_valid; + int m_bpp; + int m_bpp2; + bool m_rendered; + + explicit GSTexture(GSRenderer* renderer) + : GSSurface(renderer) + , m_valid(0, 0, 0, 0) + , m_bpp(0) + , m_bpp2(0) + , m_rendered(false) + { + m_clut = (DWORD*)_aligned_malloc(256 * sizeof(DWORD), 16); + + memset(m_clut, 0, sizeof(m_clut)); + } + + ~GSTexture() + { + _aligned_free(m_clut); + } + + virtual bool Create() = 0; + virtual bool Create(GSRenderTarget* rt) = 0; + virtual bool Create(GSDepthStencil* ds) = 0; + }; + +protected: + GSRenderer* m_renderer; + CAtlList m_rt; + CAtlList m_ds; + CAtlList m_tex; + + template void RecycleByAge(CAtlList& l, int maxage = 60) + { + POSITION pos = l.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + T* t = l.GetNext(pos); + + if(++t->m_age > maxage) + { + l.RemoveAt(cur); + + delete t; + } + } + } + + virtual GSRenderTarget* CreateRenderTarget() = 0; + virtual GSDepthStencil* CreateDepthStencil() = 0; + virtual GSTexture* CreateTexture() = 0; + +public: + GSTextureCache(GSRenderer* renderer) + : m_renderer(renderer) + { + } + + virtual ~GSTextureCache() + { + RemoveAll(); + } + + void RemoveAll() + { + while(m_rt.GetCount()) delete m_rt.RemoveHead(); + while(m_ds.GetCount()) delete m_ds.RemoveHead(); + while(m_tex.GetCount()) delete m_tex.RemoveHead(); + } + + GSRenderTarget* GetRenderTarget(const GIFRegTEX0& TEX0, int w, int h, bool fb = false) + { + GSRenderTarget* rt = NULL; + + if(rt == NULL) + { + for(POSITION pos = m_rt.GetHeadPosition(); pos; m_rt.GetNext(pos)) + { + GSRenderTarget* rt2 = m_rt.GetAt(pos); + + if(rt2->m_TEX0.TBP0 == TEX0.TBP0) + { + m_rt.MoveToHead(pos); + + rt = rt2; + + if(!fb) rt->m_TEX0 = TEX0; + + rt->Update(); + + break; + } + } + } + + if(rt == NULL && fb) + { + // HACK: try to find something close to the base pointer + + for(POSITION pos = m_rt.GetHeadPosition(); pos; m_rt.GetNext(pos)) + { + GSRenderTarget* rt2 = m_rt.GetAt(pos); + + if(rt2->m_TEX0.TBP0 <= TEX0.TBP0 && TEX0.TBP0 < rt2->m_TEX0.TBP0 + 0x700 && (!rt || rt2->m_TEX0.TBP0 >= rt->m_TEX0.TBP0)) + { + rt = rt2; + } + } + + if(rt) + { + rt->Update(); + } + } + + if(rt == NULL) + { + rt = CreateRenderTarget(); + + rt->m_TEX0 = TEX0; + + if(!rt->Create(w, h)) + { + delete rt; + + return NULL; + } + + m_rt.AddHead(rt); + } + + if(m_renderer->CanUpscale()) + { + int ww = (int)(m_renderer->GetFramePos().cx + rt->m_TEX0.TBW * 64); + int hh = (int)(m_renderer->GetFramePos().cy + m_renderer->GetDisplaySize().cy); + + if(hh <= m_renderer->GetDeviceSize().cy / 2) + { + hh *= 2; + } + + if(ww > 0 && hh > 0) + { + rt->m_texture.m_scale.x = (float)w / ww; + rt->m_texture.m_scale.y = (float)h / hh; + } + } + + if(!fb) + { + rt->m_used = true; + } + + return rt; + } + + GSDepthStencil* GetDepthStencil(const GIFRegTEX0& TEX0, int w, int h) + { + GSDepthStencil* ds = NULL; + + if(ds == NULL) + { + for(POSITION pos = m_ds.GetHeadPosition(); pos; m_ds.GetNext(pos)) + { + GSDepthStencil* ds2 = m_ds.GetAt(pos); + + if(ds2->m_TEX0.TBP0 == TEX0.TBP0) + { + m_ds.MoveToHead(pos); + + ds = ds2; + + ds->m_TEX0 = TEX0; + + ds->Update(); + + break; + } + } + } + + if(ds == NULL) + { + ds = CreateDepthStencil(); + + ds->m_TEX0 = TEX0; + + if(!ds->Create(w, h)) + { + delete ds; + + return NULL; + } + + m_ds.AddHead(ds); + } + + if(m_renderer->m_context->DepthWrite()) + { + ds->m_used = true; + } + + return ds; + } + + GSTexture* GetTexture() + { + const GIFRegTEX0& TEX0 = m_renderer->m_context->TEX0; + const GIFRegCLAMP& CLAMP = m_renderer->m_context->CLAMP; + + const DWORD* clut = m_renderer->m_mem.m_clut; + const int pal = GSLocalMemory::m_psm[TEX0.PSM].pal; + + if(pal > 0) + { + m_renderer->m_mem.m_clut.Read(TEX0); + + /* + POSITION pos = m_tex.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSSurface* s = m_tex.GetNext(pos); + + if(s->m_TEX0.TBP0 == TEX0.CBP) + { + m_tex.RemoveAt(cur); + + delete s; + } + } + + pos = m_rt.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSSurface* s = m_rt.GetNext(pos); + + if(s->m_TEX0.TBP0 == TEX0.CBP) + { + m_rt.RemoveAt(cur); + + delete s; + } + } + + pos = m_ds.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSSurface* s = m_ds.GetNext(pos); + + if(s->m_TEX0.TBP0 == TEX0.CBP) + { + m_ds.RemoveAt(cur); + + delete s; + } + } + */ + } + + GSTexture* t = NULL; + + for(POSITION pos = m_tex.GetHeadPosition(); pos; m_tex.GetNext(pos)) + { + t = m_tex.GetAt(pos); + + if(GSUtil::HasSharedBits(t->m_TEX0.TBP0, t->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM)) + { + if(TEX0.PSM == t->m_TEX0.PSM && TEX0.TBW == t->m_TEX0.TBW + && TEX0.TW == t->m_TEX0.TW && TEX0.TH == t->m_TEX0.TH + && (m_renderer->m_psrr || (CLAMP.WMS != 3 && t->m_CLAMP.WMS != 3 && CLAMP.WMT != 3 && t->m_CLAMP.WMT != 3 || CLAMP.i64 == t->m_CLAMP.i64)) + && (pal == 0 || TEX0.CPSM == t->m_TEX0.CPSM && GSVector4i::compare(t->m_clut, clut, pal * sizeof(clut[0])))) + { + m_tex.MoveToHead(pos); + + break; + } + } + + t = NULL; + } + + if(t == NULL) + { + for(POSITION pos = m_rt.GetHeadPosition(); pos; m_rt.GetNext(pos)) + { + GSRenderTarget* rt = m_rt.GetAt(pos); + + if(rt->m_dirty.IsEmpty() && GSUtil::HasSharedBits(rt->m_TEX0.TBP0, rt->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM)) + { + t = CreateTexture(); + + if(!t->Create(rt)) + { + delete t; + + return NULL; + } + + m_tex.AddHead(t); + + break; + } + } + } + + if(t == NULL) + { + for(POSITION pos = m_ds.GetHeadPosition(); pos; m_ds.GetNext(pos)) + { + GSDepthStencil* ds = m_ds.GetAt(pos); + + if(ds->m_dirty.IsEmpty() && ds->m_used && GSUtil::HasSharedBits(ds->m_TEX0.TBP0, ds->m_TEX0.PSM, TEX0.TBP0, TEX0.PSM)) + { + t = CreateTexture(); + + if(!t->Create(ds)) + { + delete t; + + return NULL; + } + + m_tex.AddHead(t); + + break; + } + } + } + + if(t == NULL) + { + t = CreateTexture(); + + if(!t->Create()) + { + delete t; + + return NULL; + } + + m_tex.AddHead(t); + } + + if(pal > 0) + { + int size = pal * sizeof(clut[0]); + + if(t->m_palette) + { + if(t->m_initpalette) + { + memcpy(t->m_clut, clut, size); + t->m_palette.Update(CRect(0, 0, pal, 1), t->m_clut, size); + t->m_initpalette = false; + } + else + { + if(GSVector4i::update(t->m_clut, clut, size)) + { + t->m_palette.Update(CRect(0, 0, pal, 1), t->m_clut, size); + } + } + } + else + { + memcpy(t->m_clut, clut, size); + } + } + + t->Update(); + + return t; + } + + void InvalidateTextures(const GIFRegFRAME& FRAME, const GIFRegZBUF& ZBUF) + { + POSITION pos = m_tex.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSTexture* t = m_tex.GetNext(pos); + + if(GSUtil::HasSharedBits(FRAME.Block(), FRAME.PSM, t->m_TEX0.TBP0, t->m_TEX0.PSM) + || GSUtil::HasSharedBits(ZBUF.Block(), ZBUF.PSM, t->m_TEX0.TBP0, t->m_TEX0.PSM)) + { + m_tex.RemoveAt(cur); + + delete t; + } + } + } + + void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, const CRect& r) + { + bool found = false; + + POSITION pos = m_tex.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSTexture* t = m_tex.GetNext(pos); + + if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, t->m_TEX0.TBP0, t->m_TEX0.PSM)) + { + if(BITBLTBUF.DBW == t->m_TEX0.TBW && !t->m_rendered) + { + t->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, r)); + + found = true; + } + else + { + m_tex.RemoveAt(cur); + + delete t; + } + } + else if(GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, t->m_TEX0.PSM)) + { + if(BITBLTBUF.DBW == t->m_TEX0.TBW && !t->m_rendered) + { + int rowsize = (int)BITBLTBUF.DBW * 8192; + int offset = ((int)BITBLTBUF.DBP - (int)t->m_TEX0.TBP0) * 256; + + if(rowsize > 0 && offset % rowsize == 0) + { + int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.cy * offset / rowsize; + + CRect r2(r.left, r.top + y, r.right, r.bottom + y); + + int w = 1 << t->m_TEX0.TW; + int h = 1 << t->m_TEX0.TH; + + if(r2.bottom > 0 && r2.top < h && r2.right > 0 && r2.left < w) + { + t->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, r2)); + } + } + } + } + } + + pos = m_rt.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSRenderTarget* rt = m_rt.GetNext(pos); + + if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, rt->m_TEX0.TBP0, rt->m_TEX0.PSM)) + { + if(!found && GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, rt->m_TEX0.PSM)) + { + rt->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, r)); + rt->m_TEX0.TBW = BITBLTBUF.DBW; + } + else + { + m_rt.RemoveAt(cur); + delete rt; + continue; + } + } + + if(GSUtil::HasSharedBits(BITBLTBUF.DPSM, rt->m_TEX0.PSM) && BITBLTBUF.DBP < rt->m_TEX0.TBP0) + { + DWORD rowsize = BITBLTBUF.DBW * 8192; + DWORD offset = (DWORD)((rt->m_TEX0.TBP0 - BITBLTBUF.DBP) * 256); + + if(rowsize > 0 && offset % rowsize == 0) + { + int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.cy * offset / rowsize; + + if(r.bottom >= y) + { + // TODO: do not add this rect above too + rt->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, CRect(r.left, r.top - y, r.right, r.bottom - y))); + rt->m_TEX0.TBW = BITBLTBUF.DBW; + continue; + } + } + } + } + + // copypaste for ds + + pos = m_ds.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSDepthStencil* ds = m_ds.GetNext(pos); + + if(GSUtil::HasSharedBits(BITBLTBUF.DBP, BITBLTBUF.DPSM, ds->m_TEX0.TBP0, ds->m_TEX0.PSM)) + { + if(!found && GSUtil::HasCompatibleBits(BITBLTBUF.DPSM, ds->m_TEX0.PSM)) + { + ds->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, r)); + ds->m_TEX0.TBW = BITBLTBUF.DBW; + } + else + { + m_ds.RemoveAt(cur); + delete ds; + continue; + } + } + + if(GSUtil::HasSharedBits(BITBLTBUF.DPSM, ds->m_TEX0.PSM) && BITBLTBUF.DBP < ds->m_TEX0.TBP0) + { + DWORD rowsize = BITBLTBUF.DBW * 8192; + DWORD offset = (DWORD)((ds->m_TEX0.TBP0 - BITBLTBUF.DBP) * 256); + + if(rowsize > 0 && offset % rowsize == 0) + { + int y = m_renderer->m_mem.m_psm[BITBLTBUF.DPSM].pgs.cy * offset / rowsize; + + if(r.bottom >= y) + { + // TODO: do not add this rect above too + ds->m_dirty.AddTail(GSDirtyRect(BITBLTBUF.DPSM, CRect(r.left, r.top - y, r.right, r.bottom - y))); + ds->m_TEX0.TBW = BITBLTBUF.DBW; + continue; + } + } + } + + } + } + + void InvalidateLocalMem(const GIFRegBITBLTBUF& BITBLTBUF, const CRect& r) + { + POSITION pos = m_rt.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSRenderTarget* rt = m_rt.GetNext(pos); + + if(GSUtil::HasSharedBits(BITBLTBUF.SBP, BITBLTBUF.SPSM, rt->m_TEX0.TBP0, rt->m_TEX0.PSM)) + { + if(GSUtil::HasCompatibleBits(BITBLTBUF.SPSM, rt->m_TEX0.PSM)) + { + rt->Read(r); + return; + } + else if(BITBLTBUF.SPSM == PSM_PSMCT32 && (rt->m_TEX0.PSM == PSM_PSMCT16 || rt->m_TEX0.PSM == PSM_PSMCT16S)) + { + // ffx-2 riku changing to her default (shoots some reflecting glass at the end), 16-bit rt read as 32-bit + + rt->Read(CRect(r.left, r.top, r.right, r.top + (r.bottom - r.top) * 2)); + return; + } + else + { + m_rt.RemoveAt(cur); + delete rt; + continue; + } + } + } +/* + // no good, ffx does a lot of readback after exiting menu, at 0x02f00 this wrongly finds rt 0x02100 (0,448 - 512,480) + + GSRenderTarget* rt2 = NULL; + int ymin = INT_MAX; + + pos = m_rt.GetHeadPosition(); + + while(pos) + { + GSRenderTarget* rt = m_rt.GetNext(pos); + + if(HasSharedBits(BITBLTBUF.SPSM, rt->m_TEX0.PSM) && BITBLTBUF.SBP > rt->m_TEX0.TBP0) + { + // ffx2 pause screen background + + DWORD rowsize = BITBLTBUF.SBW * 8192; + DWORD offset = (DWORD)((BITBLTBUF.SBP - rt->m_TEX0.TBP0) * 256); + + if(rowsize > 0 && offset % rowsize == 0) + { + int y = m_renderer->m_mem.m_psm[BITBLTBUF.SPSM].pgs.cy * offset / rowsize; + + if(y < ymin && y < 512) + { + rt2 = rt; + ymin = y; + } + } + } + } + + if(rt2) + { + rt2->Read(CRect(r.left, r.top + ymin, r.right, r.bottom + ymin)); + } + + // TODO: ds +*/ + } + + void IncAge() + { + RecycleByAge(m_tex, 2); + RecycleByAge(m_rt); + RecycleByAge(m_ds); + } +}; diff --git a/plugins/GSdx/GSTextureCache10.cpp b/plugins/GSdx/GSTextureCache10.cpp new file mode 100644 index 0000000000..025478d108 --- /dev/null +++ b/plugins/GSdx/GSTextureCache10.cpp @@ -0,0 +1,452 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTextureCache10.h" + +// GSTextureCache10 + +GSTextureCache10::GSTextureCache10(GSRenderer* renderer) + : GSTextureCache(renderer) +{ +} + +// GSRenderTargetHW10 + +void GSTextureCache10::GSRenderTargetHW10::Update() +{ + __super::Update(); + + // FIXME: the union of the rects may also update wrong parts of the render target (but a lot faster :) + + CRect r = m_dirty.GetDirtyRect(m_TEX0, m_texture.GetSize()); + + m_dirty.RemoveAll(); + + if(r.IsRectEmpty()) return; + + int w = r.Width(); + int h = r.Height(); + + static BYTE* buff = (BYTE*)_aligned_malloc(1024 * 1024 * 4, 16); + static int pitch = 1024 * 4; + + GIFRegTEXA TEXA; + + TEXA.AEM = 1; + TEXA.TA0 = 0; + TEXA.TA1 = 0x80; + + GIFRegCLAMP CLAMP; + + CLAMP.WMS = 0; + CLAMP.WMT = 0; + + m_renderer->m_mem.ReadTexture(r, buff, pitch, m_TEX0, TEXA, CLAMP); + + // s->m_perfmon.Put(GSPerfMon::Unswizzle, w * h * 4); + + Texture texture; + + if(!m_renderer->m_dev.CreateTexture(texture, w, h)) + return; + + texture.Update(CRect(0, 0, w, h), buff, pitch); + + GSVector4 dr( + m_texture.m_scale.x * r.left, + m_texture.m_scale.y * r.top, + m_texture.m_scale.x * r.right, + m_texture.m_scale.y * r.bottom); + + m_renderer->m_dev.StretchRect(texture, m_texture, dr); + + m_renderer->m_dev.Recycle(texture); +} + +void GSTextureCache10::GSRenderTargetHW10::Read(CRect r) +{ + if(m_TEX0.PSM != PSM_PSMCT32 + && m_TEX0.PSM != PSM_PSMCT24 + && m_TEX0.PSM != PSM_PSMCT16 + && m_TEX0.PSM != PSM_PSMCT16S) + { + //ASSERT(0); + return; + } + + if(!m_dirty.IsEmpty()) + { + return; + } + + TRACE(_T("GSRenderTarget::Read %d,%d - %d,%d (%08x)\n"), r.left, r.top, r.right, r.bottom, m_TEX0.TBP0); + + // m_renderer->m_perfmon.Put(GSPerfMon::ReadRT, 1); + + int w = r.Width(); + int h = r.Height(); + + GSVector4 src; + + src.x = m_texture.m_scale.x * r.left / m_texture.GetWidth(); + src.y = m_texture.m_scale.y * r.top / m_texture.GetHeight(); + src.z = m_texture.m_scale.x * r.right / m_texture.GetWidth(); + src.w = m_texture.m_scale.y * r.bottom / m_texture.GetHeight(); + + DXGI_FORMAT format = m_TEX0.PSM == PSM_PSMCT16 || m_TEX0.PSM == PSM_PSMCT16S ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R8G8B8A8_UNORM; + + Texture offscreen; + + if(!m_renderer->m_dev.CopyOffscreen(m_texture, src, offscreen, w, h, format)) + return; + + BYTE* bits; + int pitch; + + if(offscreen.Map(&bits, pitch)) + { + // TODO: block level write + + DWORD bp = m_TEX0.TBP0; + DWORD bw = m_TEX0.TBW; + + GSLocalMemory::pixelAddress pa = GSLocalMemory::m_psm[m_TEX0.PSM].pa; + + if(m_TEX0.PSM == PSM_PSMCT32) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WritePixel32(addr + offset[x], ((DWORD*)bits)[i]); + } + } + } + else if(m_TEX0.PSM == PSM_PSMCT24) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WritePixel24(addr + offset[x], ((DWORD*)bits)[i]); + } + } + } + else if(m_TEX0.PSM == PSM_PSMCT16 || m_TEX0.PSM == PSM_PSMCT16S) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WritePixel16(addr + offset[x], ((WORD*)bits)[i]); + } + } + } + else + { + ASSERT(0); + } + + offscreen.Unmap(); + } + + m_renderer->m_dev.Recycle(offscreen); +} + +// GSDepthStencilHW10 + +void GSTextureCache10::GSDepthStencilHW10::Update() +{ + __super::Update(); + + // TODO +} + +// GSTextureHW10 + +bool GSTextureCache10::GSTextureHW10::Create() +{ + // m_renderer->m_perfmon.Put(GSPerfMon::WriteTexture, 1); + + m_TEX0 = m_renderer->m_context->TEX0; + m_CLAMP = m_renderer->m_context->CLAMP; + + DWORD psm = m_TEX0.PSM; + + switch(psm) + { + case PSM_PSMT8: + case PSM_PSMT8H: + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + psm = m_TEX0.CPSM; + break; + } + + DXGI_FORMAT format; + + switch(psm) + { + default: + TRACE(_T("Invalid TEX0.PSM/CPSM (%I64d, %I64d)\n"), m_TEX0.PSM, m_TEX0.CPSM); + case PSM_PSMCT32: + m_bpp = 32; + m_bpp2 = 0; + format = DXGI_FORMAT_R8G8B8A8_UNORM; + break; + case PSM_PSMCT24: + m_bpp = 32; + m_bpp2 = 1; + format = DXGI_FORMAT_R8G8B8A8_UNORM; + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + m_bpp = 16; + m_bpp2 = 5; + format = DXGI_FORMAT_R16_UNORM; + break; + } + + int w = 1 << m_TEX0.TW; + int h = 1 << m_TEX0.TH; + + return m_renderer->m_dev.CreateTexture(m_texture, w, h, format); +} + +bool GSTextureCache10::GSTextureHW10::Create(GSRenderTarget* rt) +{ + rt->Update(); + + // m_renderer->m_perfmon.Put(GSPerfMon::ConvertRT2T, 1); + + m_TEX0 = m_renderer->m_context->TEX0; + m_CLAMP = m_renderer->m_context->CLAMP; + m_rendered = true; + + int tw = 1 << m_TEX0.TW; + int th = 1 << m_TEX0.TH; + int tp = (int)m_TEX0.TW << 6; + + // do not round here!!! if edge becomes a black pixel and addressing mode is clamp => everything outside the clamped area turns into black (kh2 shadows) + + int w = (int)(rt->m_texture.m_scale.x * tw); + int h = (int)(rt->m_texture.m_scale.y * th); + + // pitch conversion + + if(rt->m_TEX0.TBW != m_TEX0.TBW) // && rt->m_TEX0.PSM == m_TEX0.PSM + { + // sfex3 uses this trick (bw: 10 -> 5, wraps the right side below the left) + + // ASSERT(rt->m_TEX0.TBW > m_TEX0.TBW); // otherwise scale.x need to be reduced to make the larger texture fit (TODO) + + m_renderer->m_dev.CreateRenderTarget(m_texture, rt->m_texture.GetWidth(), rt->m_texture.GetHeight()); + + int bw = 64; + int bh = m_TEX0.PSM == PSM_PSMCT32 || m_TEX0.PSM == PSM_PSMCT24 ? 32 : 64; + + int sw = (int)rt->m_TEX0.TBW << 6; + + int dw = (int)m_TEX0.TBW << 6; + int dh = 1 << m_TEX0.TH; + + if(sw != 0) + for(int dy = 0; dy < dh; dy += bh) + { + for(int dx = 0; dx < dw; dx += bw) + { + int o = dy * dw / bh + dx; + + int sx = o % sw; + int sy = o / sw; + + GSVector4 src, dst; + + src.x = rt->m_texture.m_scale.x * sx / rt->m_texture.GetWidth(); + src.y = rt->m_texture.m_scale.y * sy / rt->m_texture.GetHeight(); + src.z = rt->m_texture.m_scale.x * (sx + bw) / rt->m_texture.GetWidth(); + src.w = rt->m_texture.m_scale.y * (sy + bh) / rt->m_texture.GetHeight(); + + dst.x = rt->m_texture.m_scale.x * dx; + dst.y = rt->m_texture.m_scale.y * dy; + dst.z = rt->m_texture.m_scale.x * (dx + bw); + dst.w = rt->m_texture.m_scale.y * (dy + bh); + + m_renderer->m_dev.StretchRect(rt->m_texture, src, m_texture, dst); + + // TODO: this is quite a lot of StretchRect, do it with one Draw + } + } + } + else if(tw < tp) + { + // FIXME: timesplitters blurs the render target by blending itself over a couple of times + + if(tw == 256 && th == 128 && tp == 512 && (m_TEX0.TBP0 == 0 || m_TEX0.TBP0 == 0x00e00)) + { + return false; + } + } + + // width/height conversion + + GSVector2 scale = rt->m_texture.m_scale; + + GSVector4 dst(0, 0, w, h); + + if(w > rt->m_texture.GetWidth()) + { + scale.x = (float)rt->m_texture.GetWidth() / tw; + dst.z = (float)rt->m_texture.GetWidth() * scale.x / rt->m_texture.m_scale.x; + w = rt->m_texture.GetWidth(); + } + + if(h > rt->m_texture.GetHeight()) + { + scale.y = (float)rt->m_texture.GetHeight() / th; + dst.w = (float)rt->m_texture.GetHeight() * scale.y / rt->m_texture.m_scale.y; + h = rt->m_texture.GetHeight(); + } + + GSVector4 src(0, 0, w, h); + + Texture* st; + Texture* dt; + Texture tmp; + + if(!m_texture) + { + st = &rt->m_texture; + dt = &m_texture; + } + else + { + st = &m_texture; + dt = &tmp; + } + + m_renderer->m_dev.CreateRenderTarget(*dt, w, h); + + if(src.x == dst.x && src.y == dst.y && src.z == dst.z && src.w == dst.w) + { + D3D10_BOX box = {0, 0, 0, w, h, 1}; + + m_renderer->m_dev->CopySubresourceRegion(*dt, 0, 0, 0, 0, *st, 0, &box); + } + else + { + src.z /= st->GetWidth(); + src.w /= st->GetHeight(); + + m_renderer->m_dev.StretchRect(*st, src, *dt, dst); + } + + if(tmp) + { + m_renderer->m_dev.Recycle(m_texture); + + m_texture = tmp; + } + + m_texture.m_scale = scale; + + switch(m_TEX0.PSM) + { + case PSM_PSMCT32: + m_bpp2 = 0; + break; + case PSM_PSMCT24: + m_bpp2 = 1; + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + m_bpp2 = 2; + break; + case PSM_PSMT8H: + m_bpp2 = 3; + m_renderer->m_dev.CreateTexture(m_palette, 256, 1, m_TEX0.CPSM == PSM_PSMCT32 ? DXGI_FORMAT_R8G8B8A8_UNORM : DXGI_FORMAT_R16_UNORM); // + m_initpalette = true; + break; + case PSM_PSMT4HL: + case PSM_PSMT4HH: + ASSERT(0); // TODO + break; + } + + return true; +} + +bool GSTextureCache10::GSTextureHW10::Create(GSDepthStencil* ds) +{ + m_rendered = true; + + // TODO + + return false; +} + +void GSTextureCache10::GSTextureHW10::Update() +{ + __super::Update(); + + if(m_rendered) + { + return; + } + + CRect r; + + if(!GetDirtyRect(r)) + { + return; + } + + m_valid |= r; + + //TRACE(_T("GSTexture::Update %d,%d - %d,%d (%08x)\n"), r.left, r.top, r.right, r.bottom, m_TEX0.TBP0); + + static BYTE* bits = (BYTE*)::_aligned_malloc(1024 * 1024 * 4, 16); + int pitch = 1024 * 4; + + if(m_renderer->m_psrr) + { + m_renderer->m_mem.ReadTextureNPNC(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP); + } + else + { + m_renderer->m_mem.ReadTextureNP(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP); + } + + m_texture.Update(r, bits, pitch); + + m_renderer->m_perfmon.Put(GSPerfMon::Unswizzle, r.Width() * r.Height() * m_bpp >> 3); +} diff --git a/plugins/GSdx/GSTextureCache10.h b/plugins/GSdx/GSTextureCache10.h new file mode 100644 index 0000000000..f236263516 --- /dev/null +++ b/plugins/GSdx/GSTextureCache10.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTextureCache.h" +#include "GSDevice10.h" + +class GSTextureCache10 : public GSTextureCache +{ + typedef GSDevice10::Texture Texture; + + class GSRenderTargetHW10 : public GSRenderTarget + { + public: + explicit GSRenderTargetHW10(GSRenderer* renderer) : GSRenderTarget(renderer) {} + + void Update(); + void Read(CRect r); + }; + + class GSDepthStencilHW10 : public GSDepthStencil + { + public: + explicit GSDepthStencilHW10(GSRenderer* renderer) : GSDepthStencil(renderer) {} + + void Update(); + }; + + class GSTextureHW10 : public GSTexture + { + public: + explicit GSTextureHW10(GSRenderer* renderer) : GSTexture(renderer) {} + + bool Create(); + bool Create(GSRenderTarget* rt); + bool Create(GSDepthStencil* ds); + void Update(); + }; + +protected: + GSRenderTarget* CreateRenderTarget() {return new GSRenderTargetHW10(m_renderer);} + GSDepthStencil* CreateDepthStencil() {return new GSDepthStencilHW10(m_renderer);} + GSTexture* CreateTexture() {return new GSTextureHW10(m_renderer);} + +public: + GSTextureCache10(GSRenderer* renderer); +}; diff --git a/plugins/GSdx/GSTextureCache9.cpp b/plugins/GSdx/GSTextureCache9.cpp new file mode 100644 index 0000000000..073462926e --- /dev/null +++ b/plugins/GSdx/GSTextureCache9.cpp @@ -0,0 +1,453 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTextureCache9.h" + +// GSTextureCache9 + +GSTextureCache9::GSTextureCache9(GSRenderer* renderer) + : GSTextureCache(renderer) +{ +} + +// GSRenderTarget9 + +void GSTextureCache9::GSRenderTarget9::Update() +{ + __super::Update(); + + // FIXME: the union of the rects may also update wrong parts of the render target (but a lot faster :) + + CRect r = m_dirty.GetDirtyRect(m_TEX0, m_texture.GetSize()); + + m_dirty.RemoveAll(); + + if(r.IsRectEmpty()) return; + + int w = r.Width(); + int h = r.Height(); + + Texture texture; + + if(!m_renderer->m_dev.CreateTexture(texture, w, h)) + return; + + BYTE* bits; + int pitch; + + if(texture.Map(&bits, pitch)) + { + GIFRegTEXA TEXA; + + TEXA.AEM = 1; + TEXA.TA0 = 0; + TEXA.TA1 = 0x80; + + GIFRegCLAMP CLAMP; + + CLAMP.WMS = 0; + CLAMP.WMT = 0; + + m_renderer->m_mem.ReadTexture(r, bits, pitch, m_TEX0, TEXA, CLAMP); + + texture.Unmap(); + + // m_renderer->m_perfmon.Put(GSPerfMon::Unswizzle, r.Width() * r.Height() * 4); + + GSVector4 dr( + m_texture.m_scale.x * r.left, + m_texture.m_scale.y * r.top, + m_texture.m_scale.x * r.right, + m_texture.m_scale.y * r.bottom); + + m_renderer->m_dev.StretchRect(texture, m_texture, dr); + } + + m_renderer->m_dev.Recycle(texture); +} + +void GSTextureCache9::GSRenderTarget9::Read(CRect r) +{ + if(m_TEX0.PSM != PSM_PSMCT32 + && m_TEX0.PSM != PSM_PSMCT24 + && m_TEX0.PSM != PSM_PSMCT16 + && m_TEX0.PSM != PSM_PSMCT16S) + { + //ASSERT(0); + return; + } + + if(!m_dirty.IsEmpty()) + { + return; + } + + TRACE(_T("GSRenderTarget::Read %d,%d - %d,%d (%08x)\n"), r.left, r.top, r.right, r.bottom, m_TEX0.TBP0); + + // m_renderer->m_perfmon.Put(GSPerfMon::ReadRT, 1); + + int w = r.Width(); + int h = r.Height(); + + GSVector4 src; + + src.x = m_texture.m_scale.x * r.left / m_texture.GetWidth(); + src.y = m_texture.m_scale.y * r.top / m_texture.GetHeight(); + src.z = m_texture.m_scale.x * r.right / m_texture.GetWidth(); + src.w = m_texture.m_scale.y * r.bottom / m_texture.GetHeight(); + + Texture offscreen; + + if(!m_renderer->m_dev.CopyOffscreen(m_texture, src, offscreen, w, h)) + return; + + BYTE* bits; + int pitch; + + if(offscreen.Map(&bits, pitch)) + { + // TODO: block level write + + DWORD bp = m_TEX0.TBP0; + DWORD bw = m_TEX0.TBW; + + GSLocalMemory::pixelAddress pa = GSLocalMemory::m_psm[m_TEX0.PSM].pa; + + if(m_TEX0.PSM == PSM_PSMCT32) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WritePixel32(addr + offset[x], ((DWORD*)bits)[i]); + } + } + } + else if(m_TEX0.PSM == PSM_PSMCT24) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WritePixel24(addr + offset[x], ((DWORD*)bits)[i]); + } + } + } + else if(m_TEX0.PSM == PSM_PSMCT16 || m_TEX0.PSM == PSM_PSMCT16S) + { + for(int y = r.top; y < r.bottom; y++, bits += pitch) + { + DWORD addr = pa(0, y, bp, bw); + int* offset = GSLocalMemory::m_psm[m_TEX0.PSM].rowOffset[y & 7]; + + for(int x = r.left, i = 0; x < r.right; x++, i++) + { + m_renderer->m_mem.WriteFrame16(addr + offset[x], ((DWORD*)bits)[i]); + } + } + } + else + { + ASSERT(0); + } + + offscreen.Unmap(); + } + + m_renderer->m_dev.Recycle(offscreen); +} + +// GSDepthStencil9 + +void GSTextureCache9::GSDepthStencil9::Update() +{ + __super::Update(); + + // TODO +} + +// GSTexture9 + +bool GSTextureCache9::GSTexture9::Create() +{ + // m_renderer->m_perfmon.Put(GSPerfMon::WriteTexture, 1); + + m_TEX0 = m_renderer->m_context->TEX0; + m_CLAMP = m_renderer->m_context->CLAMP; + + DWORD psm = m_TEX0.PSM; + + switch(psm) + { + case PSM_PSMT8: + case PSM_PSMT8H: + case PSM_PSMT4: + case PSM_PSMT4HL: + case PSM_PSMT4HH: + psm = m_TEX0.CPSM; + break; + } + + D3DFORMAT format; + + switch(psm) + { + default: + TRACE(_T("Invalid TEX0.PSM/CPSM (%I64d, %I64d)\n"), m_TEX0.PSM, m_TEX0.CPSM); + case PSM_PSMCT32: + m_bpp = 32; + m_bpp2 = 0; + format = D3DFMT_A8R8G8B8; + break; + case PSM_PSMCT24: + m_bpp = 32; + m_bpp2 = 1; + format = D3DFMT_A8R8G8B8; + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + m_bpp = 16; + m_bpp2 = 2; + format = D3DFMT_A1R5G5B5; + break; + } + + int w = 1 << m_TEX0.TW; + int h = 1 << m_TEX0.TH; + + return m_renderer->m_dev.CreateTexture(m_texture, w, h, format); +} + +bool GSTextureCache9::GSTexture9::Create(GSRenderTarget* rt) +{ + rt->Update(); + + // m_renderer->m_perfmon.Put(GSPerfMon::ConvertRT2T, 1); + + m_TEX0 = m_renderer->m_context->TEX0; + m_CLAMP = m_renderer->m_context->CLAMP; + m_rendered = true; + + int tw = 1 << m_TEX0.TW; + int th = 1 << m_TEX0.TH; + int tp = (int)m_TEX0.TW << 6; + + // do not round here!!! if edge becomes a black pixel and addressing mode is clamp => everything outside the clamped area turns into black (kh2 shadows) + + int w = (int)(rt->m_texture.m_scale.x * tw); + int h = (int)(rt->m_texture.m_scale.y * th); + + // pitch conversion + + if(rt->m_TEX0.TBW != m_TEX0.TBW) // && rt->m_TEX0.PSM == m_TEX0.PSM + { + // sfex3 uses this trick (bw: 10 -> 5, wraps the right side below the left) + + // ASSERT(rt->m_TEX0.TBW > m_TEX0.TBW); // otherwise scale.x need to be reduced to make the larger texture fit (TODO) + + m_renderer->m_dev.CreateRenderTarget(m_texture, rt->m_texture.GetWidth(), rt->m_texture.GetHeight()); + + int bw = 64; + int bh = m_TEX0.PSM == PSM_PSMCT32 || m_TEX0.PSM == PSM_PSMCT24 ? 32 : 64; + + int sw = (int)rt->m_TEX0.TBW << 6; + + int dw = (int)m_TEX0.TBW << 6; + int dh = 1 << m_TEX0.TH; + + for(int dy = 0; dy < dh; dy += bh) + { + for(int dx = 0; dx < dw; dx += bw) + { + int o = dy * dw / bh + dx; + + int sx = o % sw; + int sy = o / sw; + + GSVector4 src, dst; + + src.x = rt->m_texture.m_scale.x * sx / rt->m_texture.GetWidth(); + src.y = rt->m_texture.m_scale.y * sy / rt->m_texture.GetHeight(); + src.z = rt->m_texture.m_scale.x * (sx + bw) / rt->m_texture.GetWidth(); + src.w = rt->m_texture.m_scale.y * (sy + bh) / rt->m_texture.GetHeight(); + + dst.x = rt->m_texture.m_scale.x * dx; + dst.y = rt->m_texture.m_scale.y * dy; + dst.z = rt->m_texture.m_scale.x * (dx + bw); + dst.w = rt->m_texture.m_scale.y * (dy + bh); + + m_renderer->m_dev.StretchRect(rt->m_texture, src, m_texture, dst); + + // TODO: this is quite a lot of StretchRect, do it with one Draw + } + } + } + else if(tw < tp) + { + // FIXME: timesplitters blurs the render target by blending itself over a couple of times + + if(tw == 256 && th == 128 && tp == 512 && (m_TEX0.TBP0 == 0 || m_TEX0.TBP0 == 0x00e00)) + { + return false; + } + } + + // width/height conversion + + GSVector2 scale = rt->m_texture.m_scale; + + GSVector4 dst(0, 0, w, h); + + if(w > rt->m_texture.GetWidth()) + { + scale.x = (float)rt->m_texture.GetWidth() / tw; + dst.z = (float)rt->m_texture.GetWidth() * scale.x / rt->m_texture.m_scale.x; + w = rt->m_texture.GetWidth(); + } + + if(h > rt->m_texture.GetHeight()) + { + scale.y = (float)rt->m_texture.GetHeight() / th; + dst.w = (float)rt->m_texture.GetHeight() * scale.y / rt->m_texture.m_scale.y; + h = rt->m_texture.GetHeight(); + } + + GSVector4 src(0, 0, w, h); + + Texture* st; + Texture* dt; + Texture tmp; + + if(!m_texture) + { + st = &rt->m_texture; + dt = &m_texture; + } + else + { + st = &m_texture; + dt = &tmp; + } + + m_renderer->m_dev.CreateRenderTarget(*dt, w, h); + + if(src.x == dst.x && src.y == dst.y && src.z == dst.z && src.w == dst.w) + { + CRect r(0, 0, w, h); + + m_renderer->m_dev->StretchRect(*st, r, *dt, r, D3DTEXF_POINT); + } + else + { + src.z /= st->GetWidth(); + src.w /= st->GetHeight(); + + m_renderer->m_dev.StretchRect(*st, src, *dt, dst); + } + + if(tmp) + { + m_renderer->m_dev.Recycle(m_texture); + + m_texture = tmp; + } + + m_texture.m_scale = scale; + + switch(m_TEX0.PSM) + { + case PSM_PSMCT32: + m_bpp2 = 0; + break; + case PSM_PSMCT24: + m_bpp2 = 1; + break; + case PSM_PSMCT16: + case PSM_PSMCT16S: + m_bpp2 = 2; + break; + case PSM_PSMT8H: + m_bpp2 = 3; + m_renderer->m_dev.CreateTexture(m_palette, 256, 1, m_TEX0.CPSM == PSM_PSMCT32 ? D3DFMT_A8R8G8B8 : D3DFMT_A1R5G5B5); + m_initpalette = true; + break; + case PSM_PSMT4HL: + case PSM_PSMT4HH: + ASSERT(0); // TODO + break; + } + + return true; +} + +bool GSTextureCache9::GSTexture9::Create(GSDepthStencil* ds) +{ + m_rendered = true; + + // TODO + + return false; +} + +void GSTextureCache9::GSTexture9::Update() +{ + __super::Update(); + + if(m_rendered) + { + return; + } + + CRect r; + + if(!GetDirtyRect(r)) + { + return; + } + + m_valid |= r; + + BYTE* bits; + int pitch; + + if(m_texture.Map(&bits, pitch, &r)) + { + if(m_renderer->m_psrr) + { + m_renderer->m_mem.ReadTextureNPNC(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP); + } + else + { + m_renderer->m_mem.ReadTextureNP(r, bits, pitch, m_renderer->m_context->TEX0, m_renderer->m_env.TEXA, m_renderer->m_context->CLAMP); + } + + m_texture.Unmap(); + } + + m_renderer->m_perfmon.Put(GSPerfMon::Unswizzle, r.Width() * r.Height() * m_bpp >> 3); +} diff --git a/plugins/GSdx/GSTextureCache9.h b/plugins/GSdx/GSTextureCache9.h new file mode 100644 index 0000000000..5ac293fd55 --- /dev/null +++ b/plugins/GSdx/GSTextureCache9.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTextureCache.h" +#include "GSDevice9.h" + +class GSTextureCache9 : public GSTextureCache +{ + typedef GSDevice9::Texture Texture; + + class GSRenderTarget9 : public GSRenderTarget + { + public: + explicit GSRenderTarget9(GSRenderer* renderer) : GSRenderTarget(renderer) {} + + void Update(); + void Read(CRect r); + }; + + class GSDepthStencil9 : public GSDepthStencil + { + public: + explicit GSDepthStencil9(GSRenderer* renderer) : GSDepthStencil(renderer) {} + + void Update(); + }; + + class GSTexture9 : public GSTexture + { + public: + explicit GSTexture9(GSRenderer* renderer) : GSTexture(renderer) {} + + bool Create(); + bool Create(GSRenderTarget* rt); + bool Create(GSDepthStencil* ds); + void Update(); + }; + +protected: + GSRenderTarget* CreateRenderTarget() {return new GSRenderTarget9(m_renderer);} + GSDepthStencil* CreateDepthStencil() {return new GSDepthStencil9(m_renderer);} + GSTexture* CreateTexture() {return new GSTexture9(m_renderer);} + +public: + GSTextureCache9(GSRenderer* renderer); +}; \ No newline at end of file diff --git a/plugins/GSdx/GSTextureCacheSW.cpp b/plugins/GSdx/GSTextureCacheSW.cpp new file mode 100644 index 0000000000..ab230937c7 --- /dev/null +++ b/plugins/GSdx/GSTextureCacheSW.cpp @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSTextureCacheSW.h" + +// static FILE* m_log = NULL; + +GSTextureCacheSW::GSTextureCacheSW(GSState* state) + : m_state(state) +{ + // m_log = _tfopen(_T("c:\\log.txt"), _T("w")); +} + +GSTextureCacheSW::~GSTextureCacheSW() +{ + // fclose(m_log); + + RemoveAll(); +} + +const GSTextureCacheSW::GSTexture* GSTextureCacheSW::Lookup(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* r) +{ + GSLocalMemory& mem = m_state->m_mem; + + const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM]; + + const CAtlList& t2p = m_p2t[TEX0.TBP0 >> 5]; + + // fprintf(m_log, "lu %05x %d %d (%d) ", TEX0.TBP0, TEX0.TBW, TEX0.PSM, t2p.GetCount()); + + // if(r) fprintf(m_log, "(%d %d %d %d) ", r->left, r->top, r->right, r->bottom); + + GSTexture* t = NULL; + + POSITION pos = t2p.GetHeadPosition(); + + while(pos) + { + GSTexture* t2 = t2p.GetNext(pos)->t; + + // if(t2->m_TEX0.TBP0 != TEX0.TBP0 || t2->m_TEX0.TBW != TEX0.TBW || t2->m_TEX0.PSM != TEX0.PSM || t2->m_TEX0.TW != TEX0.TW || t2->m_TEX0.TH != TEX0.TH) + if(((t2->m_TEX0.ai32[0] ^ TEX0.ai32[0]) | ((t2->m_TEX0.ai32[1] ^ TEX0.ai32[1]) & 3)) != 0) + { + continue; + } + + if((psm.trbpp == 16 || psm.trbpp == 24) && (t2->m_TEX0.TCC != TEX0.TCC || TEX0.TCC == 1 && !(t2->m_TEXA == (GSVector4i)TEXA).alltrue())) + { + continue; + } + + // fprintf(m_log, "cache hit\n"); + + t = t2; + + t->m_age = 0; + + break; + } + + if(t == NULL) + { + // fprintf(m_log, "cache miss\n"); + + t = new GSTexture(m_state); + + t->m_pos = m_textures.AddTail(t); + + int tw = 1 << TEX0.TW; + int th = 1 << TEX0.TH; + + DWORD bp = TEX0.TBP0; + DWORD bw = TEX0.TBW; + + for(int j = 0, y = 0; y < th; j++, y += psm.pgs.cy) + { + DWORD page = psm.pgn(0, y, bp, bw); + + for(int i = 0, x = 0; x < tw && page < MAX_PAGES; i++, x += psm.pgs.cx, page++) + { + GSTexturePage* p = new GSTexturePage(); + + p->t = t; + p->row = j; + p->col = i; + + GSTexturePageEntry* p2te = new GSTexturePageEntry(); + + p2te->p2t = &m_p2t[page]; + p2te->pos = m_p2t[page].AddHead(p); + + t->m_p2te.AddTail(p2te); + + t->m_maxpages++; + } + } + } + + if(!t->Update(TEX0, TEXA, r)) + { + m_textures.RemoveAt(t->m_pos); + + delete t; + + printf("!@#$%\n"); // memory allocation may fail if the game is too hungry + + return NULL; + } + + return t; +} + +void GSTextureCacheSW::RemoveAll() +{ + POSITION pos = m_textures.GetHeadPosition(); + + while(pos) + { + delete m_textures.GetNext(pos); + } + + m_textures.RemoveAll(); + + for(int i = 0; i < MAX_PAGES; i++) + { + CAtlList& t2p = m_p2t[i]; + + ASSERT(t2p.IsEmpty()); + + POSITION pos = t2p.GetHeadPosition(); + + while(pos) + { + delete t2p.GetNext(pos); + } + + t2p.RemoveAll(); + } +} + +void GSTextureCacheSW::IncAge() +{ + POSITION pos = m_textures.GetHeadPosition(); + + while(pos) + { + POSITION cur = pos; + + GSTexture* t = m_textures.GetNext(pos); + + if(++t->m_age > 3) + { + m_textures.RemoveAt(cur); + + delete t; + } + } +} + +void GSTextureCacheSW::InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, const CRect& r) +{ + const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[BITBLTBUF.DPSM]; + + CRect r2; + + r2.left = r.left & ~(psm.pgs.cx - 1); + r2.top = r.top & ~(psm.pgs.cy - 1); + r2.right = (r.right + (psm.pgs.cx - 1)) & ~(psm.pgs.cx - 1); + r2.bottom = (r.bottom + (psm.pgs.cy - 1)) & ~(psm.pgs.cy - 1); + + DWORD bp = BITBLTBUF.DBP; + DWORD bw = BITBLTBUF.DBW; + + // fprintf(m_log, "ivm %05x %d %d (%d %d %d %d)\n", bp, bw, BITBLTBUF.DPSM, r2.left, r2.top, r2.right, r2.bottom); + + for(int y = r2.top; y < r2.bottom; y += psm.pgs.cy) + { + DWORD page = psm.pgn(r2.left, y, bp, bw); + + for(int x = r2.left; x < r2.right && page < MAX_PAGES; x += psm.pgs.cx, page++) + { + const CAtlList& t2p = m_p2t[page]; + + POSITION pos = t2p.GetHeadPosition(); + + while(pos) + { + GSTexturePage* p = t2p.GetNext(pos); + + DWORD flag = 1 << p->col; + + if((p->t->m_valid[p->row] & flag) == 0) + { + continue; + } + + if(GSUtil::HasSharedBits(BITBLTBUF.DPSM, p->t->m_TEX0.PSM)) + { + p->t->m_valid[p->row] &= ~flag; + p->t->m_pages--; + + // fprintf(m_log, "ivm hit %05x %d %d (%d %d) (%d)", p->t->m_TEX0.TBP0, p->t->m_TEX0.TBW, p->t->m_TEX0.PSM, p->row, p->col, p->t->m_pages); + // if(p->t->m_pages == 0) fprintf(m_log, " *"); + // fprintf(m_log, "\n"); + } + } + } + } +} + +// + +GSTextureCacheSW::GSTexture::GSTexture(GSState* state) + : m_state(state) + , m_buff(NULL) + , m_tw(0) + , m_maxpages(0) + , m_pages(0) + , m_pos(NULL) + , m_age(0) +{ + memset(m_valid, 0, sizeof(m_valid)); +} + +GSTextureCacheSW::GSTexture::~GSTexture() +{ + if(m_buff) + { + _aligned_free(m_buff); + } + + POSITION pos = m_p2te.GetHeadPosition(); + + while(pos) + { + GSTexturePageEntry* p2te = m_p2te.GetNext(pos); + + GSTexturePage* p = p2te->p2t->GetAt(p2te->pos); + + ASSERT(p->t == this); + + delete p; + + p2te->p2t->RemoveAt(p2te->pos); + + delete p2te; + } + + m_p2te.RemoveAll(); +} + +bool GSTextureCacheSW::GSTexture::Update(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* r) +{ + if(m_pages == m_maxpages) + { + return true; + } + + m_TEX0 = TEX0; + m_TEXA = TEXA; + + GSLocalMemory& mem = m_state->m_mem; + + const GSLocalMemory::psm_t& psm = GSLocalMemory::m_psm[TEX0.PSM]; + + int tw = 1 << TEX0.TW; + int th = 1 << TEX0.TH; + + if(tw < psm.bs.cx) tw = psm.bs.cx; + if(th < psm.bs.cy) th = psm.bs.cy; + + if(m_buff == NULL) + { + // fprintf(m_log, "up new (%d %d)\n", tw, th); + + m_buff = _aligned_malloc(tw * th * sizeof(DWORD), 16); + + if(m_buff == NULL) + { + return false; + } + + m_tw = max(psm.pal > 0 ? 5 : 3, TEX0.TW); // makes one row 32 bytes at least, matches the smallest block size that is allocated above for m_buff + } + + CRect r2; + + if(r) + { + r2.left = r->left & ~(psm.pgs.cx - 1); + r2.top = r->top & ~(psm.pgs.cy - 1); + r2.right = (r->right + (psm.pgs.cx - 1)) & ~(psm.pgs.cx - 1); + r2.bottom = (r->bottom + (psm.pgs.cy - 1)) & ~(psm.pgs.cy - 1); + } + + // TODO + + GSLocalMemory::readTexture rt = psm.pal > 0 ? psm.rtxP : psm.rtx; + int bytes = psm.pal > 0 ? 1 : 4; + + BYTE* dst = (BYTE*)m_buff; + + DWORD pitch = (1 << m_tw) * bytes; + DWORD mask = pitch - 1; + + for(int j = 0, y = 0; y < th; j++, y += psm.pgs.cy, dst += pitch * psm.pgs.cy) + { + if(m_valid[j] == mask) + { + continue; + } + + if(r) + { + if(y < r2.top) continue; + if(y >= r2.bottom) break; + } + + DWORD page = psm.pgn(0, y, TEX0.TBP0, TEX0.TBW); + + for(int i = 0, x = 0; x < tw && page < MAX_PAGES; i++, x += psm.pgs.cx, page++) + { + if(r) + { + if(x < r2.left) continue; + if(x >= r2.right) break; + } + + DWORD flag = 1 << i; + + if(m_valid[j] & flag) + { + continue; + } + + m_valid[j] |= flag; + m_pages++; + + ASSERT(m_pages <= m_maxpages); + + CRect r; + + r.left = x; + r.top = y; + r.right = min(x + psm.pgs.cx, tw); + r.bottom = min(y + psm.pgs.cy, th); + + // fprintf(m_log, "up fetch (%d %d) (%d %d %d %d)\n", j, i, r.left, r.top, r.right, r.bottom); + + (mem.*rt)(r, &dst[x * bytes], pitch, TEX0, TEXA); + + m_state->m_perfmon.Put(GSPerfMon::Unswizzle, r.Width() * r.Height() * bytes); + } + } + + return true; +} \ No newline at end of file diff --git a/plugins/GSdx/GSTextureCacheSW.h b/plugins/GSdx/GSTextureCacheSW.h new file mode 100644 index 0000000000..96dc5659f3 --- /dev/null +++ b/plugins/GSdx/GSTextureCacheSW.h @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSRenderer.h" + +#define MAX_PAGES 512 + +class GSTextureCacheSW +{ +public: + class GSTexture; + class GSTexturePage; + + class GSTexturePage + { + public: + GSTexture* t; + DWORD row, col; + }; + + class GSTexturePageEntry + { + public: + CAtlList* p2t; + POSITION pos; + }; + + class GSTexture + { + public: + GSState* m_state; + GIFRegTEX0 m_TEX0; + GIFRegTEXA m_TEXA; + void* m_buff; + DWORD m_tw; + DWORD m_valid[32]; + DWORD m_maxpages; + DWORD m_pages; + CAtlList m_p2te; + POSITION m_pos; + DWORD m_age; + + explicit GSTexture(GSState* state); + virtual ~GSTexture(); + + bool Update(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* r = NULL); + }; + +protected: + GSState* m_state; + CAtlList m_textures; + CAtlList m_p2t[MAX_PAGES]; + +public: + GSTextureCacheSW(GSState* state); + virtual ~GSTextureCacheSW(); + + const GSTexture* Lookup(const GIFRegTEX0& TEX0, const GIFRegTEXA& TEXA, const CRect* r = NULL); + + void RemoveAll(); + void IncAge(); + void InvalidateVideoMem(const GIFRegBITBLTBUF& BITBLTBUF, const CRect& r); +}; diff --git a/plugins/GSdx/GSTextureFX10.cpp b/plugins/GSdx/GSTextureFX10.cpp new file mode 100644 index 0000000000..c116082cad --- /dev/null +++ b/plugins/GSdx/GSTextureFX10.cpp @@ -0,0 +1,583 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTextureFX10.h" +#include "resource.h" + +GSTextureFX10::GSTextureFX10() + : m_dev(NULL) + , m_vb_max(0) + , m_vb_start(0) + , m_vb_count(0) +{ + memset(&m_vs_cb_cache, 0, sizeof(m_vs_cb_cache)); + memset(&m_ps_cb_cache, 0, sizeof(m_ps_cb_cache)); +} + +bool GSTextureFX10::Create(GSDevice10* dev) +{ + m_dev = dev; + + // + + VSSelector sel; + + sel.bppz = 0; + sel.tme = 0; + sel.fst = 0; + + VSConstantBuffer cb; + + SetupVS(sel, &cb); // creates layout + + HRESULT hr; + + D3D10_BUFFER_DESC bd; + + memset(&bd, 0, sizeof(bd)); + + bd.ByteWidth = sizeof(VSConstantBuffer); + bd.Usage = D3D10_USAGE_DEFAULT; + bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER; + + hr = (*m_dev)->CreateBuffer(&bd, NULL, &m_vs_cb); + + if(FAILED(hr)) return false; + + memset(&bd, 0, sizeof(bd)); + + bd.ByteWidth = sizeof(PSConstantBuffer); + bd.Usage = D3D10_USAGE_DEFAULT; + bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER; + + hr = (*m_dev)->CreateBuffer(&bd, NULL, &m_ps_cb); + + if(FAILED(hr)) return false; + + D3D10_SAMPLER_DESC sd; + + memset(&sd, 0, sizeof(sd)); + + sd.Filter = D3D10_ENCODE_BASIC_FILTER(D3D10_FILTER_TYPE_POINT, D3D10_FILTER_TYPE_POINT, D3D10_FILTER_TYPE_POINT, false); + sd.AddressU = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressV = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP; + sd.MaxLOD = FLT_MAX; + sd.MaxAnisotropy = 16; + sd.ComparisonFunc = D3D10_COMPARISON_NEVER; + + hr = (*m_dev)->CreateSamplerState(&sd, &m_palette_ss); + + if(FAILED(hr)) return false; + + // + + return true; +} + +bool GSTextureFX10::SetupIA(const GSVertexHW10* vertices, int count, D3D10_PRIMITIVE_TOPOLOGY prim) +{ + HRESULT hr; + + if(max(count * 3 / 2, 10000) > m_vb_max) + { + m_vb_old = m_vb; + m_vb = NULL; + m_vb_max = max(count * 2, 10000); + m_vb_start = 0; + m_vb_count = 0; + } + + if(!m_vb) + { + D3D10_BUFFER_DESC bd; + + memset(&bd, 0, sizeof(bd)); + + bd.Usage = D3D10_USAGE_DYNAMIC; + bd.ByteWidth = m_vb_max * sizeof(vertices[0]); + bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; + bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + + hr = (*m_dev)->CreateBuffer(&bd, NULL, &m_vb); + + if(FAILED(hr)) return false; + } + + GSVertexHW10* v = NULL; + + int next = m_vb_start + m_vb_count; + + if(next + count > m_vb_max) + { + if(SUCCEEDED(m_vb->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v))) + { + memcpy(v, vertices, count * sizeof(vertices[0])); + + m_vb->Unmap(); + } + + m_vb_start = 0; + m_vb_count = count; + } + else + { + if(SUCCEEDED(m_vb->Map(D3D10_MAP_WRITE_NO_OVERWRITE, 0, (void**)&v))) + { + memcpy(&v[next], vertices, count * sizeof(vertices[0])); + + m_vb->Unmap(); + } + + m_vb_start = next; + m_vb_count = count; + } + + m_dev->IASetVertexBuffer(m_vb, sizeof(vertices[0])); + m_dev->IASetInputLayout(m_il); + m_dev->IASetPrimitiveTopology(prim); + + return true; +} + +bool GSTextureFX10::SetupVS(VSSelector sel, const VSConstantBuffer* cb) +{ + CComPtr vs; + + if(CRBMap >::CPair* pair = m_vs.Lookup(sel)) + { + vs = pair->m_value; + } + else + { + CStringA str[5]; + + str[0].Format("%d", sel.bpp); + str[1].Format("%d", sel.bppz); + str[2].Format("%d", sel.tme); + str[3].Format("%d", sel.fst); + str[4].Format("%d", sel.prim); + + D3D10_SHADER_MACRO macro[] = + { + {"VS_BPP", str[0]}, + {"VS_BPPZ", str[1]}, + {"VS_TME", str[2]}, + {"VS_FST", str[3]}, + {"VS_PRIM", str[4]}, + {NULL, NULL}, + }; + + D3D10_INPUT_ELEMENT_DESC layout[] = + { + {"POSITION", 0, DXGI_FORMAT_R16G16_UINT, 0, 8, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"POSITION", 1, DXGI_FORMAT_R32_UINT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 1, DXGI_FORMAT_R32_FLOAT, 0, 20, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 16, D3D10_INPUT_PER_VERTEX_DATA, 0}, + {"COLOR", 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0}, + }; + + CComPtr il; + + m_dev->CompileShader(IDR_TFX10_FX, "vs_main", macro, &vs, layout, countof(layout), &il); + + if(m_il == NULL) + { + m_il = il; + } + + m_vs.SetAt(sel, vs); + } + + if(m_vs_cb_cache.Update(cb)) + { + (*m_dev)->UpdateSubresource(m_vs_cb, 0, NULL, cb, 0, 0); + } + + m_dev->VSSetShader(vs, m_vs_cb); + + return true; +} + +bool GSTextureFX10::SetupGS(GSSelector sel) +{ + HRESULT hr; + + CComPtr gs; + + if(sel.prim > 0 && (sel.iip == 0 || sel.prim == 3)) // geometry shader works in every case, but not needed + { + if(CRBMap >::CPair* pair = m_gs.Lookup(sel)) + { + gs = pair->m_value; + } + else + { + CStringA str[2]; + + str[0].Format("%d", sel.iip); + str[1].Format("%d", sel.prim); + + D3D10_SHADER_MACRO macro[] = + { + {"IIP", str[0]}, + {"PRIM", str[1]}, + {NULL, NULL}, + }; + + hr = m_dev->CompileShader(IDR_TFX10_FX, "gs_main", macro, &gs); + + m_gs.SetAt(sel, gs); + } + } + + m_dev->GSSetShader(gs); + + return true; +} + +bool GSTextureFX10::SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, ID3D10ShaderResourceView* tex, ID3D10ShaderResourceView* pal) +{ + m_dev->PSSetShaderResources(tex, pal); + + UpdatePS(sel, cb, ssel); + + return true; +} + +void GSTextureFX10::UpdatePS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel) +{ + HRESULT hr; + + CComPtr ps; + + if(CRBMap >::CPair* pair = m_ps.Lookup(sel)) + { + ps = pair->m_value; + } + else + { + CStringA str[13]; + + str[0].Format("%d", sel.fst); + str[1].Format("%d", sel.wms); + str[2].Format("%d", sel.wmt); + str[3].Format("%d", sel.bpp); + str[4].Format("%d", sel.aem); + str[5].Format("%d", sel.tfx); + str[6].Format("%d", sel.tcc); + str[7].Format("%d", sel.ate); + str[8].Format("%d", sel.atst); + str[9].Format("%d", sel.fog); + str[10].Format("%d", sel.clr1); + str[11].Format("%d", sel.fba); + str[12].Format("%d", sel.aout); + + D3D10_SHADER_MACRO macro[] = + { + {"FST", str[0]}, + {"WMS", str[1]}, + {"WMT", str[2]}, + {"BPP", str[3]}, + {"AEM", str[4]}, + {"TFX", str[5]}, + {"TCC", str[6]}, + {"ATE", str[7]}, + {"ATST", str[8]}, + {"FOG", str[9]}, + {"CLR1", str[10]}, + {"FBA", str[11]}, + {"AOUT", str[12]}, + {NULL, NULL}, + }; + + hr = m_dev->CompileShader(IDR_TFX10_FX, "ps_main", macro, &ps); + + m_ps.SetAt(sel, ps); + } + + if(m_ps_cb_cache.Update(cb)) + { + (*m_dev)->UpdateSubresource(m_ps_cb, 0, NULL, cb, 0, 0); + } + + m_dev->PSSetShader(ps, m_ps_cb); + + CComPtr ss0, ss1; + + if(sel.tfx != 4) + { + if(sel.bpp >= 3 || sel.wms >= 3 || sel.wmt >= 3) + { + ssel.min = ssel.mag = 0; + } + + if(CRBMap >::CPair* pair = m_ps_ss.Lookup(ssel)) + { + ss0 = pair->m_value; + } + else + { + D3D10_SAMPLER_DESC sd; + + memset(&sd, 0, sizeof(sd)); + + sd.Filter = D3D10_ENCODE_BASIC_FILTER( + (ssel.min ? D3D10_FILTER_TYPE_LINEAR : D3D10_FILTER_TYPE_POINT), + (ssel.mag ? D3D10_FILTER_TYPE_LINEAR : D3D10_FILTER_TYPE_POINT), + D3D10_FILTER_TYPE_POINT, + false); + + sd.AddressU = ssel.tau ? D3D10_TEXTURE_ADDRESS_WRAP : D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressV = ssel.tav ? D3D10_TEXTURE_ADDRESS_WRAP : D3D10_TEXTURE_ADDRESS_CLAMP; + sd.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP; + + sd.MaxLOD = FLT_MAX; + sd.MaxAnisotropy = 16; + sd.ComparisonFunc = D3D10_COMPARISON_NEVER; + + hr = (*m_dev)->CreateSamplerState(&sd, &ss0); + + m_ps_ss.SetAt(ssel, ss0); + } + + if(sel.bpp == 3) + { + ss1 = m_palette_ss; + } + } + + m_dev->PSSetSamplerState(ss0, ss1); +} + +void GSTextureFX10::SetupRS(UINT w, UINT h, const RECT& scissor) +{ + m_dev->RSSet(w, h, &scissor); +} + +void GSTextureFX10::SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, float bf, ID3D10RenderTargetView* rtv, ID3D10DepthStencilView* dsv) +{ + UpdateOM(dssel, bsel, bf); + + m_dev->OMSetRenderTargets(rtv, dsv); +} + +void GSTextureFX10::UpdateOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, float bf) +{ + HRESULT hr; + + CComPtr dss; + + if(CRBMap >::CPair* pair = m_om_dss.Lookup(dssel)) + { + dss = pair->m_value; + } + else + { + D3D10_DEPTH_STENCIL_DESC dsd; + + memset(&dsd, 0, sizeof(dsd)); + + if(dssel.date) + { + dsd.StencilEnable = true; + dsd.StencilReadMask = 1; + dsd.StencilWriteMask = 1; + dsd.FrontFace.StencilFunc = D3D10_COMPARISON_EQUAL; + dsd.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP; + dsd.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP; + dsd.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_KEEP; + dsd.BackFace.StencilFunc = D3D10_COMPARISON_EQUAL; + dsd.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP; + dsd.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP; + dsd.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_KEEP; + } + + if(!(dssel.zte && dssel.ztst == 1 && !dssel.zwe)) + { + static const D3D10_COMPARISON_FUNC ztst[] = + { + D3D10_COMPARISON_NEVER, + D3D10_COMPARISON_ALWAYS, + D3D10_COMPARISON_GREATER_EQUAL, + D3D10_COMPARISON_GREATER + }; + + dsd.DepthEnable = dssel.zte; + dsd.DepthWriteMask = dssel.zwe ? D3D10_DEPTH_WRITE_MASK_ALL : D3D10_DEPTH_WRITE_MASK_ZERO; + dsd.DepthFunc = ztst[dssel.ztst]; + } + + hr = (*m_dev)->CreateDepthStencilState(&dsd, &dss); + + m_om_dss.SetAt(dssel, dss); + } + + m_dev->OMSetDepthStencilState(dss, 1); + + CComPtr bs; + + if(CRBMap >::CPair* pair = m_om_bs.Lookup(bsel)) + { + bs = pair->m_value; + } + else + { + D3D10_BLEND_DESC bd; + + memset(&bd, 0, sizeof(bd)); + + bd.BlendEnable[0] = bsel.abe; + + if(bsel.abe) + { + // (A:Cs/Cd/0 - B:Cs/Cd/0) * C:As/Ad/FIX + D:Cs/Cd/0 + + static const struct {int bogus; D3D10_BLEND_OP op; D3D10_BLEND src, dst;} map[3*3*3*3] = + { + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 0000: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 0001: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 0002: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 0010: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 0011: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 0012: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 0020: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 0021: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 0022: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {1, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_SRC1_ALPHA}, // * 0100: (Cs - Cd)*As + Cs ==> Cs*(As + 1) - Cd*As + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_INV_SRC1_ALPHA}, // 0101: (Cs - Cd)*As + Cd ==> Cs*As + Cd*(1 - As) + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_SRC1_ALPHA}, // 0102: (Cs - Cd)*As + 0 ==> Cs*As - Cd*As + {1, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_DEST_ALPHA}, // * 0110: (Cs - Cd)*Ad + Cs ==> Cs*(Ad + 1) - Cd*Ad + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_INV_DEST_ALPHA}, // 0111: (Cs - Cd)*Ad + Cd ==> Cs*Ad + Cd*(1 - Ad) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_DEST_ALPHA}, // 0112: (Cs - Cd)*Ad + 0 ==> Cs*Ad - Cd*Ad + {1, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_BLEND_FACTOR}, // * 0120: (Cs - Cd)*F + Cs ==> Cs*(F + 1) - Cd*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_INV_BLEND_FACTOR}, // 0121: (Cs - Cd)*F + Cd ==> Cs*F + Cd*(1 - F) + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_BLEND_FACTOR}, // 0122: (Cs - Cd)*F + 0 ==> Cs*F - Cd*F + {1, D3D10_BLEND_OP_ADD, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ZERO}, // * 0200: (Cs - 0)*As + Cs ==> Cs*(As + 1) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ONE}, // 0201: (Cs - 0)*As + Cd ==> Cs*As + Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ZERO}, // 0202: (Cs - 0)*As + 0 ==> Cs*As + {1, D3D10_BLEND_OP_ADD, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ZERO}, // * 0210: (Cs - 0)*Ad + Cs ==> Cs*(Ad + 1) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_ONE}, // 0211: (Cs - 0)*Ad + Cd ==> Cs*Ad + Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_ZERO}, // 0212: (Cs - 0)*Ad + 0 ==> Cs*Ad + {1, D3D10_BLEND_OP_ADD, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_ZERO}, // * 0220: (Cs - 0)*F + Cs ==> Cs*(F + 1) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_ONE}, // 0221: (Cs - 0)*F + Cd ==> Cs*F + Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_ZERO}, // 0222: (Cs - 0)*F + 0 ==> Cs*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_SRC1_ALPHA, D3D10_BLEND_SRC1_ALPHA}, // 1000: (Cd - Cs)*As + Cs ==> Cd*As + Cs*(1 - As) + {1, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_SRC1_ALPHA}, // * 1001: (Cd - Cs)*As + Cd ==> Cd*(As + 1) - Cs*As + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_SRC1_ALPHA}, // 1002: (Cd - Cs)*As + 0 ==> Cd*As - Cs*As + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_DEST_ALPHA, D3D10_BLEND_DEST_ALPHA}, // 1010: (Cd - Cs)*Ad + Cs ==> Cd*Ad + Cs*(1 - Ad) + {1, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_DEST_ALPHA}, // * 1011: (Cd - Cs)*Ad + Cd ==> Cd*(Ad + 1) - Cs*Ad + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_DEST_ALPHA}, // 1012: (Cd - Cs)*Ad + 0 ==> Cd*Ad - Cs*Ad + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_BLEND_FACTOR, D3D10_BLEND_BLEND_FACTOR}, // 1020: (Cd - Cs)*F + Cs ==> Cd*F + Cs*(1 - F) + {1, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_BLEND_FACTOR},// * 1021: (Cd - Cs)*F + Cd ==> Cd*(F + 1) - Cs*F + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_BLEND_FACTOR},// 1022: (Cd - Cs)*F + 0 ==> Cd*F - Cs*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 1100: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 1101: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 1102: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 1110: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 1111: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 1112: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 1120: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 1121: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 1122: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_SRC1_ALPHA}, // 1200: (Cd - 0)*As + Cs ==> Cs + Cd*As + {2, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_COLOR, D3D10_BLEND_SRC1_ALPHA}, // ** 1201: (Cd - 0)*As + Cd ==> Cd*(1 + As) // ffxii main menu background glow effect + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_SRC1_ALPHA}, // 1202: (Cd - 0)*As + 0 ==> Cd*As + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_DEST_ALPHA}, // 1210: (Cd - 0)*Ad + Cs ==> Cs + Cd*Ad + {2, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_COLOR, D3D10_BLEND_DEST_ALPHA}, // ** 1211: (Cd - 0)*Ad + Cd ==> Cd*(1 + Ad) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_DEST_ALPHA}, // 1212: (Cd - 0)*Ad + 0 ==> Cd*Ad + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_BLEND_FACTOR}, // 1220: (Cd - 0)*F + Cs ==> Cs + Cd*F + {2, D3D10_BLEND_OP_ADD, D3D10_BLEND_DEST_COLOR, D3D10_BLEND_BLEND_FACTOR}, // ** 1221: (Cd - 0)*F + Cd ==> Cd*(1 + F) + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_BLEND_FACTOR}, // 1222: (Cd - 0)*F + 0 ==> Cd*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_SRC1_ALPHA, D3D10_BLEND_ZERO}, // 2000: (0 - Cs)*As + Cs ==> Cs*(1 - As) + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ONE}, // 2001: (0 - Cs)*As + Cd ==> Cd - Cs*As + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_SRC1_ALPHA, D3D10_BLEND_ZERO}, // 2002: (0 - Cs)*As + 0 ==> 0 - Cs*As + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_DEST_ALPHA, D3D10_BLEND_ZERO}, // 2010: (0 - Cs)*Ad + Cs ==> Cs*(1 - Ad) + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_ONE}, // 2011: (0 - Cs)*Ad + Cd ==> Cd - Cs*Ad + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_DEST_ALPHA, D3D10_BLEND_ZERO}, // 2012: (0 - Cs)*Ad + 0 ==> 0 - Cs*Ad + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_INV_BLEND_FACTOR, D3D10_BLEND_ZERO}, // 2020: (0 - Cs)*F + Cs ==> Cs*(1 - F) + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_ONE}, // 2021: (0 - Cs)*F + Cd ==> Cd - Cs*F + {0, D3D10_BLEND_OP_REV_SUBTRACT, D3D10_BLEND_BLEND_FACTOR, D3D10_BLEND_ZERO}, // 2022: (0 - Cs)*F + 0 ==> 0 - Cs*F + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ONE, D3D10_BLEND_SRC1_ALPHA}, // 2100: (0 - Cd)*As + Cs ==> Cs - Cd*As + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_INV_SRC1_ALPHA}, // 2101: (0 - Cd)*As + Cd ==> Cd*(1 - As) + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ZERO, D3D10_BLEND_SRC1_ALPHA}, // 2102: (0 - Cd)*As + 0 ==> 0 - Cd*As + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ONE, D3D10_BLEND_DEST_ALPHA}, // 2110: (0 - Cd)*Ad + Cs ==> Cs - Cd*Ad + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_INV_DEST_ALPHA}, // 2111: (0 - Cd)*Ad + Cd ==> Cd*(1 - Ad) + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ONE, D3D10_BLEND_DEST_ALPHA}, // 2112: (0 - Cd)*Ad + 0 ==> 0 - Cd*Ad + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ONE, D3D10_BLEND_BLEND_FACTOR}, // 2120: (0 - Cd)*F + Cs ==> Cs - Cd*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_INV_BLEND_FACTOR}, // 2121: (0 - Cd)*F + Cd ==> Cd*(1 - F) + {0, D3D10_BLEND_OP_SUBTRACT, D3D10_BLEND_ONE, D3D10_BLEND_BLEND_FACTOR}, // 2122: (0 - Cd)*F + 0 ==> 0 - Cd*F + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 2200: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 2201: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 2202: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 2210: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 2211: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 2212: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ONE, D3D10_BLEND_ZERO}, // 2220: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ONE}, // 2221: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3D10_BLEND_OP_ADD, D3D10_BLEND_ZERO, D3D10_BLEND_ZERO}, // 2222: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + }; + + // bogus: 0100, 0110, 0120, 0200, 0210, 0220, 1001, 1011, 1021 + + // tricky: 1201, 1211, 1221 + // + // Source.rgb = float3(1, 1, 1); + // 1201 Cd*(1 + As) => Source * Dest color + Dest * Source1 alpha + // 1211 Cd*(1 + Ad) => Source * Dest color + Dest * Dest alpha + // 1221 Cd*(1 + F) => Source * Dest color + Dest * Factor + + int i = ((bsel.a * 3 + bsel.b) * 3 + bsel.c) * 3 + bsel.d; + + bd.BlendOp = map[i].op; + bd.SrcBlend = map[i].src; + bd.DestBlend = map[i].dst; + bd.BlendOpAlpha = D3D10_BLEND_OP_ADD; + bd.SrcBlendAlpha = D3D10_BLEND_ONE; + bd.DestBlendAlpha = D3D10_BLEND_ZERO; + + if(map[i].bogus == 1) + { + ASSERT(0); + + (bsel.a == 0 ? bd.SrcBlend : bd.DestBlend) = D3D10_BLEND_ONE; + } + } + + if(bsel.wr) bd.RenderTargetWriteMask[0] |= D3D10_COLOR_WRITE_ENABLE_RED; + if(bsel.wg) bd.RenderTargetWriteMask[0] |= D3D10_COLOR_WRITE_ENABLE_GREEN; + if(bsel.wb) bd.RenderTargetWriteMask[0] |= D3D10_COLOR_WRITE_ENABLE_BLUE; + if(bsel.wa) bd.RenderTargetWriteMask[0] |= D3D10_COLOR_WRITE_ENABLE_ALPHA; + + hr = (*m_dev)->CreateBlendState(&bd, &bs); + + m_om_bs.SetAt(bsel, bs); + } + + m_dev->OMSetBlendState(bs, bf); +} + +void GSTextureFX10::Draw() +{ + m_dev->DrawPrimitive(m_vb_count, m_vb_start); +} diff --git a/plugins/GSdx/GSTextureFX10.h b/plugins/GSdx/GSTextureFX10.h new file mode 100644 index 0000000000..823176ee14 --- /dev/null +++ b/plugins/GSdx/GSTextureFX10.h @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice10.h" + +class GSTextureFX10 +{ +public: + #pragma pack(push, 1) + + __declspec(align(16)) struct VSConstantBuffer + { + GSVector4 VertexScale; + GSVector4 VertexOffset; + GSVector2 TextureScale; + float _pad[2]; + + struct VSConstantBuffer() {memset(this, 0, sizeof(*this));} + + __forceinline bool Update(const VSConstantBuffer* cb) + { + GSVector4i* a = (GSVector4i*)this; + GSVector4i* b = (GSVector4i*)cb; + + GSVector4i b0 = b[0]; + GSVector4i b1 = b[1]; + GSVector4i b2 = b[2]; + + if(!((a[0] == b0) & (a[1] == b1) & (a[2] == b2)).alltrue()) + { + a[0] = b0; + a[1] = b1; + a[2] = b2; + + return true; + } + + return false; + } + }; + + union VSSelector + { + struct + { + DWORD bpp:3; + DWORD bppz:2; + DWORD tme:1; + DWORD fst:1; + DWORD prim:3; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x3ff;} + }; + + __declspec(align(16)) struct PSConstantBuffer + { + GSVector4 FogColor; + float MINU; + float MAXU; + float MINV; + float MAXV; + DWORD UMSK; + DWORD UFIX; + DWORD VMSK; + DWORD VFIX; + float TA0; + float TA1; + float AREF; + float _pad[1]; + GSVector2 WH; + GSVector2 rWrH; + + struct PSConstantBuffer() {memset(this, 0, sizeof(*this));} + + __forceinline bool Update(const PSConstantBuffer* cb) + { + GSVector4i* a = (GSVector4i*)this; + GSVector4i* b = (GSVector4i*)cb; + + GSVector4i b0 = b[0]; + GSVector4i b1 = b[1]; + GSVector4i b2 = b[2]; + GSVector4i b3 = b[3]; + GSVector4i b4 = b[4]; + + if(!((a[0] == b0) & (a[1] == b1) & (a[2] == b2) & (a[3] == b3) & (a[4] == b4)).alltrue()) + { + a[0] = b0; + a[1] = b1; + a[2] = b2; + a[3] = b3; + a[4] = b4; + + return true; + } + + return false; + } + }; + + union PSSelector + { + struct + { + DWORD fst:1; + DWORD wms:2; + DWORD wmt:2; + DWORD bpp:3; + DWORD aem:1; + DWORD tfx:3; + DWORD tcc:1; + DWORD ate:1; + DWORD atst:3; + DWORD fog:1; + DWORD clr1:1; + DWORD fba:1; + DWORD aout:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x1fffff;} + }; + + union GSSelector + { + struct + { + DWORD iip:1; + DWORD prim:2; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x7;} + }; + + union PSSamplerSelector + { + struct + { + DWORD tau:1; + DWORD tav:1; + DWORD min:1; + DWORD mag:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0xf;} + }; + + union OMDepthStencilSelector + { + struct + { + DWORD zte:1; + DWORD ztst:2; + DWORD zwe:1; + DWORD date:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x1f;} + }; + + union OMBlendSelector + { + struct + { + DWORD abe:1; + DWORD a:2; + DWORD b:2; + DWORD c:2; + DWORD d:2; + DWORD wr:1; + DWORD wg:1; + DWORD wb:1; + DWORD wa:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x1fff;} + }; + + #pragma pack(pop) + +private: + GSDevice10* m_dev; + CComPtr m_il; + CRBMapC > m_vs; + CComPtr m_vs_cb; + CRBMapC > m_gs; + CRBMapC > m_ps; + CComPtr m_ps_cb; + CRBMapC > m_ps_ss; + CComPtr m_palette_ss; + CRBMapC > m_om_dss; + CRBMapC > m_om_bs; + + CComPtr m_vb, m_vb_old; + int m_vb_max; + int m_vb_start; + int m_vb_count; + + VSConstantBuffer m_vs_cb_cache; + PSConstantBuffer m_ps_cb_cache; + +public: + GSTextureFX10(); + + bool Create(GSDevice10* dev); + + bool SetupIA(const GSVertexHW10* vertices, int count, D3D10_PRIMITIVE_TOPOLOGY prim); + bool SetupVS(VSSelector sel, const VSConstantBuffer* cb); + bool SetupGS(GSSelector sel); + bool SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, ID3D10ShaderResourceView* tex, ID3D10ShaderResourceView* pal); + void UpdatePS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel); + void SetupRS(UINT w, UINT h, const RECT& scissor); + void SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, float bf, ID3D10RenderTargetView* rtv, ID3D10DepthStencilView* dsv); + void UpdateOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, float bf); + void Draw(); +}; diff --git a/plugins/GSdx/GSTextureFX9.cpp b/plugins/GSdx/GSTextureFX9.cpp new file mode 100644 index 0000000000..de10336849 --- /dev/null +++ b/plugins/GSdx/GSTextureFX9.cpp @@ -0,0 +1,464 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTextureFX9.h" +#include "resource.h" + +GSTextureFX9::GSTextureFX9() + : m_dev(NULL) +{ +} + +bool GSTextureFX9::Create(GSDevice9* dev) +{ + m_dev = dev; + + VSSelector sel; + + sel.bppz = 0; + sel.tme = 0; + sel.fst = 0; + sel.logz = 0; + + VSConstantBuffer cb; + + SetupVS(sel, &cb); // creates layout + + return true; +} + +bool GSTextureFX9::CreateMskFix(GSTexture9& t, DWORD size, DWORD msk, DWORD fix) +{ + DWORD hash = (size << 20) | (msk << 10) | fix; + + if(CRBMap::CPair* pair = m_mskfix.Lookup(hash)) + { + t = pair->m_value; + } + else + { + if(!m_dev->CreateTexture(t, size, 1, D3DFMT_R32F)) + { + return false; + } + + BYTE* bits; + int pitch; + + if(t.Map(&bits, pitch)) + { + for(DWORD i = 0; i < size; i++) + { + ((float*)bits)[i] = (float)((i & msk) | fix) / size; + } + + t.Unmap(); + } + + m_mskfix.SetAt(hash, t); + } + + return true; +} + +bool GSTextureFX9::SetupIA(const GSVertexHW9* vertices, UINT count, D3DPRIMITIVETYPE prim) +{ + m_dev->IASetVertexBuffer(count, vertices); + m_dev->IASetInputLayout(m_il); + m_dev->IASetPrimitiveTopology(prim); + + return true; +} + +bool GSTextureFX9::SetupVS(VSSelector sel, const VSConstantBuffer* cb) +{ + CComPtr vs; + + if(CRBMap >::CPair* pair = m_vs.Lookup(sel)) + { + vs = pair->m_value; + } + else + { + CStringA str[4]; + + str[0].Format("%d", sel.bppz); + str[1].Format("%d", sel.tme); + str[2].Format("%d", sel.fst); + str[3].Format("%d", sel.logz); + + D3DXMACRO macro[] = + { + {"VS_BPPZ", str[0]}, + {"VS_TME", str[1]}, + {"VS_FST", str[2]}, + {"VS_LOGZ", str[3]}, + {NULL, NULL}, + }; + + static const D3DVERTEXELEMENT9 layout[] = + { + {0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, + {0, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, + {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1}, + {0, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, + D3DDECL_END() + }; + + CComPtr il; + + m_dev->CompileShader(IDR_TFX9_FX, "vs_main", macro, &vs, layout, countof(layout), &il); + + if(m_il == NULL) + { + m_il = il; + } + + m_vs.SetAt(sel, vs); + } + + m_dev->VSSetShader(vs, (const float*)cb, sizeof(*cb) / sizeof(GSVector4)); + + return true; +} + +bool GSTextureFX9::SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, IDirect3DTexture9* tex, IDirect3DTexture9* pal, bool psrr) +{ + m_dev->PSSetShaderResources(tex, pal); + + if(tex && psrr) + { + if(sel.wms == 3) + { + D3DSURFACE_DESC desc; + tex->GetLevelDesc(0, &desc); + + GSTexture9 t; + CreateMskFix(t, desc.Width, cb->UMSK, cb->UFIX); + + (*m_dev)->SetTexture(2, t); + } + + if(sel.wmt == 3) + { + D3DSURFACE_DESC desc; + tex->GetLevelDesc(0, &desc); + + GSTexture9 t; + CreateMskFix(t, desc.Height, cb->VMSK, cb->VFIX); + + (*m_dev)->SetTexture(3, t); + } + } + + UpdatePS(sel, cb, ssel, psrr); + + return true; +} + +void GSTextureFX9::UpdatePS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, bool psrr) +{ + HRESULT hr; + + if(!psrr) + { + if(sel.wms == 3) sel.wms = 0; + if(sel.wmt == 3) sel.wmt = 0; + } + + CComPtr ps; + + if(CRBMap >::CPair* pair = m_ps.Lookup(sel)) + { + ps = pair->m_value; + } + else + { + CStringA str[12]; + + str[0].Format("%d", sel.fst); + str[1].Format("%d", sel.wms); + str[2].Format("%d", sel.wmt); + str[3].Format("%d", sel.bpp); + str[4].Format("%d", sel.aem); + str[5].Format("%d", sel.tfx); + str[6].Format("%d", sel.tcc); + str[7].Format("%d", sel.ate); + str[8].Format("%d", sel.atst); + str[9].Format("%d", sel.fog); + str[10].Format("%d", sel.clr1); + str[11].Format("%d", sel.rt); + + D3DXMACRO macro[] = + { + {"FST", str[0]}, + {"WMS", str[1]}, + {"WMT", str[2]}, + {"BPP", str[3]}, + {"AEM", str[4]}, + {"TFX", str[5]}, + {"TCC", str[6]}, + {"ATE", str[7]}, + {"ATST", str[8]}, + {"FOG", str[9]}, + {"CLR1", str[10]}, + {"RT", str[11]}, + {NULL, NULL}, + }; + + hr = m_dev->CompileShader(IDR_TFX9_FX, "ps_main", macro, &ps); + + m_ps.SetAt(sel, ps); + } + + m_dev->PSSetShader(ps, (const float*)cb, sizeof(*cb) / sizeof(GSVector4)); + + Direct3DSamplerState9* ss = NULL; + + if(sel.tfx != 4) + { + if(sel.bpp >= 3 || sel.wms >= 3 || sel.wmt >= 3) + { + ssel.min = ssel.mag = 0; + } + + if(CRBMap::CPair* pair = m_ps_ss.Lookup(ssel)) + { + ss = pair->m_value; + } + else + { + ss = new Direct3DSamplerState9(); + + memset(ss, 0, sizeof(*ss)); + + ss->FilterMin[0] = ssel.min ? D3DTEXF_LINEAR : D3DTEXF_POINT; + ss->FilterMag[0] = ssel.mag ? D3DTEXF_LINEAR : D3DTEXF_POINT; + ss->FilterMin[1] = D3DTEXF_POINT; + ss->FilterMag[1] = D3DTEXF_POINT; + + ss->AddressU = ssel.tau ? D3DTADDRESS_WRAP : D3DTADDRESS_CLAMP; + ss->AddressV = ssel.tav ? D3DTADDRESS_WRAP : D3DTADDRESS_CLAMP; + + m_ps_ss.SetAt(ssel, ss); + } + } + + m_dev->PSSetSamplerState(ss); +} + +void GSTextureFX9::SetupRS(int w, int h, const RECT& scissor) +{ + m_dev->RSSet(w, h, &scissor); +} + +void GSTextureFX9::SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, BYTE bf, IDirect3DSurface9* rt, IDirect3DSurface9* ds) +{ + UpdateOM(dssel, bsel, bf); + + m_dev->OMSetRenderTargets(rt, ds); +} + +void GSTextureFX9::UpdateOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, BYTE bf) +{ + Direct3DDepthStencilState9* dss = NULL; + + if(CRBMap::CPair* pair = m_om_dss.Lookup(dssel)) + { + dss = pair->m_value; + } + else + { + dss = new Direct3DDepthStencilState9(); + + memset(dss, 0, sizeof(*dss)); + + if(dssel.date || dssel.fba) + { + dss->StencilEnable = true; + dss->StencilReadMask = 1; + dss->StencilWriteMask = 2; + dss->StencilFunc = dssel.date ? D3DCMP_EQUAL : D3DCMP_ALWAYS; + dss->StencilPassOp = dssel.fba ? D3DSTENCILOP_REPLACE : D3DSTENCILOP_KEEP; + dss->StencilFailOp = dssel.fba ? D3DSTENCILOP_ZERO : D3DSTENCILOP_KEEP; + dss->StencilDepthFailOp = dssel.fba ? D3DSTENCILOP_ZERO : D3DSTENCILOP_KEEP; + } + + if(!(dssel.zte && dssel.ztst == 1 && !dssel.zwe)) + { + static const D3DCMPFUNC ztst[] = + { + D3DCMP_NEVER, + D3DCMP_ALWAYS, + D3DCMP_GREATEREQUAL, + D3DCMP_GREATER + }; + + dss->DepthEnable = dssel.zte; + dss->DepthWriteMask = dssel.zwe; + dss->DepthFunc = ztst[dssel.ztst]; + } + + m_om_dss.SetAt(dssel, dss); + } + + m_dev->OMSetDepthStencilState(dss, 3); + + Direct3DBlendState9* bs = NULL; + + if(CRBMap::CPair* pair = m_om_bs.Lookup(bsel)) + { + bs = pair->m_value; + } + else + { + bs = new Direct3DBlendState9(); + + memset(bs, 0, sizeof(*bs)); + + bs->BlendEnable = bsel.abe; + + if(bsel.abe) + { + // (A:Cs/Cd/0 - B:Cs/Cd/0) * C:As/Ad/FIX + D:Cs/Cd/0 + + static const struct {int bogus; D3DBLENDOP op; D3DBLEND src, dst;} map[3*3*3*3] = + { + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 0000: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 0001: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 0002: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 0010: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 0011: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 0012: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 0020: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 0021: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 0022: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {1, D3DBLENDOP_SUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA}, // * 0100: (Cs - Cd)*As + Cs ==> Cs*(As + 1) - Cd*As + {0, D3DBLENDOP_ADD, D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA}, // 0101: (Cs - Cd)*As + Cd ==> Cs*As + Cd*(1 - As) + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA}, // 0102: (Cs - Cd)*As + 0 ==> Cs*As - Cd*As + {1, D3DBLENDOP_SUBTRACT, D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA}, // * 0110: (Cs - Cd)*Ad + Cs ==> Cs*(Ad + 1) - Cd*Ad + {0, D3DBLENDOP_ADD, D3DBLEND_DESTALPHA, D3DBLEND_INVDESTALPHA}, // 0111: (Cs - Cd)*Ad + Cd ==> Cs*Ad + Cd*(1 - Ad) + {0, D3DBLENDOP_ADD, D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA}, // 0112: (Cs - Cd)*Ad + 0 ==> Cs*Ad - Cd*Ad + {1, D3DBLENDOP_SUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR}, // * 0120: (Cs - Cd)*F + Cs ==> Cs*(F + 1) - Cd*F + {0, D3DBLENDOP_ADD, D3DBLEND_BLENDFACTOR, D3DBLEND_INVBLENDFACTOR}, // 0121: (Cs - Cd)*F + Cd ==> Cs*F + Cd*(1 - F) + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR}, // 0122: (Cs - Cd)*F + 0 ==> Cs*F - Cd*F + {1, D3DBLENDOP_ADD, D3DBLEND_SRCALPHA, D3DBLEND_ZERO}, // * 0200: (Cs - 0)*As + Cs ==> Cs*(As + 1) + {0, D3DBLENDOP_ADD, D3DBLEND_SRCALPHA, D3DBLEND_ONE}, // 0201: (Cs - 0)*As + Cd ==> Cs*As + Cd + {0, D3DBLENDOP_ADD, D3DBLEND_SRCALPHA, D3DBLEND_ZERO}, // 0202: (Cs - 0)*As + 0 ==> Cs*As + {1, D3DBLENDOP_ADD, D3DBLEND_SRCALPHA, D3DBLEND_ZERO}, // * 0210: (Cs - 0)*Ad + Cs ==> Cs*(As + 1) + {0, D3DBLENDOP_ADD, D3DBLEND_DESTALPHA, D3DBLEND_ONE}, // 0211: (Cs - 0)*Ad + Cd ==> Cs*Ad + Cd + {0, D3DBLENDOP_ADD, D3DBLEND_DESTALPHA, D3DBLEND_ZERO}, // 0212: (Cs - 0)*Ad + 0 ==> Cs*Ad + {1, D3DBLENDOP_ADD, D3DBLEND_BLENDFACTOR, D3DBLEND_ZERO}, // * 0220: (Cs - 0)*F + Cs ==> Cs*(F + 1) + {0, D3DBLENDOP_ADD, D3DBLEND_BLENDFACTOR, D3DBLEND_ONE}, // 0221: (Cs - 0)*F + Cd ==> Cs*F + Cd + {0, D3DBLENDOP_ADD, D3DBLEND_BLENDFACTOR, D3DBLEND_ZERO}, // 0222: (Cs - 0)*F + 0 ==> Cs*F + {0, D3DBLENDOP_ADD, D3DBLEND_INVSRCALPHA, D3DBLEND_SRCALPHA}, // 1000: (Cd - Cs)*As + Cs ==> Cd*As + Cs*(1 - As) + {1, D3DBLENDOP_REVSUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA}, // * 1001: (Cd - Cs)*As + Cd ==> Cd*(As + 1) - Cs*As + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA}, // 1002: (Cd - Cs)*As + 0 ==> Cd*As - Cs*As + {0, D3DBLENDOP_ADD, D3DBLEND_INVDESTALPHA, D3DBLEND_DESTALPHA}, // 1010: (Cd - Cs)*Ad + Cs ==> Cd*Ad + Cs*(1 - Ad) + {1, D3DBLENDOP_REVSUBTRACT, D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA}, // * 1011: (Cd - Cs)*Ad + Cd ==> Cd*(Ad + 1) - Cs*Ad + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA}, // 1012: (Cd - Cs)*Ad + 0 ==> Cd*Ad - Cs*Ad + {0, D3DBLENDOP_ADD, D3DBLEND_INVBLENDFACTOR, D3DBLEND_BLENDFACTOR}, // 1020: (Cd - Cs)*F + Cs ==> Cd*F + Cs*(1 - F) + {1, D3DBLENDOP_REVSUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR},// * 1021: (Cd - Cs)*F + Cd ==> Cd*(F + 1) - Cs*F + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR},// 1022: (Cd - Cs)*F + 0 ==> Cd*F - Cs*F + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 1100: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 1101: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 1102: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 1110: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 1111: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 1112: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 1120: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 1121: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 1122: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_SRCALPHA}, // 1200: (Cd - 0)*As + Cs ==> Cs + Cd*As + {2, D3DBLENDOP_ADD, D3DBLEND_DESTCOLOR, D3DBLEND_SRCALPHA}, // ** 1201: (Cd - 0)*As + Cd ==> Cd*(1 + As) // ffxii main menu background glow effect + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_SRCALPHA}, // 1202: (Cd - 0)*As + 0 ==> Cd*As + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_DESTALPHA}, // 1210: (Cd - 0)*Ad + Cs ==> Cs + Cd*Ad + {2, D3DBLENDOP_ADD, D3DBLEND_DESTCOLOR, D3DBLEND_DESTALPHA}, // ** 1211: (Cd - 0)*Ad + Cd ==> Cd*(1 + Ad) + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_DESTALPHA}, // 1212: (Cd - 0)*Ad + 0 ==> Cd*Ad + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_BLENDFACTOR}, // 1220: (Cd - 0)*F + Cs ==> Cs + Cd*F + {2, D3DBLENDOP_ADD, D3DBLEND_DESTCOLOR, D3DBLEND_BLENDFACTOR}, // ** 1221: (Cd - 0)*F + Cd ==> Cd*(1 + F) + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_BLENDFACTOR}, // 1222: (Cd - 0)*F + 0 ==> Cd*F + {0, D3DBLENDOP_ADD, D3DBLEND_INVSRCALPHA, D3DBLEND_ZERO}, // 2000: (0 - Cs)*As + Cs ==> Cs*(1 - As) + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_ONE}, // 2001: (0 - Cs)*As + Cd ==> Cd - Cs*As + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_SRCALPHA, D3DBLEND_ZERO}, // 2002: (0 - Cs)*As + 0 ==> 0 - Cs*As + {0, D3DBLENDOP_ADD, D3DBLEND_INVDESTALPHA, D3DBLEND_ZERO}, // 2010: (0 - Cs)*Ad + Cs ==> Cs*(1 - Ad) + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_DESTALPHA, D3DBLEND_ONE}, // 2011: (0 - Cs)*Ad + Cd ==> Cd - Cs*Ad + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_DESTALPHA, D3DBLEND_ZERO}, // 2012: (0 - Cs)*Ad + 0 ==> 0 - Cs*Ad + {0, D3DBLENDOP_ADD, D3DBLEND_INVBLENDFACTOR, D3DBLEND_ZERO}, // 2020: (0 - Cs)*F + Cs ==> Cs*(1 - F) + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_ONE}, // 2021: (0 - Cs)*F + Cd ==> Cd - Cs*F + {0, D3DBLENDOP_REVSUBTRACT, D3DBLEND_BLENDFACTOR, D3DBLEND_ZERO}, // 2022: (0 - Cs)*F + 0 ==> 0 - Cs*F + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ONE, D3DBLEND_SRCALPHA}, // 2100: (0 - Cd)*As + Cs ==> Cs - Cd*As + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_INVSRCALPHA}, // 2101: (0 - Cd)*As + Cd ==> Cd*(1 - As) + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ZERO, D3DBLEND_SRCALPHA}, // 2102: (0 - Cd)*As + 0 ==> 0 - Cd*As + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ONE, D3DBLEND_DESTALPHA}, // 2110: (0 - Cd)*Ad + Cs ==> Cs - Cd*Ad + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_INVDESTALPHA}, // 2111: (0 - Cd)*Ad + Cd ==> Cd*(1 - Ad) + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ONE, D3DBLEND_DESTALPHA}, // 2112: (0 - Cd)*Ad + 0 ==> 0 - Cd*Ad + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ONE, D3DBLEND_BLENDFACTOR}, // 2120: (0 - Cd)*F + Cs ==> Cs - Cd*F + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_INVBLENDFACTOR}, // 2121: (0 - Cd)*F + Cd ==> Cd*(1 - F) + {0, D3DBLENDOP_SUBTRACT, D3DBLEND_ONE, D3DBLEND_BLENDFACTOR}, // 2122: (0 - Cd)*F + 0 ==> 0 - Cd*F + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 2200: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 2201: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 2202: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 2210: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 2211: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 2212: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + {0, D3DBLENDOP_ADD, D3DBLEND_ONE, D3DBLEND_ZERO}, // 2220: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cs ==> Cs + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ONE}, // 2221: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + Cd ==> Cd + {0, D3DBLENDOP_ADD, D3DBLEND_ZERO, D3DBLEND_ZERO}, // 2222: (Cs/Cd/0 - Cs/Cd/0)*As/Ad/F + 0 ==> 0 + }; + + // bogus: 0100, 0110, 0120, 0200, 0210, 0220, 1001, 1011, 1021 + + // tricky: 1201, 1211, 1221 + // + // Source.rgb = float3(1, 1, 1); + // 1201 Cd*(1 + As) => Source * Dest color + Dest * Source alpha + // 1211 Cd*(1 + Ad) => Source * Dest color + Dest * Dest alpha + // 1221 Cd*(1 + F) => Source * Dest color + Dest * Factor + + int i = ((bsel.a * 3 + bsel.b) * 3 + bsel.c) * 3 + bsel.d; + + bs->BlendOp = map[i].op; + bs->SrcBlend = map[i].src; + bs->DestBlend = map[i].dst; + bs->BlendOpAlpha = D3DBLENDOP_ADD; + bs->SrcBlendAlpha = D3DBLEND_ONE; + bs->DestBlendAlpha = D3DBLEND_ZERO; + + if(map[i].bogus == 1) + { + ASSERT(0); + + (bsel.a == 0 ? bs->SrcBlend : bs->DestBlend) = D3DBLEND_ONE; + } + } + + if(bsel.wr) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_RED; + if(bsel.wg) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_GREEN; + if(bsel.wb) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_BLUE; + if(bsel.wa) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_ALPHA; + + m_om_bs.SetAt(bsel, bs); + } + + m_dev->OMSetBlendState(bs, 0x010101 * bf); +} diff --git a/plugins/GSdx/GSTextureFX9.h b/plugins/GSdx/GSTextureFX9.h new file mode 100644 index 0000000000..6b7fad206a --- /dev/null +++ b/plugins/GSdx/GSTextureFX9.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSDevice9.h" + +class GSTextureFX9 +{ +public: + #pragma pack(push, 1) + + struct VSConstantBuffer + { + GSVector4 VertexScale; + GSVector4 VertexOffset; + GSVector2 TextureScale; + float _pad[2]; + }; + + union VSSelector + { + struct + { + DWORD bppz:2; + DWORD tme:1; + DWORD fst:1; + DWORD logz:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x1f;} + }; + + struct PSConstantBuffer + { + GSVector4 FogColor; + float MINU; + float MAXU; + float MINV; + float MAXV; + DWORD UMSK; + DWORD UFIX; + DWORD VMSK; + DWORD VFIX; + float TA0; + float TA1; + float AREF; + float _pad[1]; + GSVector2 WH; + GSVector2 rWrH; + }; + + union PSSelector + { + struct + { + DWORD fst:1; + DWORD wms:2; + DWORD wmt:2; + DWORD bpp:3; + DWORD aem:1; + DWORD tfx:3; + DWORD tcc:1; + DWORD ate:1; + DWORD atst:3; + DWORD fog:1; + DWORD clr1:1; + DWORD rt:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0xfffff;} + }; + + union PSSamplerSelector + { + struct + { + DWORD tau:1; + DWORD tav:1; + DWORD min:1; + DWORD mag:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0xf;} + }; + + union OMDepthStencilSelector + { + struct + { + DWORD zte:1; + DWORD ztst:2; + DWORD zwe:1; + DWORD date:1; + DWORD fba:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x3f;} + }; + + union OMBlendSelector + { + struct + { + DWORD abe:1; + DWORD a:2; + DWORD b:2; + DWORD c:2; + DWORD d:2; + DWORD wr:1; + DWORD wg:1; + DWORD wb:1; + DWORD wa:1; + }; + + DWORD dw; + + operator DWORD() {return dw & 0x1fff;} + }; + + #pragma pack(pop) + +private: + GSDevice9* m_dev; + CComPtr m_il; + CRBMapC > m_vs; + D3DXHANDLE m_vs_params; + CRBMapC > m_ps; + CRBMapC m_ps_ss; + CRBMapC m_om_dss; + CRBMapC m_om_bs; + CRBMapC m_mskfix; + +public: + GSTextureFX9(); + + bool Create(GSDevice9* dev); + bool CreateMskFix(GSTexture9& t, DWORD size, DWORD msk, DWORD fix); + + bool SetupIA(const GSVertexHW9* vertices, UINT count, D3DPRIMITIVETYPE prim); + bool SetupVS(VSSelector sel, const VSConstantBuffer* cb); + bool SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, IDirect3DTexture9* tex, IDirect3DTexture9* pal, bool psrr); + void UpdatePS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel, bool psrr); + void SetupRS(int w, int h, const RECT& scissor); + void SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, BYTE bf, IDirect3DSurface9* rt, IDirect3DSurface9* ds); + void UpdateOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, BYTE bf); +}; diff --git a/plugins/GSdx/GSTextureNull.cpp b/plugins/GSdx/GSTextureNull.cpp new file mode 100644 index 0000000000..b2141079e1 --- /dev/null +++ b/plugins/GSdx/GSTextureNull.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSTextureNull.h" + +GSTextureNull::GSTextureNull() +{ + memset(&m_desc, 0, sizeof(m_desc)); +} + +GSTextureNull::GSTextureNull(int type, int w, int h, int format) +{ + m_desc.type = type; + m_desc.w = w; + m_desc.h = h; + m_desc.format = format; +} diff --git a/plugins/GSdx/GSTextureNull.h b/plugins/GSdx/GSTextureNull.h new file mode 100644 index 0000000000..18898b9008 --- /dev/null +++ b/plugins/GSdx/GSTextureNull.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSTexture.h" + +class GSTextureNull : public GSTexture +{ + struct {int type, w, h, format;} m_desc; + +public: + GSTextureNull(); + GSTextureNull(int type, int w, int h, int format); + + operator bool() {return m_desc.type != 0;} + + int GetType() const {return m_desc.type;} + int GetWidth() const {return m_desc.w;} + int GetHeight() const {return m_desc.h;} + int GetFormat() const {return m_desc.format;} + bool Update(const CRect& r, const void* data, int pitch) {return true;} + bool Map(BYTE** bits, int& pitch, const RECT* r = NULL) {return true;} + void Unmap() {} + bool Save(CString fn, bool dds = false) {return false;} +}; diff --git a/plugins/GSdx/GSUtil.cpp b/plugins/GSdx/GSUtil.cpp new file mode 100644 index 0000000000..bba238a38e --- /dev/null +++ b/plugins/GSdx/GSUtil.cpp @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GS.h" +#include "GSUtil.h" +#include "svnrev.h" + +static struct GSUtilMaps +{ + BYTE PrimClassField[8]; + bool CompatibleBitsField[64][64]; + bool SharedBitsField[64][64]; + + struct GSUtilMaps() + { + PrimClassField[GS_POINTLIST] = GS_POINT_CLASS; + PrimClassField[GS_LINELIST] = GS_LINE_CLASS; + PrimClassField[GS_LINESTRIP] = GS_LINE_CLASS; + PrimClassField[GS_TRIANGLELIST] = GS_TRIANGLE_CLASS; + PrimClassField[GS_TRIANGLESTRIP] = GS_TRIANGLE_CLASS; + PrimClassField[GS_TRIANGLEFAN] = GS_TRIANGLE_CLASS; + PrimClassField[GS_SPRITE] = GS_SPRITE_CLASS; + PrimClassField[GS_INVALID] = GS_INVALID_CLASS; + + memset(CompatibleBitsField, 0, sizeof(CompatibleBitsField)); + + CompatibleBitsField[PSM_PSMCT32][PSM_PSMCT24] = true; + CompatibleBitsField[PSM_PSMCT24][PSM_PSMCT32] = true; + CompatibleBitsField[PSM_PSMCT16][PSM_PSMCT16S] = true; + CompatibleBitsField[PSM_PSMCT16S][PSM_PSMCT16] = true; + CompatibleBitsField[PSM_PSMZ32][PSM_PSMZ24] = true; + CompatibleBitsField[PSM_PSMZ24][PSM_PSMZ32] = true; + CompatibleBitsField[PSM_PSMZ16][PSM_PSMZ16S] = true; + CompatibleBitsField[PSM_PSMZ16S][PSM_PSMZ16] = true; + + memset(SharedBitsField, 1, sizeof(SharedBitsField)); + + SharedBitsField[PSM_PSMCT24][PSM_PSMT8H] = false; + SharedBitsField[PSM_PSMCT24][PSM_PSMT4HL] = false; + SharedBitsField[PSM_PSMCT24][PSM_PSMT4HH] = false; + SharedBitsField[PSM_PSMZ24][PSM_PSMT8H] = false; + SharedBitsField[PSM_PSMZ24][PSM_PSMT4HL] = false; + SharedBitsField[PSM_PSMZ24][PSM_PSMT4HH] = false; + SharedBitsField[PSM_PSMT8H][PSM_PSMCT24] = false; + SharedBitsField[PSM_PSMT8H][PSM_PSMZ24] = false; + SharedBitsField[PSM_PSMT4HL][PSM_PSMCT24] = false; + SharedBitsField[PSM_PSMT4HL][PSM_PSMZ24] = false; + SharedBitsField[PSM_PSMT4HL][PSM_PSMT4HH] = false; + SharedBitsField[PSM_PSMT4HH][PSM_PSMCT24] = false; + SharedBitsField[PSM_PSMT4HH][PSM_PSMZ24] = false; + SharedBitsField[PSM_PSMT4HH][PSM_PSMT4HL] = false; + } + +} s_maps; + +GS_PRIM_CLASS GSUtil::GetPrimClass(DWORD prim) +{ + return (GS_PRIM_CLASS)s_maps.PrimClassField[prim]; +} + +bool GSUtil::HasSharedBits(DWORD spsm, DWORD dpsm) +{ + return s_maps.SharedBitsField[spsm][dpsm]; +} + +bool GSUtil::HasSharedBits(DWORD sbp, DWORD spsm, DWORD dbp, DWORD dpsm) +{ + if(sbp != dbp) return false; + + return HasSharedBits(spsm, dpsm); +} + +bool GSUtil::HasCompatibleBits(DWORD spsm, DWORD dpsm) +{ + if(spsm == dpsm) return true; + + return s_maps.CompatibleBitsField[spsm][dpsm]; +} + +bool GSUtil::IsRectInRect(const CRect& inner, const CRect& outer) +{ + return outer.left <= inner.left && inner.right <= outer.right && outer.top <= inner.top && inner.bottom <= outer.bottom; +} + +bool GSUtil::IsRectInRectH(const CRect& inner, const CRect& outer) +{ + return outer.top <= inner.top && inner.bottom <= outer.bottom; +} + +bool GSUtil::IsRectInRectV(const CRect& inner, const CRect& outer) +{ + return outer.left <= inner.left && inner.right <= outer.right; +} + +void GSUtil::FitRect(CRect& r, int aspectratio) +{ + static const int ar[][2] = {{0, 0}, {4, 3}, {16, 9}}; + + if(aspectratio <= 0 || aspectratio >= countof(ar)) + { + return; + } + + int arx = ar[aspectratio][0]; + int ary = ar[aspectratio][1]; + + CRect r2 = r; + + if(arx > 0 && ary > 0) + { + if(r.Width() * ary > r.Height() * arx) + { + int w = r.Height() * arx / ary; + r.left = r.CenterPoint().x - w / 2; + if(r.left & 1) r.left++; + r.right = r.left + w; + } + else + { + int h = r.Width() * ary / arx; + r.top = r.CenterPoint().y - h / 2; + if(r.top & 1) r.top++; + r.bottom = r.top + h; + } + } + + r &= r2; +} + +bool GSUtil::CheckDirectX() +{ + CString str; + + str.Format(_T("d3dx9_%d.dll"), D3DX_SDK_VERSION); + + if(HINSTANCE hDll = LoadLibrary(str)) + { + FreeLibrary(hDll); + } + else + { + int res = AfxMessageBox(_T("Please update DirectX!\n\nWould you like to open the download page in your browser?"), MB_YESNO); + + if(res == IDYES) + { + ShellExecute(NULL, _T("open"), _T("http://www.microsoft.com/downloads/details.aspx?FamilyId=2DA43D38-DB71-4C1B-BC6A-9B6652CD92A3"), NULL, NULL, SW_SHOWNORMAL); + } + + return false; + } + + return true; +} + +static bool _CheckSSE() +{ + __try + { + static __m128i m; + + #if _M_SSE >= 0x402 + m.m128i_i32[0] = _mm_popcnt_u32(1234); + #elif _M_SSE >= 0x401 + m = _mm_packus_epi32(m, m); + #elif _M_SSE >= 0x301 + m = _mm_alignr_epi8(m, m, 1); + #elif _M_SSE >= 0x200 + m = _mm_packs_epi32(m, m); + #endif + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + return false; + } + + return true; +} + +bool GSUtil::CheckSSE() +{ + if(!_CheckSSE()) + { + CString str; + str.Format(_T("This CPU does not support SSE %d.%02d"), _M_SSE >> 8, _M_SSE & 0xff); + AfxMessageBox(str, MB_OK); + + return false; + } + + return true; +} + +bool GSUtil::IsDirect3D10Available() +{ + if(HMODULE hModule = LoadLibrary(_T("d3d10.dll"))) + { + FreeLibrary(hModule); + + return true; + } + + return false; +} + +char* GSUtil::GetLibName() +{ + CString str; + + str.Format(_T("GSdx %d"), SVN_REV); + + if(SVN_MODS) str += _T("m"); + +#if _M_AMD64 + str += _T(" 64-bit"); +#endif + + CAtlList sl; + +#ifdef __INTEL_COMPILER + CString s; + s.Format(_T("Intel C++ %d.%02d"), __INTEL_COMPILER/100, __INTEL_COMPILER%100); + sl.AddTail(s); +#elif _MSC_VER + CString s; + s.Format(_T("MSVC %d.%02d"), _MSC_VER/100, _MSC_VER%100); + sl.AddTail(s); +#endif + +#if _M_SSE >= 0x402 + sl.AddTail(_T("SSE42")); +#elif _M_SSE >= 0x401 + sl.AddTail(_T("SSE41")); +#elif _M_SSE >= 0x301 + sl.AddTail(_T("SSSE3")); +#elif _M_SSE >= 0x200 + sl.AddTail(_T("SSE2")); +#elif _M_SSE >= 0x100 + sl.AddTail(_T("SSE")); +#endif + + POSITION pos = sl.GetHeadPosition(); + + while(pos) + { + if(pos == sl.GetHeadPosition()) str += _T(" ("); + str += sl.GetNext(pos); + str += pos ? _T(", ") : _T(")"); + } + + static char buff[256]; + strncpy(buff, CStringA(str), min(countof(buff)-1, str.GetLength())); + return buff; +} \ No newline at end of file diff --git a/plugins/GSdx/GSUtil.h b/plugins/GSdx/GSUtil.h new file mode 100644 index 0000000000..973111ff72 --- /dev/null +++ b/plugins/GSdx/GSUtil.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" + +class GSUtil +{ +public: + static GS_PRIM_CLASS GetPrimClass(DWORD prim); + + static bool HasSharedBits(DWORD spsm, DWORD dpsm); + static bool HasSharedBits(DWORD sbp, DWORD spsm, DWORD dbp, DWORD dpsm); + static bool HasCompatibleBits(DWORD spsm, DWORD dpsm); + + static bool IsRectInRect(const CRect& inner, const CRect& outer); + static bool IsRectInRectH(const CRect& inner, const CRect& outer); + static bool IsRectInRectV(const CRect& inner, const CRect& outer); + + static void FitRect(CRect& r, int aspectratio); + + static int EncodePSM(int psm) + { + switch(psm) + { + case PSM_PSMCT32: + case PSM_PSMZ32: + return 0; + case PSM_PSMCT24: + case PSM_PSMZ24: + return 1; + case PSM_PSMCT16: + case PSM_PSMCT16S: + case PSM_PSMZ16: + case PSM_PSMZ16S: + return 2; + default: + return 3; + } + } + + static bool CheckDirectX(); + static bool CheckSSE(); + + static bool IsDirect3D10Available(); + + static char* GetLibName(); +}; + diff --git a/plugins/GSdx/GSVector.cpp b/plugins/GSdx/GSVector.cpp new file mode 100644 index 0000000000..ff11789881 --- /dev/null +++ b/plugins/GSdx/GSVector.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSVector.h" + +const GSVector4 GSVector4::m_ps0123(0.0f, 1.0f, 2.0f, 3.0f); +const GSVector4 GSVector4::m_ps4567(4.0f, 5.0f, 6.0f, 7.0f); + +void GSVector4::operator = (const GSVector4i& v) +{ + m = _mm_cvtepi32_ps(v); +} + +void GSVector4i::operator = (const GSVector4& v) +{ + m = _mm_cvttps_epi32(v); +} + +GSVector4i GSVector4i::cast(const GSVector4& v) +{ + return GSVector4i(_mm_castps_si128(v.m)); +} + +GSVector4 GSVector4::cast(const GSVector4i& v) +{ + return GSVector4(_mm_castsi128_ps(v.m)); +} diff --git a/plugins/GSdx/GSVector.h b/plugins/GSdx/GSVector.h new file mode 100644 index 0000000000..4244bc0b29 --- /dev/null +++ b/plugins/GSdx/GSVector.h @@ -0,0 +1,2644 @@ +#pragma once + +// NOTE: x64 version of the _mm_set_* functions are terrible, first they store components into memory then reload in one piece (VS2008 SP1) + +#pragma pack(push, 1) + +template class GSVector2T +{ +public: + union + { + struct {T x, y;}; + struct {T r, g;}; + struct {T v[2];}; + }; + + GSVector2T() + { + } + + GSVector2T(T x, T y) + { + this->x = x; + this->y = y; + } + + GSVector2T(const GSVector2T& v) + { + *this = v; + } + + void operator = (const GSVector2T& v) + { + _mm_storel_epi64((__m128i*)this, _mm_loadl_epi64((__m128i*)&v)); + } +}; + +typedef __declspec(align(8)) GSVector2T GSVector2; +typedef __declspec(align(8)) GSVector2T GSVector2i; + +class GSVector4; + +__declspec(align(16)) class GSVector4i +{ +public: + union + { + struct {int x, y, z, w;}; + struct {int r, g, b, a;}; + int v[4]; + float f32[4]; + unsigned __int64 u64[2]; + __int8 i8[16]; + __int16 i16[8]; + __int32 i32[4]; + __int64 i64[2]; + unsigned __int8 u8[16]; + unsigned __int16 u16[8]; + unsigned __int32 u32[4]; + __m128i m; + }; + + GSVector4i() + { + } + + GSVector4i(int x, int y, int z, int w) + { + // 4 gprs + + // m = _mm_set_epi32(w, z, y, x); + + // 2 gprs + + GSVector4i xz = load(x).upl32(load(z)); + GSVector4i yw = load(y).upl32(load(w)); + + *this = xz.upl32(yw); + } + + GSVector4i(int x, int y) + { + *this = load(x).upl32(load(y)); + } + + GSVector4i(short s0, short s1, short s2, short s3, short s4, short s5, short s6, short s7) + { + m = _mm_set_epi16(s7, s6, s5, s4, s3, s2, s1, s0); + } + + GSVector4i(char b0, char b1, char b2, char b3, char b4, char b5, char b6, char b7, char b8, char b9, char b10, char b11, char b12, char b13, char b14, char b15) + { + m = _mm_set_epi8(b15, b14, b13, b12, b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0); + } + + GSVector4i(const GSVector4i& v) + { + m = v.m; + } + + explicit GSVector4i(const GSVector2i& v) + { + m = _mm_loadl_epi64((__m128i*)&v); + } + + explicit GSVector4i(int i) + { + m = _mm_set1_epi32(i); + } + + explicit GSVector4i(__m128i m) + { + this->m = m; + } + + explicit GSVector4i(const CRect& r) + { + *this = GSVector4i(r.left, r.top, r.right, r.bottom); + } + + explicit GSVector4i(const GSVector4& v) + { + *this = v; + } + + void operator = (const GSVector4i& v) + { + m = v.m; + } + + void operator = (const GSVector4& v); + + void operator = (int i) + { + m = _mm_set1_epi32(i); + } + + void operator = (__m128i m) + { + this->m = m; + } + + void operator = (const CRect& r) + { + m = GSVector4i(r); + } + + operator __m128i() const + { + return m; + } + + operator CRect() const + { + return *(CRect*)&m; + } + + UINT32 rgba32() const + { + GSVector4i v = *this; + + v = v.ps32(v); + v = v.pu16(v); + + return (UINT32)store(v); + } + + static GSVector4i cast(const GSVector4& v); + + #if _M_SSE >= 0x401 + + GSVector4i sat_i8(const GSVector4i& a, const GSVector4i& b) const + { + return max_i8(a).min_i8(b); + } + + GSVector4i sat_i8(const GSVector4i& a) const + { + return max_i8(a.xyxy()).min_i8(a.zwzw()); + } + + #endif + + GSVector4i sat_i16(const GSVector4i& a, const GSVector4i& b) const + { + return max_i16(a).min_i16(b); + } + + GSVector4i sat_i16(const GSVector4i& a) const + { + return max_i16(a.xyxy()).min_i16(a.zwzw()); + } + + #if _M_SSE >= 0x401 + + GSVector4i sat_i32(const GSVector4i& a, const GSVector4i& b) const + { + return max_i32(a).min_i32(b); + } + + GSVector4i sat_i32(const GSVector4i& a) const + { + return max_i32(a.xyxy()).min_i32(a.zwzw()); + } + + #endif + + GSVector4i sat_u8(const GSVector4i& a, const GSVector4i& b) const + { + return max_u8(a).min_u8(b); + } + + GSVector4i sat_u8(const GSVector4i& a) const + { + return max_u8(a.xyxy()).min_u8(a.zwzw()); + } + + #if _M_SSE >= 0x401 + + GSVector4i sat_u16(const GSVector4i& a, const GSVector4i& b) const + { + return max_u16(a).min_u16(b); + } + + GSVector4i sat_u16(const GSVector4i& a) const + { + return max_u16(a.xyxy()).min_u16(a.zwzw()); + } + + #endif + + #if _M_SSE >= 0x401 + + GSVector4i sat_u32(const GSVector4i& a, const GSVector4i& b) const + { + return max_u32(a).min_u32(b); + } + + GSVector4i sat_u32(const GSVector4i& a) const + { + return max_u32(a.xyxy()).min_u32(a.zwzw()); + } + + #endif + + #if _M_SSE >= 0x401 + + GSVector4i min_i8(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epi8(m, a)); + } + + GSVector4i max_i8(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epi8(m, a)); + } + + #endif + + GSVector4i min_i16(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epi16(m, a)); + } + + GSVector4i max_i16(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epi16(m, a)); + } + + #if _M_SSE >= 0x401 + + GSVector4i min_i32(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epi32(m, a)); + } + + GSVector4i max_i32(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epi32(m, a)); + } + + #endif + + GSVector4i min_u8(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epu8(m, a)); + } + + GSVector4i max_u8(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epu8(m, a)); + } + + #if _M_SSE >= 0x401 + + GSVector4i min_u16(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epu16(m, a)); + } + + GSVector4i max_u16(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epu16(m, a)); + } + + GSVector4i min_u32(const GSVector4i& a) const + { + return GSVector4i(_mm_min_epu32(m, a)); + } + + GSVector4i max_u32(const GSVector4i& a) const + { + return GSVector4i(_mm_max_epu32(m, a)); + } + + #endif + + static int min_i16(int a, int b) + { + return store(load(a).min_i16(load(b))); + } + + GSVector4i clamp8() const + { + return pu16().upl8(); + } + + GSVector4i blend8(const GSVector4i& a, const GSVector4i& mask) const + { + return GSVector4i(_mm_blendv_epi8(m, a, mask)); + } + + #if _M_SSE >= 0x401 + + template GSVector4i blend16(const GSVector4i& a) const + { + return GSVector4i(_mm_blend_epi16(m, a, mask)); + } + + #endif + + GSVector4i blend(const GSVector4i& a, const GSVector4i& mask) const + { + return GSVector4i(_mm_or_si128(_mm_andnot_si128(mask, m), _mm_and_si128(mask, a))); + } + + GSVector4i mix16(const GSVector4i& a) const + { + #if _M_SSE >= 0x401 + + return blend16<0xaa>(a); + + #else + + return blend8(a, GSVector4i::xffff0000()); + + #endif + } + + #if _M_SSE >= 0x301 + + GSVector4i shuffle8(const GSVector4i& mask) const + { + return GSVector4i(_mm_shuffle_epi8(m, mask)); + } + + #endif + + GSVector4i ps16(const GSVector4i& a) const + { + return GSVector4i(_mm_packs_epi16(m, a)); + } + + GSVector4i ps16() const + { + return GSVector4i(_mm_packs_epi16(m, m)); + } + + GSVector4i pu16(const GSVector4i& a) const + { + return GSVector4i(_mm_packus_epi16(m, a)); + } + + GSVector4i pu16() const + { + return GSVector4i(_mm_packus_epi16(m, m)); + } + + GSVector4i ps32(const GSVector4i& a) const + { + return GSVector4i(_mm_packs_epi32(m, a)); + } + + GSVector4i ps32() const + { + return GSVector4i(_mm_packs_epi32(m, m)); + } + + #if _M_SSE >= 0x401 + + GSVector4i pu32(const GSVector4i& a) const + { + return GSVector4i(_mm_packus_epi32(m, a)); + } + + GSVector4i pu32() const + { + return GSVector4i(_mm_packus_epi32(m, m)); + } + + #endif + + GSVector4i upl8(const GSVector4i& a) const + { + return GSVector4i(_mm_unpacklo_epi8(m, a)); + } + + GSVector4i uph8(const GSVector4i& a) const + { + return GSVector4i(_mm_unpackhi_epi8(m, a)); + } + + GSVector4i upl16(const GSVector4i& a) const + { + return GSVector4i(_mm_unpacklo_epi16(m, a)); + } + + GSVector4i uph16(const GSVector4i& a) const + { + return GSVector4i(_mm_unpackhi_epi16(m, a)); + } + + GSVector4i upl32(const GSVector4i& a) const + { + return GSVector4i(_mm_unpacklo_epi32(m, a)); + } + + GSVector4i uph32(const GSVector4i& a) const + { + return GSVector4i(_mm_unpackhi_epi32(m, a)); + } + + GSVector4i upl64(const GSVector4i& a) const + { + return GSVector4i(_mm_unpacklo_epi64(m, a)); + } + + GSVector4i uph64(const GSVector4i& a) const + { + return GSVector4i(_mm_unpackhi_epi64(m, a)); + } + + GSVector4i upl8() const + { + #if 0 // _M_SSE >= 0x401 // TODO: compiler bug + + return GSVector4i(_mm_cvtepu8_epi16(m)); + + #else + + return GSVector4i(_mm_unpacklo_epi8(m, _mm_setzero_si128())); + + #endif + } + + GSVector4i uph8() const + { + return GSVector4i(_mm_unpackhi_epi8(m, _mm_setzero_si128())); + } + + GSVector4i upl16() const + { + #if 0 //_M_SSE >= 0x401 // TODO: compiler bug + + return GSVector4i(_mm_cvtepu16_epi32(m)); + + #else + + return GSVector4i(_mm_unpacklo_epi16(m, _mm_setzero_si128())); + + #endif + } + + GSVector4i uph16() const + { + return GSVector4i(_mm_unpackhi_epi16(m, _mm_setzero_si128())); + } + + GSVector4i upl32() const + { + #if 0 //_M_SSE >= 0x401 // TODO: compiler bug + + return GSVector4i(_mm_cvtepu32_epi64(m)); + + #else + + return GSVector4i(_mm_unpacklo_epi32(m, _mm_setzero_si128())); + + #endif + } + + GSVector4i uph32() const + { + return GSVector4i(_mm_unpackhi_epi32(m, _mm_setzero_si128())); + } + + GSVector4i upl64() const + { + return GSVector4i(_mm_unpacklo_epi64(m, _mm_setzero_si128())); + } + + GSVector4i uph64() const + { + return GSVector4i(_mm_unpackhi_epi64(m, _mm_setzero_si128())); + } + + #if _M_SSE >= 0x401 + + // WARNING!!! + // + // MSVC (2008, 2010 ctp) believes that there is a "mem, reg" form of the pmovz/sx* instructions, + // turning these intrinsics into a minefield, don't spill regs when using them... + + GSVector4i i8to16() const + { + return GSVector4i(_mm_cvtepi8_epi16(m)); + } + + GSVector4i u8to16() const + { + return GSVector4i(_mm_cvtepu8_epi16(m)); + } + + GSVector4i i8to32() const + { + return GSVector4i(_mm_cvtepi8_epi32(m)); + } + + GSVector4i u8to32() const + { + return GSVector4i(_mm_cvtepu8_epi32(m)); + } + + GSVector4i i8to64() const + { + return GSVector4i(_mm_cvtepi8_epi64(m)); + } + + GSVector4i u8to64() const + { + return GSVector4i(_mm_cvtepu16_epi64(m)); + } + + GSVector4i i16to32() const + { + return GSVector4i(_mm_cvtepi16_epi32(m)); + } + + GSVector4i u16to32() const + { + return GSVector4i(_mm_cvtepu16_epi32(m)); + } + + GSVector4i i16to64() const + { + return GSVector4i(_mm_cvtepi16_epi64(m)); + } + + GSVector4i u16to64() const + { + return GSVector4i(_mm_cvtepu16_epi64(m)); + } + + GSVector4i i32to64() const + { + return GSVector4i(_mm_cvtepi32_epi64(m)); + } + + GSVector4i u32to64() const + { + return GSVector4i(_mm_cvtepu32_epi64(m)); + } + + #else + + GSVector4i u8to16() const + { + return upl8(); + } + + GSVector4i u8to32() const + { + return upl8().upl16(); + } + + GSVector4i u8to64() const + { + return upl8().upl16().upl32(); + } + + GSVector4i u16to32() const + { + return upl16(); + } + + GSVector4i u16to64() const + { + return upl16().upl32(); + } + + GSVector4i u32to64() const + { + return upl32(); + } + + #endif + + template GSVector4i srl() const + { + #pragma warning(push) + #pragma warning(disable: 4556) + + return GSVector4i(_mm_srli_si128(m, i)); + + #pragma warning(pop) + } + + template GSVector4i srl(const GSVector4i& v) + { + #if _M_SSE >= 0x301 + + return GSVector4i(_mm_alignr_epi8(v.m, m, i)); + + #else + + if(i == 0) return *this; + else if(i < 16) return srl() | v.sll<16 - i>(); + else if(i == 16) return v; + else if(i < 32) return v.srl(); + else return zero(); + + #endif + } + + template GSVector4i sll() const + { + #pragma warning(push) + #pragma warning(disable: 4556) + + return GSVector4i(_mm_slli_si128(m, i)); + + #pragma warning(pop) + } + + GSVector4i sra16(int i) const + { + return GSVector4i(_mm_srai_epi16(m, i)); + } + + GSVector4i sra32(int i) const + { + return GSVector4i(_mm_srai_epi32(m, i)); + } + + GSVector4i sll16(int i) const + { + return GSVector4i(_mm_slli_epi16(m, i)); + } + + GSVector4i sll32(int i) const + { + return GSVector4i(_mm_slli_epi32(m, i)); + } + + GSVector4i sll64(int i) const + { + return GSVector4i(_mm_slli_epi64(m, i)); + } + + GSVector4i srl16(int i) const + { + return GSVector4i(_mm_srli_epi16(m, i)); + } + + GSVector4i srl32(int i) const + { + return GSVector4i(_mm_srli_epi32(m, i)); + } + + GSVector4i srl64(int i) const + { + return GSVector4i(_mm_srli_epi64(m, i)); + } + + GSVector4i add8(const GSVector4i& v) const + { + return GSVector4i(_mm_add_epi8(m, v.m)); + } + + GSVector4i add16(const GSVector4i& v) const + { + return GSVector4i(_mm_add_epi16(m, v.m)); + } + + GSVector4i add32(const GSVector4i& v) const + { + return GSVector4i(_mm_add_epi32(m, v.m)); + } + + GSVector4i adds8(const GSVector4i& v) const + { + return GSVector4i(_mm_adds_epi8(m, v.m)); + } + + GSVector4i adds16(const GSVector4i& v) const + { + return GSVector4i(_mm_adds_epi16(m, v.m)); + } + + GSVector4i addus8(const GSVector4i& v) const + { + return GSVector4i(_mm_adds_epu8(m, v.m)); + } + + GSVector4i addus16(const GSVector4i& v) const + { + return GSVector4i(_mm_adds_epu16(m, v.m)); + } + + GSVector4i sub8(const GSVector4i& v) const + { + return GSVector4i(_mm_sub_epi8(m, v.m)); + } + + GSVector4i sub16(const GSVector4i& v) const + { + return GSVector4i(_mm_sub_epi16(m, v.m)); + } + + GSVector4i sub32(const GSVector4i& v) const + { + return GSVector4i(_mm_sub_epi32(m, v.m)); + } + + GSVector4i subs8(const GSVector4i& v) const + { + return GSVector4i(_mm_subs_epi8(m, v.m)); + } + + GSVector4i subs16(const GSVector4i& v) const + { + return GSVector4i(_mm_subs_epi16(m, v.m)); + } + + GSVector4i subus8(const GSVector4i& v) const + { + return GSVector4i(_mm_subs_epu8(m, v.m)); + } + + GSVector4i subus16(const GSVector4i& v) const + { + return GSVector4i(_mm_subs_epu16(m, v.m)); + } + + GSVector4i avg8(const GSVector4i& v) const + { + return GSVector4i(_mm_avg_epu8(m, v.m)); + } + + GSVector4i avg16(const GSVector4i& v) const + { + return GSVector4i(_mm_avg_epu16(m, v.m)); + } + + GSVector4i mul16hs(const GSVector4i& v) const + { + return GSVector4i(_mm_mulhi_epi16(m, v.m)); + } + + GSVector4i mul16hu(const GSVector4i& v) const + { + return GSVector4i(_mm_mulhi_epu16(m, v.m)); + } + + GSVector4i mul16l(const GSVector4i& v) const + { + return GSVector4i(_mm_mullo_epi16(m, v.m)); + } + + #if _M_SSE >= 0x301 + + GSVector4i mul16hrs(const GSVector4i& v) const + { + return GSVector4i(_mm_mulhrs_epi16(m, v.m)); + } + + #endif + + template GSVector4i lerp16(const GSVector4i& a, const GSVector4i& f) const + { + // (a - this) * f << shift + this + + return add16(a.sub16(*this).modulate16(f)); + } + + template static GSVector4i lerp16(const GSVector4i& a, const GSVector4i& b, const GSVector4i& c) + { + // (a - b) * c << shift + + return a.sub16(b).modulate16(c); + } + + template static GSVector4i lerp16(const GSVector4i& a, const GSVector4i& b, const GSVector4i& c, const GSVector4i& d) + { + // (a - b) * c << shift + d + + return d.add16(a.sub16(b).modulate16(c)); + } + + template GSVector4i modulate16(const GSVector4i& f) const + { + // a * f << shift + + #if _M_SSE >= 0x301 + + if(shift == 0) + { + return mul16hrs(f); + } + + #endif + + return sll16(shift + 1).mul16hs(f); + } + + GSVector4i eq8(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpeq_epi8(m, v.m)); + } + + GSVector4i eq16(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpeq_epi16(m, v.m)); + } + + GSVector4i eq32(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpeq_epi32(m, v.m)); + } + + GSVector4i neq8(const GSVector4i& v) const + { + return ~eq8(v); + } + + GSVector4i neq16(const GSVector4i& v) const + { + return ~eq16(v); + } + + GSVector4i neq32(const GSVector4i& v) const + { + return ~eq32(v); + } + + GSVector4i gt8(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpgt_epi8(m, v.m)); + } + + GSVector4i gt16(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpgt_epi16(m, v.m)); + } + + GSVector4i gt32(const GSVector4i& v) const + { + return GSVector4i(_mm_cmpgt_epi32(m, v.m)); + } + + GSVector4i lt8(const GSVector4i& v) const + { + return GSVector4i(_mm_cmplt_epi8(m, v.m)); + } + + GSVector4i lt16(const GSVector4i& v) const + { + return GSVector4i(_mm_cmplt_epi16(m, v.m)); + } + + GSVector4i lt32(const GSVector4i& v) const + { + return GSVector4i(_mm_cmplt_epi32(m, v.m)); + } + + GSVector4i andnot(const GSVector4i& v) const + { + return GSVector4i(_mm_andnot_si128(v.m, m)); + } + + int mask() const + { + return _mm_movemask_epi8(m); + } + + bool alltrue() const + { + return _mm_movemask_epi8(m) == 0xffff; + } + + bool anytrue() const + { + return _mm_movemask_epi8(m) != 0x0000; + } + + #if _M_SSE >= 0x401 + + template GSVector4i insert8(int a) const + { + return GSVector4i(_mm_insert_epi8(m, a, i)); + } + + #endif + + template int extract8() const + { + #if _M_SSE >= 0x401 + return _mm_extract_epi8(m, i); + #else + return (int)u8[i]; + #endif + } + + template GSVector4i insert16(int a) const + { + return GSVector4i(_mm_insert_epi16(m, a, i)); + } + + template int extract16() const + { + return _mm_extract_epi16(m, i); + } + + #if _M_SSE >= 0x401 + + template GSVector4i insert32(int a) const + { + return GSVector4i(_mm_insert_epi32(m, a, i)); + } + + #endif + + template int extract32() const + { + if(i == 0) return GSVector4i::store(*this); + #if _M_SSE >= 0x401 + return _mm_extract_epi32(m, i); + #else + return i32[i]; + #endif + } + + #ifdef _M_AMD64 + + #if _M_SSE >= 0x401 + + template GSVector4i insert64(__int64 a) const + { + return GSVector4i(_mm_insert_epi64(m, a, i)); + } + + #endif + + template __int64 extract64() const + { + if(i == 0) return GSVector4i::storeq(*this); + #if _M_SSE >= 0x401 + return _mm_extract_epi64(m, i); + #else + return i64[i]; + #endif + } + + #endif + + #if _M_SSE >= 0x401 + + template __forceinline GSVector4i gather8_4(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8() & 0xf]); + v = v.insert8<1>((int)ptr[extract8() >> 4]); + v = v.insert8<2>((int)ptr[extract8() & 0xf]); + v = v.insert8<3>((int)ptr[extract8() >> 4]); + v = v.insert8<4>((int)ptr[extract8() & 0xf]); + v = v.insert8<5>((int)ptr[extract8() >> 4]); + v = v.insert8<6>((int)ptr[extract8() & 0xf]); + v = v.insert8<7>((int)ptr[extract8() >> 4]); + v = v.insert8<8>((int)ptr[extract8() & 0xf]); + v = v.insert8<9>((int)ptr[extract8() >> 4]); + v = v.insert8<10>((int)ptr[extract8() & 0xf]); + v = v.insert8<11>((int)ptr[extract8() >> 4]); + v = v.insert8<12>((int)ptr[extract8() & 0xf]); + v = v.insert8<13>((int)ptr[extract8() >> 4]); + v = v.insert8<14>((int)ptr[extract8() & 0xf]); + v = v.insert8<15>((int)ptr[extract8() >> 4]); + + return v; + } + + template __forceinline GSVector4i gather8_8(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8<0>()]); + v = v.insert8<1>((int)ptr[extract8<1>()]); + v = v.insert8<2>((int)ptr[extract8<2>()]); + v = v.insert8<3>((int)ptr[extract8<3>()]); + v = v.insert8<4>((int)ptr[extract8<4>()]); + v = v.insert8<5>((int)ptr[extract8<5>()]); + v = v.insert8<6>((int)ptr[extract8<6>()]); + v = v.insert8<7>((int)ptr[extract8<7>()]); + v = v.insert8<8>((int)ptr[extract8<8>()]); + v = v.insert8<9>((int)ptr[extract8<9>()]); + v = v.insert8<10>((int)ptr[extract8<10>()]); + v = v.insert8<11>((int)ptr[extract8<11>()]); + v = v.insert8<12>((int)ptr[extract8<12>()]); + v = v.insert8<13>((int)ptr[extract8<13>()]); + v = v.insert8<14>((int)ptr[extract8<14>()]); + v = v.insert8<15>((int)ptr[extract8<15>()]); + + return v; + } + + template __forceinline GSVector4i gather8_16(const T* ptr, const GSVector4i& a) const + { + GSVector4i v = a; + + v = v.insert8((int)ptr[extract16<0>()]); + v = v.insert8((int)ptr[extract16<1>()]); + v = v.insert8((int)ptr[extract16<2>()]); + v = v.insert8((int)ptr[extract16<3>()]); + v = v.insert8((int)ptr[extract16<4>()]); + v = v.insert8((int)ptr[extract16<5>()]); + v = v.insert8((int)ptr[extract16<6>()]); + v = v.insert8((int)ptr[extract16<7>()]); + + return v; + } + + template __forceinline GSVector4i gather8_32(const T* ptr, const GSVector4i& a) const + { + GSVector4i v = a; + + v = v.insert8((int)ptr[extract32<0>()]); + v = v.insert8((int)ptr[extract32<1>()]); + v = v.insert8((int)ptr[extract32<2>()]); + v = v.insert8((int)ptr[extract32<3>()]); + + return v; + } + + #endif + + template __forceinline GSVector4i gather16_4(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8() & 0xf]); + v = v.insert16<1>((int)ptr[extract8() >> 4]); + v = v.insert16<2>((int)ptr[extract8() & 0xf]); + v = v.insert16<3>((int)ptr[extract8() >> 4]); + v = v.insert16<4>((int)ptr[extract8() & 0xf]); + v = v.insert16<5>((int)ptr[extract8() >> 4]); + v = v.insert16<6>((int)ptr[extract8() & 0xf]); + v = v.insert16<7>((int)ptr[extract8() >> 4]); + + return v; + } + + template __forceinline GSVector4i gather16_8(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8()]); + v = v.insert16<1>((int)ptr[extract8()]); + v = v.insert16<2>((int)ptr[extract8()]); + v = v.insert16<3>((int)ptr[extract8()]); + v = v.insert16<4>((int)ptr[extract8()]); + v = v.insert16<5>((int)ptr[extract8()]); + v = v.insert16<6>((int)ptr[extract8()]); + v = v.insert16<7>((int)ptr[extract8()]); + + return v; + } + + template__forceinline GSVector4i gather16_16(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract16<0>()]); + v = v.insert16<1>((int)ptr[extract16<1>()]); + v = v.insert16<2>((int)ptr[extract16<2>()]); + v = v.insert16<3>((int)ptr[extract16<3>()]); + v = v.insert16<4>((int)ptr[extract16<4>()]); + v = v.insert16<5>((int)ptr[extract16<5>()]); + v = v.insert16<6>((int)ptr[extract16<6>()]); + v = v.insert16<7>((int)ptr[extract16<7>()]); + + return v; + } + + template__forceinline GSVector4i gather16_16(const T1* ptr1, const T2* ptr2) const + { + GSVector4i v; + + v = load((int)ptr2[ptr1[extract16<0>()]]); + v = v.insert16<1>((int)ptr2[ptr1[extract16<1>()]]); + v = v.insert16<2>((int)ptr2[ptr1[extract16<2>()]]); + v = v.insert16<3>((int)ptr2[ptr1[extract16<3>()]]); + v = v.insert16<4>((int)ptr2[ptr1[extract16<4>()]]); + v = v.insert16<5>((int)ptr2[ptr1[extract16<5>()]]); + v = v.insert16<6>((int)ptr2[ptr1[extract16<6>()]]); + v = v.insert16<7>((int)ptr2[ptr1[extract16<7>()]]); + + return v; + } + + template __forceinline GSVector4i gather16_32(const T* ptr, const GSVector4i& a) const + { + GSVector4i v = a; + + v = v.insert16((int)ptr[extract32<0>()]); + v = v.insert16((int)ptr[extract32<1>()]); + v = v.insert16((int)ptr[extract32<2>()]); + v = v.insert16((int)ptr[extract32<3>()]); + + return v; + } + + #if _M_SSE >= 0x401 + + template __forceinline GSVector4i gather32_4(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8() & 0xf]); + v = v.insert32<1>((int)ptr[extract8() >> 4]); + v = v.insert32<2>((int)ptr[extract8() & 0xf]); + v = v.insert32<3>((int)ptr[extract8() >> 4]); + return v; + } + + template __forceinline GSVector4i gather32_8(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract8()]); + v = v.insert32<1>((int)ptr[extract8()]); + v = v.insert32<2>((int)ptr[extract8()]); + v = v.insert32<3>((int)ptr[extract8()]); + + return v; + } + + template __forceinline GSVector4i gather32_16(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract16()]); + v = v.insert32<1>((int)ptr[extract16()]); + v = v.insert32<2>((int)ptr[extract16()]); + v = v.insert32<3>((int)ptr[extract16()]); + + return v; + } + + template __forceinline GSVector4i gather32_32(const T* ptr) const + { + GSVector4i v; + + v = load((int)ptr[extract32<0>()]); + v = v.insert32<1>((int)ptr[extract32<1>()]); + v = v.insert32<2>((int)ptr[extract32<2>()]); + v = v.insert32<3>((int)ptr[extract32<3>()]); + + return v; + } + + template __forceinline GSVector4i gather32_32(const T1* ptr1, const T2* ptr2) const + { + GSVector4i v; + + v = load((int)ptr2[ptr1[extract32<0>()]]); + v = v.insert32<1>((int)ptr2[ptr1[extract32<1>()]]); + v = v.insert32<2>((int)ptr2[ptr1[extract32<2>()]]); + v = v.insert32<3>((int)ptr2[ptr1[extract32<3>()]]); + + return v; + } + + #else + + template __forceinline GSVector4i gather32_4(const T* ptr) const + { + return GSVector4i( + (int)ptr[extract8() & 0xf], + (int)ptr[extract8() >> 4], + (int)ptr[extract8() & 0xf], + (int)ptr[extract8() >> 4]); + } + + template __forceinline GSVector4i gather32_8(const T* ptr) const + { + return GSVector4i( + (int)ptr[extract8()], + (int)ptr[extract8()], + (int)ptr[extract8()], + (int)ptr[extract8()]); + } + + template __forceinline GSVector4i gather32_16(const T* ptr) const + { + return GSVector4i( + (int)ptr[extract16()], + (int)ptr[extract16()], + (int)ptr[extract16()], + (int)ptr[extract16()]); + } + + template __forceinline GSVector4i gather32_32(const T* ptr) const + { + return GSVector4i( + (int)ptr[extract32<0>()], + (int)ptr[extract32<1>()], + (int)ptr[extract32<2>()], + (int)ptr[extract32<3>()]); + } + + template __forceinline GSVector4i gather32_32(const T1* ptr1, const T2* ptr2) const + { + return GSVector4i( + (int)ptr2[ptr1[extract32<0>()]], + (int)ptr2[ptr1[extract32<1>()]], + (int)ptr2[ptr1[extract32<2>()]], + (int)ptr2[ptr1[extract32<3>()]]); + } + + #endif + + #if defined(_M_AMD64) && _M_SSE >= 0x401 + + template __forceinline GSVector4i gather64_4(const T* ptr) const + { + GSVector4i v; + + v = loadq((__int64)ptr[extract8() & 0xf]); + v = v.insert64<1>((__int64)ptr[extract8() >> 4]); + + return v; + } + + template __forceinline GSVector4i gather64_8(const T* ptr) const + { + GSVector4i v; + + v = loadq((__int64)ptr[extract8()]); + v = v.insert64<1>((__int64)ptr[extract8()]); + + return v; + } + + template __forceinline GSVector4i gather64_16(const T* ptr) const + { + GSVector4i v; + + v = loadq((__int64)ptr[extract16()]); + v = v.insert64<1>((__int64)ptr[extract16()]); + + return v; + } + + template __forceinline GSVector4i gather64_32(const T* ptr) const + { + GSVector4i v; + + v = loadq((__int64)ptr[extract32()]); + v = v.insert64<1>((__int64)ptr[extract32()]); + + return v; + } + + template __forceinline GSVector4i gather64_64(const T* ptr) const + { + GSVector4i v; + + v = loadq((__int64)ptr[extract64<0>()]); + v = v.insert64<1>((__int64)ptr[extract64<1>()]); + + return v; + } + + #else + + template __forceinline GSVector4i gather64_4(const T* ptr) const + { + GSVector4i v; + + v = loadu(&ptr[extract8() & 0xf], &ptr[extract8() >> 4]); + + return v; + } + + template __forceinline GSVector4i gather64_8(const T* ptr) const + { + GSVector4i v; + + v = load(&ptr[extract8()], &ptr[extract8()]); + + return v; + } + + template __forceinline GSVector4i gather64_16(const T* ptr) const + { + GSVector4i v; + + v = load(&ptr[extract16()], &ptr[extract16()]); + + return v; + } + + template __forceinline GSVector4i gather64_32(const T* ptr) const + { + GSVector4i v; + + v = load(&ptr[extract32()], &ptr[extract32()]); + + return v; + } + + #endif + + #if _M_SSE >= 0x401 + + template __forceinline void gather8_4(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather8_4<0>(ptr); + dst[1] = gather8_4<8>(ptr); + } + + __forceinline void gather8_8(const BYTE* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather8_8<>(ptr); + } + + #endif + + template __forceinline void gather16_4(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather16_4<0>(ptr); + dst[1] = gather16_4<4>(ptr); + dst[2] = gather16_4<8>(ptr); + dst[3] = gather16_4<12>(ptr); + } + + template __forceinline void gather16_8(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather16_8<0>(ptr); + dst[1] = gather16_8<8>(ptr); + } + + template __forceinline void gather16_16(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather16_16<>(ptr); + } + + template __forceinline void gather32_4(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather32_4<0>(ptr); + dst[1] = gather32_4<2>(ptr); + dst[2] = gather32_4<4>(ptr); + dst[3] = gather32_4<6>(ptr); + dst[4] = gather32_4<8>(ptr); + dst[5] = gather32_4<10>(ptr); + dst[6] = gather32_4<12>(ptr); + dst[7] = gather32_4<14>(ptr); + } + + template __forceinline void gather32_8(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather32_8<0>(ptr); + dst[1] = gather32_8<4>(ptr); + dst[2] = gather32_8<8>(ptr); + dst[3] = gather32_8<12>(ptr); + } + + template __forceinline void gather32_16(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather32_16<0>(ptr); + dst[1] = gather32_16<4>(ptr); + } + + template __forceinline void gather32_32(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather32_32<>(ptr); + } + + template __forceinline void gather64_4(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather64_4<0>(ptr); + dst[1] = gather64_4<1>(ptr); + dst[2] = gather64_4<2>(ptr); + dst[3] = gather64_4<3>(ptr); + dst[4] = gather64_4<4>(ptr); + dst[5] = gather64_4<5>(ptr); + dst[6] = gather64_4<6>(ptr); + dst[7] = gather64_4<7>(ptr); + dst[8] = gather64_4<8>(ptr); + dst[9] = gather64_4<9>(ptr); + dst[10] = gather64_4<10>(ptr); + dst[11] = gather64_4<11>(ptr); + dst[12] = gather64_4<12>(ptr); + dst[13] = gather64_4<13>(ptr); + dst[14] = gather64_4<14>(ptr); + dst[15] = gather64_4<15>(ptr); + } + + template __forceinline void gather64_8(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather64_8<0>(ptr); + dst[1] = gather64_8<2>(ptr); + dst[2] = gather64_8<4>(ptr); + dst[3] = gather64_8<6>(ptr); + dst[4] = gather64_8<8>(ptr); + dst[5] = gather64_8<10>(ptr); + dst[6] = gather64_8<12>(ptr); + dst[7] = gather64_8<14>(ptr); + } + + template __forceinline void gather64_16(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather64_16<0>(ptr); + dst[1] = gather64_16<2>(ptr); + dst[2] = gather64_16<4>(ptr); + dst[3] = gather64_16<8>(ptr); + } + + template __forceinline void gather64_32(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather64_32<0>(ptr); + dst[1] = gather64_32<2>(ptr); + } + + #ifdef _M_AMD64 + + template __forceinline void gather64_64(const T* RESTRICT ptr, GSVector4i* RESTRICT dst) const + { + dst[0] = gather64_64<>(ptr); + } + + #endif + + #if _M_SSE >= 0x401 + + static GSVector4i loadnt(const void* p) + { + return GSVector4i(_mm_stream_load_si128((__m128i*)p)); + } + + #endif + + static GSVector4i loadl(const void* p) + { + return GSVector4i(_mm_loadl_epi64((__m128i*)p)); + } + + static GSVector4i loadh(const void* p) + { + return GSVector4i(_mm_castps_si128(_mm_loadh_pi(_mm_setzero_ps(), (__m64*)p))); + } + + static GSVector4i loadh(const void* p, const GSVector4i& v) + { + return GSVector4i(_mm_castps_si128(_mm_loadh_pi(_mm_castsi128_ps(v.m), (__m64*)p))); + } + + static GSVector4i load(const void* pl, const void* ph) + { + return loadh(ph, loadl(pl)); + } +/* + static GSVector4i load(const void* pl, const void* ph) + { + __m128i lo = _mm_loadl_epi64((__m128i*)pl); + __m128i hi = _mm_loadl_epi64((__m128i*)ph); + + return GSVector4i(_mm_unpacklo_epi64(lo, hi)); + } +*/ + template static GSVector4i load(const void* p) + { + return GSVector4i(aligned ? _mm_load_si128((__m128i*)p) : _mm_loadu_si128((__m128i*)p)); + } + + static GSVector4i load(int i) + { + return GSVector4i(_mm_cvtsi32_si128(i)); + } + + #ifdef _M_AMD64 + + static GSVector4i loadq(__int64 i) + { + return GSVector4i(_mm_cvtsi64_si128(i)); + } + + #endif + + static void storent(void* p, const GSVector4i& v) + { + _mm_stream_si128((__m128i*)p, v.m); + } + + static void storel(void* p, const GSVector4i& v) + { + _mm_storel_epi64((__m128i*)p, v.m); + } + + static void storeh(void* p, const GSVector4i& v) + { + _mm_storeh_pi((__m64*)p, _mm_castsi128_ps(v.m)); + } + + static void store(void* pl, void* ph, const GSVector4i& v) + { + GSVector4i::storel(pl, v); + GSVector4i::storeh(ph, v); + } + + template static void store(void* p, const GSVector4i& v) + { + if(aligned) _mm_store_si128((__m128i*)p, v.m); + else _mm_storeu_si128((__m128i*)p, v.m); + } + + static int store(const GSVector4i& v) + { + return _mm_cvtsi128_si32(v.m); + } + + #ifdef _M_AMD64 + + static __int64 storeq(const GSVector4i& v) + { + return _mm_cvtsi128_si64(v.m); + } + + #endif + + __forceinline static void transpose(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + _MM_TRANSPOSE4_SI128(a.m, b.m, c.m, d.m); + } + + __forceinline static void sw4(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + const __m128i epi32_0f0f0f0f = _mm_set1_epi32(0x0f0f0f0f); + + GSVector4i mask(epi32_0f0f0f0f); + + GSVector4i e = (b << 4).blend(a, mask); + GSVector4i f = b.blend(a >> 4, mask); + GSVector4i g = (d << 4).blend(c, mask); + GSVector4i h = d.blend(c >> 4, mask); + + a = e.upl8(f); + c = e.uph8(f); + b = g.upl8(h); + d = g.uph8(h); + } + + __forceinline static void sw8(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = e.upl8(b); + c = e.uph8(b); + b = f.upl8(d); + d = f.uph8(d); + } + + __forceinline static void sw16(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = e.upl16(b); + c = e.uph16(b); + b = f.upl16(d); + d = f.uph16(d); + } + + __forceinline static void sw16rl(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = b.upl16(e); + c = e.uph16(b); + b = d.upl16(f); + d = f.uph16(d); + } + + __forceinline static void sw16rh(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = e.upl16(b); + c = b.uph16(e); + b = f.upl16(d); + d = d.uph16(f); + } + + __forceinline static void sw32(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = e.upl32(b); + c = e.uph32(b); + b = f.upl32(d); + d = f.uph32(d); + } + + __forceinline static void sw64(GSVector4i& a, GSVector4i& b, GSVector4i& c, GSVector4i& d) + { + GSVector4i e = a; + GSVector4i f = c; + + a = e.upl64(b); + c = e.uph64(b); + b = f.upl64(d); + d = f.uph64(d); + } + + __forceinline static bool compare(const void* dst, const void* src, int size) + { + ASSERT((size & 15) == 0); + + size >>= 4; + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i v = GSVector4i::xffffffff(); + + for(int i = 0; i < size; i++) + { + v &= d[i] == s[i]; + } + + return v.alltrue(); + } + + __forceinline static bool update(const void* dst, const void* src, int size) + { + ASSERT((size & 15) == 0); + + size >>= 4; + + GSVector4i* s = (GSVector4i*)src; + GSVector4i* d = (GSVector4i*)dst; + + GSVector4i v = GSVector4i::xffffffff(); + + for(int i = 0; i < size; i++) + { + v &= d[i] == s[i]; + + d[i] = s[i]; + } + + return v.alltrue(); + } + + void operator += (const GSVector4i& v) + { + m = _mm_add_epi32(m, v); + } + + void operator -= (const GSVector4i& v) + { + m = _mm_sub_epi32(m, v); + } + + void operator += (int i) + { + *this += GSVector4i(i); + } + + void operator -= (int i) + { + *this -= GSVector4i(i); + } + + void operator <<= (const int i) + { + m = _mm_slli_epi32(m, i); + } + + void operator >>= (const int i) + { + m = _mm_srli_epi32(m, i); + } + + void operator &= (const GSVector4i& v) + { + m = _mm_and_si128(m, v); + } + + void operator |= (const GSVector4i& v) + { + m = _mm_or_si128(m, v); + } + + void operator ^= (const GSVector4i& v) + { + m = _mm_xor_si128(m, v); + } + + friend GSVector4i operator + (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_add_epi32(v1, v2)); + } + + friend GSVector4i operator - (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_sub_epi32(v1, v2)); + } + + friend GSVector4i operator + (const GSVector4i& v, int i) + { + return v + GSVector4i(i); + } + + friend GSVector4i operator - (const GSVector4i& v, int i) + { + return v - GSVector4i(i); + } + + friend GSVector4i operator << (const GSVector4i& v, const int i) + { + return GSVector4i(_mm_slli_epi32(v, i)); + } + + friend GSVector4i operator >> (const GSVector4i& v, const int i) + { + return GSVector4i(_mm_srli_epi32(v, i)); + } + + friend GSVector4i operator & (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_and_si128(v1, v2)); + } + + friend GSVector4i operator | (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_or_si128(v1, v2)); + } + + friend GSVector4i operator ^ (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_xor_si128(v1, v2)); + } + + friend GSVector4i operator & (const GSVector4i& v, int i) + { + return v & GSVector4i(i); + } + + friend GSVector4i operator | (const GSVector4i& v, int i) + { + return v | GSVector4i(i); + } + + friend GSVector4i operator ^ (const GSVector4i& v, int i) + { + return v ^ GSVector4i(i); + } + + friend GSVector4i operator ~ (const GSVector4i& v) + { + return v ^ (v == v); + } + + friend GSVector4i operator == (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_cmpeq_epi32(v1, v2)); + } + + friend GSVector4i operator != (const GSVector4i& v1, const GSVector4i& v2) + { + return ~(v1 == v2); + } + + friend GSVector4i operator > (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_cmpgt_epi32(v1, v2)); + } + + friend GSVector4i operator < (const GSVector4i& v1, const GSVector4i& v2) + { + return GSVector4i(_mm_cmplt_epi32(v1, v2)); + } + + friend GSVector4i operator >= (const GSVector4i& v1, const GSVector4i& v2) + { + return (v1 > v2) | (v1 == v2); + } + + friend GSVector4i operator <= (const GSVector4i& v1, const GSVector4i& v2) + { + return (v1 < v2) | (v1 == v2); + } + + template GSVector4i shuffle() const + { + return GSVector4i(_mm_shuffle_epi32(m, _MM_SHUFFLE(i, i, i, i))); + } + + #define VECTOR4i_SHUFFLE_4(xs, xn, ys, yn, zs, zn, ws, wn) \ + GSVector4i xs##ys##zs##ws() const {return GSVector4i(_mm_shuffle_epi32(m, _MM_SHUFFLE(wn, zn, yn, xn)));} \ + GSVector4i xs##ys##zs##ws##l() const {return GSVector4i(_mm_shufflelo_epi16(m, _MM_SHUFFLE(wn, zn, yn, xn)));} \ + GSVector4i xs##ys##zs##ws##h() const {return GSVector4i(_mm_shufflehi_epi16(m, _MM_SHUFFLE(wn, zn, yn, xn)));} \ + GSVector4i xs##ys##zs##ws##lh() const {return GSVector4i(_mm_shufflehi_epi16(_mm_shufflelo_epi16(m, _MM_SHUFFLE(wn, zn, yn, xn)), _MM_SHUFFLE(wn, zn, yn, xn)));} \ + + #define VECTOR4i_SHUFFLE_3(xs, xn, ys, yn, zs, zn) \ + VECTOR4i_SHUFFLE_4(xs, xn, ys, yn, zs, zn, x, 0) \ + VECTOR4i_SHUFFLE_4(xs, xn, ys, yn, zs, zn, y, 1) \ + VECTOR4i_SHUFFLE_4(xs, xn, ys, yn, zs, zn, z, 2) \ + VECTOR4i_SHUFFLE_4(xs, xn, ys, yn, zs, zn, w, 3) \ + + #define VECTOR4i_SHUFFLE_2(xs, xn, ys, yn) \ + VECTOR4i_SHUFFLE_3(xs, xn, ys, yn, x, 0) \ + VECTOR4i_SHUFFLE_3(xs, xn, ys, yn, y, 1) \ + VECTOR4i_SHUFFLE_3(xs, xn, ys, yn, z, 2) \ + VECTOR4i_SHUFFLE_3(xs, xn, ys, yn, w, 3) \ + + #define VECTOR4i_SHUFFLE_1(xs, xn) \ + GSVector4i xs##4##() const {return GSVector4i(_mm_shuffle_epi32(m, _MM_SHUFFLE(xn, xn, xn, xn)));} \ + GSVector4i xs##4##l() const {return GSVector4i(_mm_shufflelo_epi16(m, _MM_SHUFFLE(xn, xn, xn, xn)));} \ + GSVector4i xs##4##h() const {return GSVector4i(_mm_shufflehi_epi16(m, _MM_SHUFFLE(xn, xn, xn, xn)));} \ + VECTOR4i_SHUFFLE_2(xs, xn, x, 0) \ + VECTOR4i_SHUFFLE_2(xs, xn, y, 1) \ + VECTOR4i_SHUFFLE_2(xs, xn, z, 2) \ + VECTOR4i_SHUFFLE_2(xs, xn, w, 3) \ + + VECTOR4i_SHUFFLE_1(x, 0) + VECTOR4i_SHUFFLE_1(y, 1) + VECTOR4i_SHUFFLE_1(z, 2) + VECTOR4i_SHUFFLE_1(w, 3) + + static GSVector4i zero() {return GSVector4i(_mm_setzero_si128());} + + static GSVector4i xffffffff() {return zero() == zero();} + + static GSVector4i x00000001() {return xffffffff().srl32(31);} + static GSVector4i x00000003() {return xffffffff().srl32(30);} + static GSVector4i x00000007() {return xffffffff().srl32(29);} + static GSVector4i x0000000f() {return xffffffff().srl32(28);} + static GSVector4i x0000001f() {return xffffffff().srl32(27);} + static GSVector4i x0000003f() {return xffffffff().srl32(26);} + static GSVector4i x0000007f() {return xffffffff().srl32(25);} + static GSVector4i x000000ff() {return xffffffff().srl32(24);} + static GSVector4i x000001ff() {return xffffffff().srl32(23);} + static GSVector4i x000003ff() {return xffffffff().srl32(22);} + static GSVector4i x000007ff() {return xffffffff().srl32(21);} + static GSVector4i x00000fff() {return xffffffff().srl32(20);} + static GSVector4i x00001fff() {return xffffffff().srl32(19);} + static GSVector4i x00003fff() {return xffffffff().srl32(18);} + static GSVector4i x00007fff() {return xffffffff().srl32(17);} + static GSVector4i x0000ffff() {return xffffffff().srl32(16);} + static GSVector4i x0001ffff() {return xffffffff().srl32(15);} + static GSVector4i x0003ffff() {return xffffffff().srl32(14);} + static GSVector4i x0007ffff() {return xffffffff().srl32(13);} + static GSVector4i x000fffff() {return xffffffff().srl32(12);} + static GSVector4i x001fffff() {return xffffffff().srl32(11);} + static GSVector4i x003fffff() {return xffffffff().srl32(10);} + static GSVector4i x007fffff() {return xffffffff().srl32( 9);} + static GSVector4i x00ffffff() {return xffffffff().srl32( 8);} + static GSVector4i x01ffffff() {return xffffffff().srl32( 7);} + static GSVector4i x03ffffff() {return xffffffff().srl32( 6);} + static GSVector4i x07ffffff() {return xffffffff().srl32( 5);} + static GSVector4i x0fffffff() {return xffffffff().srl32( 4);} + static GSVector4i x1fffffff() {return xffffffff().srl32( 3);} + static GSVector4i x3fffffff() {return xffffffff().srl32( 2);} + static GSVector4i x7fffffff() {return xffffffff().srl32( 1);} + + static GSVector4i x80000000() {return xffffffff().sll32(31);} + static GSVector4i xc0000000() {return xffffffff().sll32(30);} + static GSVector4i xe0000000() {return xffffffff().sll32(29);} + static GSVector4i xf0000000() {return xffffffff().sll32(28);} + static GSVector4i xf8000000() {return xffffffff().sll32(27);} + static GSVector4i xfc000000() {return xffffffff().sll32(26);} + static GSVector4i xfe000000() {return xffffffff().sll32(25);} + static GSVector4i xff000000() {return xffffffff().sll32(24);} + static GSVector4i xff800000() {return xffffffff().sll32(23);} + static GSVector4i xffc00000() {return xffffffff().sll32(22);} + static GSVector4i xffe00000() {return xffffffff().sll32(21);} + static GSVector4i xfff00000() {return xffffffff().sll32(20);} + static GSVector4i xfff80000() {return xffffffff().sll32(19);} + static GSVector4i xfffc0000() {return xffffffff().sll32(18);} + static GSVector4i xfffe0000() {return xffffffff().sll32(17);} + static GSVector4i xffff0000() {return xffffffff().sll32(16);} + static GSVector4i xffff8000() {return xffffffff().sll32(15);} + static GSVector4i xffffc000() {return xffffffff().sll32(14);} + static GSVector4i xffffe000() {return xffffffff().sll32(13);} + static GSVector4i xfffff000() {return xffffffff().sll32(12);} + static GSVector4i xfffff800() {return xffffffff().sll32(11);} + static GSVector4i xfffffc00() {return xffffffff().sll32(10);} + static GSVector4i xfffffe00() {return xffffffff().sll32( 9);} + static GSVector4i xffffff00() {return xffffffff().sll32( 8);} + static GSVector4i xffffff80() {return xffffffff().sll32( 7);} + static GSVector4i xffffffc0() {return xffffffff().sll32( 6);} + static GSVector4i xffffffe0() {return xffffffff().sll32( 5);} + static GSVector4i xfffffff0() {return xffffffff().sll32( 4);} + static GSVector4i xfffffff8() {return xffffffff().sll32( 3);} + static GSVector4i xfffffffc() {return xffffffff().sll32( 2);} + static GSVector4i xfffffffe() {return xffffffff().sll32( 1);} + + static GSVector4i x0001() {return xffffffff().srl16(15);} + static GSVector4i x0003() {return xffffffff().srl16(14);} + static GSVector4i x0007() {return xffffffff().srl16(13);} + static GSVector4i x000f() {return xffffffff().srl16(12);} + static GSVector4i x001f() {return xffffffff().srl16(11);} + static GSVector4i x003f() {return xffffffff().srl16(10);} + static GSVector4i x007f() {return xffffffff().srl16( 9);} + static GSVector4i x00ff() {return xffffffff().srl16( 8);} + static GSVector4i x01ff() {return xffffffff().srl16( 7);} + static GSVector4i x03ff() {return xffffffff().srl16( 6);} + static GSVector4i x07ff() {return xffffffff().srl16( 5);} + static GSVector4i x0fff() {return xffffffff().srl16( 4);} + static GSVector4i x1fff() {return xffffffff().srl16( 3);} + static GSVector4i x3fff() {return xffffffff().srl16( 2);} + static GSVector4i x7fff() {return xffffffff().srl16( 1);} + + static GSVector4i x8000() {return xffffffff().sll16(15);} + static GSVector4i xc000() {return xffffffff().sll16(14);} + static GSVector4i xe000() {return xffffffff().sll16(13);} + static GSVector4i xf000() {return xffffffff().sll16(12);} + static GSVector4i xf800() {return xffffffff().sll16(11);} + static GSVector4i xfc00() {return xffffffff().sll16(10);} + static GSVector4i xfe00() {return xffffffff().sll16( 9);} + static GSVector4i xff00() {return xffffffff().sll16( 8);} + static GSVector4i xff80() {return xffffffff().sll16( 7);} + static GSVector4i xffc0() {return xffffffff().sll16( 6);} + static GSVector4i xffe0() {return xffffffff().sll16( 5);} + static GSVector4i xfff0() {return xffffffff().sll16( 4);} + static GSVector4i xfff8() {return xffffffff().sll16( 3);} + static GSVector4i xfffc() {return xffffffff().sll16( 2);} + static GSVector4i xfffe() {return xffffffff().sll16( 1);} + + static GSVector4i xffffffff(const GSVector4i& v) {return v == v;} + + static GSVector4i x00000001(const GSVector4i& v) {return xffffffff(v).srl32(31);} + static GSVector4i x00000003(const GSVector4i& v) {return xffffffff(v).srl32(30);} + static GSVector4i x00000007(const GSVector4i& v) {return xffffffff(v).srl32(29);} + static GSVector4i x0000000f(const GSVector4i& v) {return xffffffff(v).srl32(28);} + static GSVector4i x0000001f(const GSVector4i& v) {return xffffffff(v).srl32(27);} + static GSVector4i x0000003f(const GSVector4i& v) {return xffffffff(v).srl32(26);} + static GSVector4i x0000007f(const GSVector4i& v) {return xffffffff(v).srl32(25);} + static GSVector4i x000000ff(const GSVector4i& v) {return xffffffff(v).srl32(24);} + static GSVector4i x000001ff(const GSVector4i& v) {return xffffffff(v).srl32(23);} + static GSVector4i x000003ff(const GSVector4i& v) {return xffffffff(v).srl32(22);} + static GSVector4i x000007ff(const GSVector4i& v) {return xffffffff(v).srl32(21);} + static GSVector4i x00000fff(const GSVector4i& v) {return xffffffff(v).srl32(20);} + static GSVector4i x00001fff(const GSVector4i& v) {return xffffffff(v).srl32(19);} + static GSVector4i x00003fff(const GSVector4i& v) {return xffffffff(v).srl32(18);} + static GSVector4i x00007fff(const GSVector4i& v) {return xffffffff(v).srl32(17);} + static GSVector4i x0000ffff(const GSVector4i& v) {return xffffffff(v).srl32(16);} + static GSVector4i x0001ffff(const GSVector4i& v) {return xffffffff(v).srl32(15);} + static GSVector4i x0003ffff(const GSVector4i& v) {return xffffffff(v).srl32(14);} + static GSVector4i x0007ffff(const GSVector4i& v) {return xffffffff(v).srl32(13);} + static GSVector4i x000fffff(const GSVector4i& v) {return xffffffff(v).srl32(12);} + static GSVector4i x001fffff(const GSVector4i& v) {return xffffffff(v).srl32(11);} + static GSVector4i x003fffff(const GSVector4i& v) {return xffffffff(v).srl32(10);} + static GSVector4i x007fffff(const GSVector4i& v) {return xffffffff(v).srl32( 9);} + static GSVector4i x00ffffff(const GSVector4i& v) {return xffffffff(v).srl32( 8);} + static GSVector4i x01ffffff(const GSVector4i& v) {return xffffffff(v).srl32( 7);} + static GSVector4i x03ffffff(const GSVector4i& v) {return xffffffff(v).srl32( 6);} + static GSVector4i x07ffffff(const GSVector4i& v) {return xffffffff(v).srl32( 5);} + static GSVector4i x0fffffff(const GSVector4i& v) {return xffffffff(v).srl32( 4);} + static GSVector4i x1fffffff(const GSVector4i& v) {return xffffffff(v).srl32( 3);} + static GSVector4i x3fffffff(const GSVector4i& v) {return xffffffff(v).srl32( 2);} + static GSVector4i x7fffffff(const GSVector4i& v) {return xffffffff(v).srl32( 1);} + + static GSVector4i x80000000(const GSVector4i& v) {return xffffffff(v).sll32(31);} + static GSVector4i xc0000000(const GSVector4i& v) {return xffffffff(v).sll32(30);} + static GSVector4i xe0000000(const GSVector4i& v) {return xffffffff(v).sll32(29);} + static GSVector4i xf0000000(const GSVector4i& v) {return xffffffff(v).sll32(28);} + static GSVector4i xf8000000(const GSVector4i& v) {return xffffffff(v).sll32(27);} + static GSVector4i xfc000000(const GSVector4i& v) {return xffffffff(v).sll32(26);} + static GSVector4i xfe000000(const GSVector4i& v) {return xffffffff(v).sll32(25);} + static GSVector4i xff000000(const GSVector4i& v) {return xffffffff(v).sll32(24);} + static GSVector4i xff800000(const GSVector4i& v) {return xffffffff(v).sll32(23);} + static GSVector4i xffc00000(const GSVector4i& v) {return xffffffff(v).sll32(22);} + static GSVector4i xffe00000(const GSVector4i& v) {return xffffffff(v).sll32(21);} + static GSVector4i xfff00000(const GSVector4i& v) {return xffffffff(v).sll32(20);} + static GSVector4i xfff80000(const GSVector4i& v) {return xffffffff(v).sll32(19);} + static GSVector4i xfffc0000(const GSVector4i& v) {return xffffffff(v).sll32(18);} + static GSVector4i xfffe0000(const GSVector4i& v) {return xffffffff(v).sll32(17);} + static GSVector4i xffff0000(const GSVector4i& v) {return xffffffff(v).sll32(16);} + static GSVector4i xffff8000(const GSVector4i& v) {return xffffffff(v).sll32(15);} + static GSVector4i xffffc000(const GSVector4i& v) {return xffffffff(v).sll32(14);} + static GSVector4i xffffe000(const GSVector4i& v) {return xffffffff(v).sll32(13);} + static GSVector4i xfffff000(const GSVector4i& v) {return xffffffff(v).sll32(12);} + static GSVector4i xfffff800(const GSVector4i& v) {return xffffffff(v).sll32(11);} + static GSVector4i xfffffc00(const GSVector4i& v) {return xffffffff(v).sll32(10);} + static GSVector4i xfffffe00(const GSVector4i& v) {return xffffffff(v).sll32( 9);} + static GSVector4i xffffff00(const GSVector4i& v) {return xffffffff(v).sll32( 8);} + static GSVector4i xffffff80(const GSVector4i& v) {return xffffffff(v).sll32( 7);} + static GSVector4i xffffffc0(const GSVector4i& v) {return xffffffff(v).sll32( 6);} + static GSVector4i xffffffe0(const GSVector4i& v) {return xffffffff(v).sll32( 5);} + static GSVector4i xfffffff0(const GSVector4i& v) {return xffffffff(v).sll32( 4);} + static GSVector4i xfffffff8(const GSVector4i& v) {return xffffffff(v).sll32( 3);} + static GSVector4i xfffffffc(const GSVector4i& v) {return xffffffff(v).sll32( 2);} + static GSVector4i xfffffffe(const GSVector4i& v) {return xffffffff(v).sll32( 1);} + + static GSVector4i x0001(const GSVector4i& v) {return xffffffff(v).srl16(15);} + static GSVector4i x0003(const GSVector4i& v) {return xffffffff(v).srl16(14);} + static GSVector4i x0007(const GSVector4i& v) {return xffffffff(v).srl16(13);} + static GSVector4i x000f(const GSVector4i& v) {return xffffffff(v).srl16(12);} + static GSVector4i x001f(const GSVector4i& v) {return xffffffff(v).srl16(11);} + static GSVector4i x003f(const GSVector4i& v) {return xffffffff(v).srl16(10);} + static GSVector4i x007f(const GSVector4i& v) {return xffffffff(v).srl16( 9);} + static GSVector4i x00ff(const GSVector4i& v) {return xffffffff(v).srl16( 8);} + static GSVector4i x01ff(const GSVector4i& v) {return xffffffff(v).srl16( 7);} + static GSVector4i x03ff(const GSVector4i& v) {return xffffffff(v).srl16( 6);} + static GSVector4i x07ff(const GSVector4i& v) {return xffffffff(v).srl16( 5);} + static GSVector4i x0fff(const GSVector4i& v) {return xffffffff(v).srl16( 4);} + static GSVector4i x1fff(const GSVector4i& v) {return xffffffff(v).srl16( 3);} + static GSVector4i x3fff(const GSVector4i& v) {return xffffffff(v).srl16( 2);} + static GSVector4i x7fff(const GSVector4i& v) {return xffffffff(v).srl16( 1);} + + static GSVector4i x8000(const GSVector4i& v) {return xffffffff(v).sll16(15);} + static GSVector4i xc000(const GSVector4i& v) {return xffffffff(v).sll16(14);} + static GSVector4i xe000(const GSVector4i& v) {return xffffffff(v).sll16(13);} + static GSVector4i xf000(const GSVector4i& v) {return xffffffff(v).sll16(12);} + static GSVector4i xf800(const GSVector4i& v) {return xffffffff(v).sll16(11);} + static GSVector4i xfc00(const GSVector4i& v) {return xffffffff(v).sll16(10);} + static GSVector4i xfe00(const GSVector4i& v) {return xffffffff(v).sll16( 9);} + static GSVector4i xff00(const GSVector4i& v) {return xffffffff(v).sll16( 8);} + static GSVector4i xff80(const GSVector4i& v) {return xffffffff(v).sll16( 7);} + static GSVector4i xffc0(const GSVector4i& v) {return xffffffff(v).sll16( 6);} + static GSVector4i xffe0(const GSVector4i& v) {return xffffffff(v).sll16( 5);} + static GSVector4i xfff0(const GSVector4i& v) {return xffffffff(v).sll16( 4);} + static GSVector4i xfff8(const GSVector4i& v) {return xffffffff(v).sll16( 3);} + static GSVector4i xfffc(const GSVector4i& v) {return xffffffff(v).sll16( 2);} + static GSVector4i xfffe(const GSVector4i& v) {return xffffffff(v).sll16( 1);} +}; + +__declspec(align(16)) class GSVector4 +{ +public: + union + { + struct {float x, y, z, w;}; + struct {float r, g, b, a;}; + float v[4]; + float f32[4]; + unsigned __int64 u64[2]; + __int8 i8[16]; + __int16 i16[8]; + __int32 i32[4]; + __int64 i64[2]; + unsigned __int8 u8[16]; + unsigned __int16 u16[8]; + unsigned __int32 u32[4]; + __m128 m; + }; + + static const GSVector4 m_ps0123; + static const GSVector4 m_ps4567; + + GSVector4() + { + } + + GSVector4(float x, float y, float z, float w) + { + m = _mm_set_ps(w, z, y, x); + } + + GSVector4(float x, float y) + { + m = _mm_unpacklo_ps(_mm_load_ss(&x), _mm_load_ss(&y)); + } + + GSVector4(int x, int y, int z, int w) + { + m = _mm_cvtepi32_ps(_mm_set_epi32(w, z, y, x)); + } + + GSVector4(int x, int y) + { + m = _mm_cvtepi32_ps(_mm_unpacklo_epi32(_mm_cvtsi32_si128(x), _mm_cvtsi32_si128(y))); + } + + GSVector4(const GSVector4& v) + { + m = v.m; + } + + explicit GSVector4(const GSVector2& v) + { + m = _mm_castsi128_ps(_mm_loadl_epi64((__m128i*)&v)); + } + + explicit GSVector4(float f) + { + m = _mm_set1_ps(f); + } + + explicit GSVector4(__m128 m) + { + this->m = m; + } + + explicit GSVector4(CRect r) + { + m = _mm_set_ps((float)r.bottom, (float)r.right, (float)r.top, (float)r.left); + } + + explicit GSVector4(DWORD dw) + { + *this = GSVector4(GSVector4i::load((int)dw).u8to32()); + } + + explicit GSVector4(const GSVector4i& v) + { + *this = v; + } + + void operator = (const GSVector4& v) + { + m = v.m; + } + + void operator = (const GSVector4i& v); + + void operator = (float f) + { + m = _mm_set1_ps(f); + } + + void operator = (__m128 m) + { + this->m = m; + } + + void operator = (DWORD dw) + { + *this = GSVector4(GSVector4i::load((int)dw).u8to32()); + } + + void operator = (CRect r) + { + *this = GSVector4(GSVector4i(r.left, r.top, r.right, r.bottom)); + } + + operator __m128() const + { + return m; + } + + UINT32 rgba32() const + { + return GSVector4i(*this).rgba32(); + } + + static GSVector4 cast(const GSVector4i& v); + + GSVector4 abs() const + { + return GSVector4(_mm_abs_ps(m)); + } + + GSVector4 neg() const + { + return GSVector4(_mm_neg_ps(m)); + } + + GSVector4 rcp() const + { + return GSVector4(_mm_rcp_ps(m)); + } + + GSVector4 rcpnr() const + { + return GSVector4(_mm_rcpnr_ps(m)); + } + + GSVector4 floor() const + { + return GSVector4(_mm_floor_ps(m)); + } + + GSVector4 ceil() const + { + return GSVector4(_mm_ceil_ps(m)); + } + + GSVector4 mod2x(const GSVector4& f, const int scale = 256) const + { + return *this * (f * (2.0f / scale)); + } + + GSVector4 mod2x(float f, const int scale = 256) const + { + return mod2x(GSVector4(f), scale); + } + + GSVector4 madd(const GSVector4& a, const GSVector4& b) const + { + return *this * a + b; // TODO: _mm_fmadd_ps + } + + GSVector4 msub(const GSVector4& a, const GSVector4& b) const + { + return *this * a + b; // TODO: _mm_fmsub_ps + } + + GSVector4 nmadd(const GSVector4& a, const GSVector4& b) const + { + return b - *this * a; // TODO: _mm_fnmadd_ps + } + + GSVector4 nmsub(const GSVector4& a, const GSVector4& b) const + { + return -b - *this * a; // TODO: _mm_fmnsub_ps + } + + GSVector4 lerp(const GSVector4& v, const GSVector4& f) const + { + return *this + (v - *this) * f; + } + + GSVector4 lerp(const GSVector4& v, float f) const + { + return lerp(v, GSVector4(f)); + } + + GSVector4 hadd() const + { + #if _M_SSE >= 0x300 + return GSVector4(_mm_hadd_ps(m, m)); + #else + return xzxz() + ywyw(); + #endif + } + + GSVector4 hadd(const GSVector4& v) const + { + #if _M_SSE >= 0x300 + return GSVector4(_mm_hadd_ps(m, v.m)); + #else + return xzxz(v) + ywyw(v); + #endif + } + + #if _M_SSE >= 0x401 + template GSVector4 dp(const GSVector4& v) const + { + return GSVector4(_mm_dp_ps(m, v.m, i)); + } + #endif + + GSVector4 sat(const GSVector4& a, const GSVector4& b) const + { + return GSVector4(_mm_min_ps(_mm_max_ps(m, a), b)); + } + + GSVector4 sat(const GSVector4& a) const + { + return GSVector4(_mm_min_ps(_mm_max_ps(m, a.xyxy()), a.zwzw())); + } + + GSVector4 sat(const float scale = 255) const + { + return sat(zero(), GSVector4(scale)); + } + + GSVector4 clamp(const float scale = 255) const + { + return minv(GSVector4(scale)); + } + + GSVector4 minv(const GSVector4& a) const + { + return GSVector4(_mm_min_ps(m, a)); + } + + GSVector4 maxv(const GSVector4& a) const + { + return GSVector4(_mm_max_ps(m, a)); + } + + GSVector4 blend8(const GSVector4& a, const GSVector4& mask) const + { + return GSVector4(_mm_blendv_ps(m, a, mask)); + } + + GSVector4 upl(const GSVector4& a) const + { + return GSVector4(_mm_unpacklo_ps(m, a)); + } + + GSVector4 uph(const GSVector4& a) const + { + return GSVector4(_mm_unpackhi_ps(m, a)); + } + + GSVector4 l2h(const GSVector4& a) const + { + return GSVector4(_mm_movelh_ps(m, a)); + } + + GSVector4 h2l(const GSVector4& a) const + { + return GSVector4(_mm_movehl_ps(m, a)); + } + + GSVector4 andnot(const GSVector4& v) const + { + return GSVector4(_mm_andnot_ps(v.m, m)); + } + + int mask() const + { + return _mm_movemask_ps(m); + } + + bool alltrue() const + { + return _mm_movemask_ps(m) == 0xf; + } + + bool allfalse() const + { + return _mm_movemask_ps(m) == 0; + } + + // TODO: insert + + template int extract() const + { + #if _M_SSE >= 0x401 + return _mm_extract_ps(m, i); + #else + return i32[i]; + #endif + } + + static GSVector4 zero() + { + return GSVector4(_mm_setzero_ps()); + } + + static GSVector4 xffffffff() + { + return zero() == zero(); + } + + static GSVector4 ps0123() + { + return GSVector4(m_ps0123); + } + + static GSVector4 ps4567() + { + return GSVector4(m_ps4567); + } + + static GSVector4 loadl(const void* p) + { + return GSVector4(_mm_castpd_ps(_mm_load_sd((double*)p))); + } + + static GSVector4 load(float f) + { + return GSVector4(_mm_load_ss(&f)); + } + + template static GSVector4 load(const void* p) + { + return GSVector4i(aligned ? _mm_load_ps((__m128*)p) : _mm_loadu_ps((__m128*)p)); + } + + static void storel(void* p, const GSVector4& v) + { + _mm_store_sd((double*)p, _mm_castps_pd(v.m)); + } + + template static void store(void* p, const GSVector4& v) + { + if(aligned) _mm_store_ps((__m128*)p, v.m); + else _mm_storeu_ps((__m128*)p, v.m); + } + + __forceinline static void expand(const GSVector4i& v, GSVector4& a, GSVector4& b, GSVector4& c, GSVector4& d) + { + GSVector4i mask = GSVector4i::x000000ff(); + + a = v & mask; + b = (v >> 8) & mask; + c = (v >> 16) & mask; + d = (v >> 24); + } + + __forceinline static void transpose(GSVector4& a, GSVector4& b, GSVector4& c, GSVector4& d) + { + GSVector4 v0 = a.xyxy(b); + GSVector4 v1 = c.xyxy(d); + + GSVector4 e = v0.xzxz(v1); + GSVector4 f = v0.ywyw(v1); + + GSVector4 v2 = a.zwzw(b); + GSVector4 v3 = c.zwzw(d); + + GSVector4 g = v2.xzxz(v3); + GSVector4 h = v2.ywyw(v3); + + a = e; + b = f; + c = g; + d = h; +/* + GSVector4 v0 = a.xyxy(b); + GSVector4 v1 = c.xyxy(d); + GSVector4 v2 = a.zwzw(b); + GSVector4 v3 = c.zwzw(d); + + a = v0.xzxz(v1); + b = v0.ywyw(v1); + c = v2.xzxz(v3); + d = v2.ywyw(v3); +*/ +/* + GSVector4 v0 = a.upl(b); + GSVector4 v1 = a.uph(b); + GSVector4 v2 = c.upl(d); + GSVector4 v3 = c.uph(d); + + a = v0.l2h(v2); + b = v2.h2l(v0); + c = v1.l2h(v3); + d = v3.h2l(v1); +*/ } + + GSVector4 operator - () const + { + return neg(); + } + + void operator += (const GSVector4& v) + { + m = _mm_add_ps(m, v); + } + + void operator -= (const GSVector4& v) + { + m = _mm_sub_ps(m, v); + } + + void operator *= (const GSVector4& v) + { + m = _mm_mul_ps(m, v); + } + + void operator /= (const GSVector4& v) + { + m = _mm_div_ps(m, v); + } + + void operator += (float f) + { + *this += GSVector4(f); + } + + void operator -= (float f) + { + *this -= GSVector4(f); + } + + void operator *= (float f) + { + *this *= GSVector4(f); + } + + void operator /= (float f) + { + *this /= GSVector4(f); + } + + void operator &= (const GSVector4& v) + { + m = _mm_and_ps(m, v); + } + + void operator |= (const GSVector4& v) + { + m = _mm_or_ps(m, v); + } + + void operator ^= (const GSVector4& v) + { + m = _mm_xor_ps(m, v); + } + + friend GSVector4 operator + (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_add_ps(v1, v2)); + } + + friend GSVector4 operator - (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_sub_ps(v1, v2)); + } + + friend GSVector4 operator * (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_mul_ps(v1, v2)); + } + + friend GSVector4 operator / (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_div_ps(v1, v2)); + } + + friend GSVector4 operator + (const GSVector4& v, float f) + { + return v + GSVector4(f); + } + + friend GSVector4 operator - (const GSVector4& v, float f) + { + return v - GSVector4(f); + } + + friend GSVector4 operator * (const GSVector4& v, float f) + { + return v * GSVector4(f); + } + + friend GSVector4 operator / (const GSVector4& v, float f) + { + return v / GSVector4(f); + } + + friend GSVector4 operator & (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_and_ps(v1, v2)); + } + + friend GSVector4 operator | (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_or_ps(v1, v2)); + } + + friend GSVector4 operator ^ (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_xor_ps(v1, v2)); + } + + friend GSVector4 operator == (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmpeq_ps(v1, v2)); + } + + friend GSVector4 operator != (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmpneq_ps(v1, v2)); + } + + friend GSVector4 operator > (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmpgt_ps(v1, v2)); + } + + friend GSVector4 operator < (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmplt_ps(v1, v2)); + } + + friend GSVector4 operator >= (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmpge_ps(v1, v2)); + } + + friend GSVector4 operator <= (const GSVector4& v1, const GSVector4& v2) + { + return GSVector4(_mm_cmple_ps(v1, v2)); + } + + template GSVector4 shuffle() const + { + return GSVector4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(i, i, i, i))); + } + + #define VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, ws, wn) \ + GSVector4 xs##ys##zs##ws() const {return GSVector4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(wn, zn, yn, xn)));} \ + GSVector4 xs##ys##zs##ws(const GSVector4& v) const {return GSVector4(_mm_shuffle_ps(m, v.m, _MM_SHUFFLE(wn, zn, yn, xn)));} \ + + #define VECTOR4_SHUFFLE_3(xs, xn, ys, yn, zs, zn) \ + VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, x, 0) \ + VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, y, 1) \ + VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, z, 2) \ + VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, w, 3) \ + + #define VECTOR4_SHUFFLE_2(xs, xn, ys, yn) \ + VECTOR4_SHUFFLE_3(xs, xn, ys, yn, x, 0) \ + VECTOR4_SHUFFLE_3(xs, xn, ys, yn, y, 1) \ + VECTOR4_SHUFFLE_3(xs, xn, ys, yn, z, 2) \ + VECTOR4_SHUFFLE_3(xs, xn, ys, yn, w, 3) \ + + #define VECTOR4_SHUFFLE_1(xs, xn) \ + GSVector4 xs##4##() const {return GSVector4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(xn, xn, xn, xn)));} \ + GSVector4 xs##4##(const GSVector4& v) const {return GSVector4(_mm_shuffle_ps(m, v.m, _MM_SHUFFLE(xn, xn, xn, xn)));} \ + VECTOR4_SHUFFLE_2(xs, xn, x, 0) \ + VECTOR4_SHUFFLE_2(xs, xn, y, 1) \ + VECTOR4_SHUFFLE_2(xs, xn, z, 2) \ + VECTOR4_SHUFFLE_2(xs, xn, w, 3) \ + + VECTOR4_SHUFFLE_1(x, 0) + VECTOR4_SHUFFLE_1(y, 1) + VECTOR4_SHUFFLE_1(z, 2) + VECTOR4_SHUFFLE_1(w, 3) +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GSVertex.h b/plugins/GSdx/GSVertex.h new file mode 100644 index 0000000000..c1b52ac154 --- /dev/null +++ b/plugins/GSdx/GSVertex.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GS.h" +#include "GSVector.h" +#include "GSVertexHW.h" +#include "GSVertexSW.h" + +#pragma pack(push, 1) + +__declspec(align(16)) struct GSVertex +{ + union + { + struct + { + GIFRegST ST; + GIFRegXYZ XYZ; + GIFRegRGBAQ RGBAQ; + GIFRegFOG FOG; + }; + + struct {__m128i m128i[2];}; + struct {__m128 m128[2];}; + }; + + GIFRegUV UV; + + GSVertex() {memset(this, 0, sizeof(*this));} + + GSVector4 GetUV() const {return GSVector4(GSVector4i::load(UV.ai32[0]).upl16());} +}; + +struct GSVertexOld +{ + GIFRegRGBAQ RGBAQ; + GIFRegST ST; + GIFRegUV UV; + GIFRegXYZ XYZ; + GIFRegFOG FOG; + + GSVertexOld() {memset(this, 0, sizeof(*this));} +}; + +struct GSVertexP +{ + GSVector4 p; +}; + +struct GSVertexPT1 +{ + GSVector4 p; + GSVector2 t; +}; + +struct GSVertexPT2 +{ + GSVector4 p; + GSVector2 t[2]; +}; + +struct GSVertexNull +{ + GSVector4 p; +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GSVertexHW.h b/plugins/GSdx/GSVertexHW.h new file mode 100644 index 0000000000..eaa209f81b --- /dev/null +++ b/plugins/GSdx/GSVertexHW.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSVector.h" + +#pragma pack(push, 1) + +__declspec(align(16)) union GSVertexHW9 +{ + struct + { + GSVector2 t; + union {struct {BYTE r, g, b, a;}; DWORD c0;}; + union {struct {BYTE ta0, ta1, res, f;}; DWORD c1;}; + GSVector4 p; + }; + + struct {__m128i m128i[2];}; + struct {__m128 m128[2];}; + +#if _M_SSE >= 0x200 + GSVertexHW9& operator = (GSVertexHW9& v) {m128i[0] = v.m128i[0]; m128i[1] = v.m128i[1]; return *this;} +#endif + + float GetQ() {return p.w;} +}; + +__declspec(align(16)) union GSVertexHW10 +{ + struct + { + union + { + struct {float x, y;} t; + GIFRegST ST; + }; + + union + { + struct {union {struct {WORD x, y;}; DWORD xy;}; DWORD z;} p; + GIFRegXYZ XYZ; + }; + + union + { + union {struct {BYTE r, g, b, a; float q;}; DWORD c0;}; + GIFRegRGBAQ RGBAQ; + }; + + union + { + struct {DWORD _pad[1]; union {struct {BYTE ta0, ta1, res, f;}; DWORD c1;};}; + GIFRegFOG FOG; + }; + }; + + struct {__m128i m128i[2];}; + struct {__m128 m128[2];}; + +#if _M_SSE >= 0x200 + GSVertexHW10& operator = (GSVertexHW10& v) {m128i[0] = v.m128i[0]; m128i[1] = v.m128i[1]; return *this;} +#endif + + float GetQ() {return q;} +}; + +#pragma pack(pop) diff --git a/plugins/GSdx/GSVertexList.cpp b/plugins/GSdx/GSVertexList.cpp new file mode 100644 index 0000000000..21377b3758 --- /dev/null +++ b/plugins/GSdx/GSVertexList.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSVertexList.h" \ No newline at end of file diff --git a/plugins/GSdx/GSVertexList.h b/plugins/GSdx/GSVertexList.h new file mode 100644 index 0000000000..95111cd6de --- /dev/null +++ b/plugins/GSdx/GSVertexList.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +template class GSVertexList +{ + void* m_base; + Vertex* m_v[3]; + DWORD m_count; + +public: + GSVertexList() + : m_count(0) + { + m_base = _aligned_malloc(sizeof(Vertex) * countof(m_v), 16); + + for(int i = 0; i < countof(m_v); i++) + { + m_v[i] = &((Vertex*)m_base)[i]; + } + } + + virtual ~GSVertexList() + { + _aligned_free(m_base); + } + + void RemoveAll() + { + m_count = 0; + } + + __forceinline Vertex& AddTail() + { + ASSERT(m_count < 3); + + return *m_v[m_count++]; + } + + __forceinline void RemoveAt(int pos, int keep) + { + if(keep == 1) + { + Vertex* tmp = m_v[pos + 0]; + m_v[pos + 0] = m_v[pos + 1]; + m_v[pos + 1] = tmp; + } + else if(keep == 2) + { + Vertex* tmp = m_v[pos + 0]; + m_v[pos + 0] = m_v[pos + 1]; + m_v[pos + 1] = m_v[pos + 2]; + m_v[pos + 2] = tmp; + } + + m_count = pos + keep; + } + + __forceinline void GetAt(int i, Vertex& v) + { + v = *m_v[i]; + } + + DWORD GetCount() + { + return m_count; + } +}; diff --git a/plugins/GSdx/GSVertexSW.h b/plugins/GSdx/GSVertexSW.h new file mode 100644 index 0000000000..f29481b51c --- /dev/null +++ b/plugins/GSdx/GSVertexSW.h @@ -0,0 +1,254 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#include "GSVector.h" + +__declspec(align(16)) union GSVertexSW +{ + struct {GSVector4 c, p, t;}; + struct {GSVector4 v[3];}; + struct {float f[12];}; + + GSVertexSW() {} + GSVertexSW(const GSVertexSW& v) {*this = v;} + + void operator = (const GSVertexSW& v) {c = v.c; p = v.p; t = v.t;} + void operator += (const GSVertexSW& v) {c += v.c; p += v.p; t += v.t;} + + friend GSVertexSW operator + (const GSVertexSW& v1, const GSVertexSW& v2); + friend GSVertexSW operator - (const GSVertexSW& v1, const GSVertexSW& v2); + friend GSVertexSW operator * (const GSVertexSW& v, const GSVector4& vv); + friend GSVertexSW operator / (const GSVertexSW& v, const GSVector4& vv); + friend GSVertexSW operator * (const GSVertexSW& v, float f); + friend GSVertexSW operator / (const GSVertexSW& v, float f); + + static bool IsQuad(const GSVertexSW* v, int& tl, int& br) + { + GSVector4 v0 = v[0].p.xyxy(v[0].t); + GSVector4 v1 = v[1].p.xyxy(v[1].t); + GSVector4 v2 = v[2].p.xyxy(v[2].t); + + GSVector4 v01 = v0 == v1; + GSVector4 v12 = v1 == v2; + GSVector4 v02 = v0 == v2; + + GSVector4 vtl, vbr; + + GSVector4 test; + + int i; + + if(v12.allfalse()) + { + test = (v01 ^ v02) & (v01 ^ v02.zwxy()); + vtl = v0; + vbr = v1 + (v2 - v0); + i = 0; + } + else if(v02.allfalse()) + { + test = (v01 ^ v12) & (v01 ^ v12.zwxy()); + vtl = v1; + vbr = v0 + (v2 - v1); + i = 1; + } + else if(v01.allfalse()) + { + test = (v02 ^ v12) & (v02 ^ v12.zwxy()); + vtl = v2; + vbr = v0 + (v1 - v2); + i = 2; + } + else + { + return false; + } + + if(!test.alltrue()) + { + return false; + } + + tl = i; + + GSVector4 v3 = v[3].p.xyxy(v[3].t); + GSVector4 v4 = v[4].p.xyxy(v[4].t); + GSVector4 v5 = v[5].p.xyxy(v[5].t); + + GSVector4 v34 = v3 == v4; + GSVector4 v45 = v4 == v5; + GSVector4 v35 = v3 == v5; + + if(v34.allfalse()) + { + test = (v35 ^ v45) & (v35 ^ v45.zwxy()) & (vtl == v3 + (v4 - v5)) & (vbr == v5); + i = 5; + } + else if(v35.allfalse()) + { + test = (v34 ^ v45) & (v34 ^ v45.zwxy()) & (vtl == v3 + (v5 - v4)) & (vbr == v4); + i = 4; + } + else if(v45.allfalse()) + { + test = (v34 ^ v35) & (v34 ^ v35.zwxy()) & (vtl == v5 + (v4 - v3)) & (vbr == v3); + i = 3; + } + else + { + return false; + } + + if(!test.alltrue()) + { + return false; + } + + br = i; + + v0 = v[0].p.zwzw(v[0].t); + v1 = v[1].p.zwzw(v[1].t); + v2 = v[2].p.zwzw(v[2].t); + v3 = v[3].p.zwzw(v[3].t); + v4 = v[4].p.zwzw(v[4].t); + v5 = v[5].p.zwzw(v[5].t); + + test = ((v0 == v1) & (v0 == v2)) & ((v0 == v3) & (v0 == v4)) & (v0 == v5); + + if(!test.alltrue()) + { + return false; + } + + v0 = v[0].c; + v1 = v[1].c; + v2 = v[2].c; + v3 = v[3].c; + v4 = v[4].c; + v5 = v[5].c; + + test = ((v0 == v1) & (v0 == v2)) & ((v0 == v3) & (v0 == v4)) & (v0 == v5); + + if(!test.alltrue()) + { + return false; + } + + return true; + } +}; + +__forceinline GSVertexSW operator + (const GSVertexSW& v1, const GSVertexSW& v2) +{ + GSVertexSW v0; + v0.c = v1.c + v2.c; + v0.p = v1.p + v2.p; + v0.t = v1.t + v2.t; + return v0; +} + +__forceinline GSVertexSW operator - (const GSVertexSW& v1, const GSVertexSW& v2) +{ + GSVertexSW v0; + v0.c = v1.c - v2.c; + v0.p = v1.p - v2.p; + v0.t = v1.t - v2.t; + return v0; +} + +__forceinline GSVertexSW operator * (const GSVertexSW& v, const GSVector4& vv) +{ + GSVertexSW v0; + v0.c = v.c * vv; + v0.p = v.p * vv; + v0.t = v.t * vv; + return v0; +} + +__forceinline GSVertexSW operator / (const GSVertexSW& v, const GSVector4& vv) +{ + GSVertexSW v0; + v0.c = v.c / vv; + v0.p = v.p / vv; + v0.t = v.t / vv; + return v0; +} + +__forceinline GSVertexSW operator * (const GSVertexSW& v, float f) +{ + GSVertexSW v0; + GSVector4 vf(f); + v0.c = v.c * vf; + v0.p = v.p * vf; + v0.t = v.t * vf; + return v0; +} + +__forceinline GSVertexSW operator / (const GSVertexSW& v, float f) +{ + GSVertexSW v0; + GSVector4 vf(f); + v0.c = v.c / vf; + v0.p = v.p / vf; + v0.t = v.t / vf; + return v0; +} + +__declspec(align(16)) struct GSVertexTrace +{ + GSVertexSW m_min, m_max; + + union + { + DWORD value; + struct {DWORD x:1, y:1, z:1, f:1, s:1, t:1, q:1, _pad:1, r:1, g:1, b:1, a:1;}; + struct {DWORD xyzf:4, stq:4, rgba:4;}; + } m_eq; + + void Update(const GSVertexSW* v, int count) + { + GSVertexSW min, max; + + min.p = v[0].p; + max.p = v[0].p; + min.t = v[0].t; + max.t = v[0].t; + min.c = v[0].c; + max.c = v[0].c; + + for(int i = 1; i < count; i++) + { + min.c = min.c.minv(v[i].c); + max.c = max.c.maxv(v[i].c); + min.p = min.p.minv(v[i].p); + max.p = max.p.maxv(v[i].p); + min.t = min.t.minv(v[i].t); + max.t = max.t.maxv(v[i].t); + } + + m_min = min; + m_max = max; + + m_eq.value = (min.p == max.p).mask() | ((min.t == max.t).mask() << 4) | ((min.c == max.c).mask() << 8); + } +}; diff --git a/plugins/GSdx/GSWnd.cpp b/plugins/GSdx/GSWnd.cpp new file mode 100644 index 0000000000..6c716fbd8f --- /dev/null +++ b/plugins/GSdx/GSWnd.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "StdAfx.h" +#include "GSWnd.h" + +BEGIN_MESSAGE_MAP(GSWnd, CWnd) + ON_WM_CLOSE() +END_MESSAGE_MAP() + +GSWnd::GSWnd() +{ +} + +GSWnd::~GSWnd() +{ + DestroyWindow(); +} + +bool GSWnd::Create(LPCTSTR title) +{ + CRect r; + + GetDesktopWindow()->GetWindowRect(r); + + CSize s(r.Width() / 3, r.Width() / 4); + + if(!GetSystemMetrics(SM_REMOTESESSION)) + { + s.cx *= 2; + s.cy *= 2; + } + + r = CRect(r.CenterPoint() - CSize(s.cx / 2, s.cy / 2), s); + + LPCTSTR wc = AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW|CS_DBLCLKS, AfxGetApp()->LoadStandardCursor(IDC_ARROW), 0, 0); + + return !!CreateEx(0, wc, title, WS_OVERLAPPEDWINDOW, r, NULL, 0); +} + +void GSWnd::Show() +{ + SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); + SetForegroundWindow(); + ShowWindow(SW_SHOWNORMAL); +} + +void GSWnd::Hide() +{ + ShowWindow(SW_HIDE); +} + +void GSWnd::OnClose() +{ + Hide(); + + PostMessage(WM_QUIT); +} \ No newline at end of file diff --git a/plugins/GSdx/GSWnd.h b/plugins/GSdx/GSWnd.h new file mode 100644 index 0000000000..27a9773521 --- /dev/null +++ b/plugins/GSdx/GSWnd.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +class GSWnd : public CWnd +{ + DECLARE_MESSAGE_MAP() + +public: + GSWnd(); + virtual ~GSWnd(); + + virtual bool Create(LPCTSTR title); + + void Show(); + void Hide(); + void OnClose(); +}; diff --git a/plugins/GSdx/GSdx.cpp b/plugins/GSdx/GSdx.cpp new file mode 100644 index 0000000000..e20ebb5548 --- /dev/null +++ b/plugins/GSdx/GSdx.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "stdafx.h" +#include "GSdx.h" + +// +// Note! +// +// If this DLL is dynamically linked against the MFC +// DLLs, any functions exported from this DLL which +// call into MFC must have the AFX_MANAGE_STATE macro +// added at the very beginning of the function. +// +// For example: +// +// extern "C" BOOL PASCAL EXPORT ExportedFunction() +// { +// AFX_MANAGE_STATE(AfxGetStaticModuleState()); +// // normal function body here +// } +// +// It is very important that this macro appear in each +// function, prior to any calls into MFC. This means that +// it must appear as the first statement within the +// function, even before any object variable declarations +// as their constructors may generate calls into the MFC +// DLL. +// +// Please see MFC Technical Notes 33 and 58 for additional +// details. +// + +BEGIN_MESSAGE_MAP(GSdxApp, CWinApp) +END_MESSAGE_MAP() + +GSdxApp::GSdxApp() +{ +} + +GSdxApp theApp; + +BOOL GSdxApp::InitInstance() +{ + __super::InitInstance(); + + SetRegistryKey(_T("Gabest")); + + CString str; + GetModuleFileName(AfxGetInstanceHandle(), str.GetBuffer(MAX_PATH), MAX_PATH); + str.ReleaseBuffer(); + + CPath path(str); + path.RenameExtension(_T(".ini")); + + CPath fn = path; + fn.StripPath(); + + path.RemoveFileSpec(); + path.Append(_T("..\\inis")); + CreateDirectory(path, NULL); + path.Append(fn); + + if(m_pszRegistryKey) + { + free((void*)m_pszRegistryKey); + } + + m_pszRegistryKey = NULL; + + if(m_pszProfileName) + { + free((void*)m_pszProfileName); + } + + m_pszProfileName = _tcsdup((LPCTSTR)path); + + return TRUE; +} \ No newline at end of file diff --git a/plugins/GSdx/GSdx.def b/plugins/GSdx/GSdx.def new file mode 100644 index 0000000000..fd1bf9dbb5 --- /dev/null +++ b/plugins/GSdx/GSdx.def @@ -0,0 +1,63 @@ +; GSdx.def : Declares the module parameters for the DLL. + +LIBRARY "GSdx" + +EXPORTS + ; Explicit exports can go here + PS2EgetLibType + PS2EgetLibName + PS2EgetLibVersion2 + PS2EgetCpuPlatform + GSsetBaseMem + GSinit + GSshutdown + GSopen + GSclose + GSreset + GSwriteCSR + GSgifSoftReset + GSgifTransfer1 + GSgifTransfer2 + GSgifTransfer3 + GSvsync + GSmakeSnapshot + GSkeyEvent + GSfreeze + GSconfigure + GStest + GSabout + GSreadFIFO + GSreadFIFO2 + GSirqCallback + GSsetGameCRC + GSsetFrameSkip + GSgetLastTag + GSReplay + GSBenchmark + PSEgetLibType + PSEgetLibName + PSEgetLibVersion + GPUinit + GPUshutdown + GPUopen + GPUclose + GPUconfigure + GPUabout + GPUtest + GPUwriteData + GPUwriteStatus + GPUreadData + GPUreadStatus + GPUdmaChain + GPUgetMode + GPUsetMode + GPUupdateLace + GPUmakeSnapshot + GPUwriteDataMem + GPUreadDataMem + GPUdisplayText + GPUdisplayFlags + GPUfreeze + GPUshowScreenPic + GPUgetScreenPic + GPUcursor diff --git a/plugins/GSdx/GSdx.h b/plugins/GSdx/GSdx.h new file mode 100644 index 0000000000..c1ba09a618 --- /dev/null +++ b/plugins/GSdx/GSdx.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +#ifndef __AFXWIN_H__ + #error include 'stdafx.h' before including this file for PCH +#endif + +class GSdxApp : public CWinApp +{ +public: + GSdxApp(); + +public: + virtual BOOL InitInstance(); + + DECLARE_MESSAGE_MAP() +}; diff --git a/plugins/GSdx/GSdx.rc b/plugins/GSdx/GSdx.rc new file mode 100644 index 0000000000..81c53bc620 --- /dev/null +++ b/plugins/GSdx/GSdx.rc @@ -0,0 +1,269 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// Hungarian resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_HUN) +#ifdef _WIN32 +LANGUAGE LANG_HUNGARIAN, SUBLANG_DEFAULT +#pragma code_page(1250) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "#define _AFX_NO_SPLITTER_RESOURCES\r\n" + "#define _AFX_NO_OLE_RESOURCES\r\n" + "#define _AFX_NO_TRACKER_RESOURCES\r\n" + "#define _AFX_NO_PROPERTY_RESOURCES\r\n" + "\r\n" + "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n" + "LANGUAGE 9, 1\r\n" + "#pragma code_page(1252)\r\n" + "#include ""res\\GSdx.rc2"" // non-Microsoft Visual C++ edited resources\r\n" + "#include ""afxres.rc"" // Standard components\r\n" + "#endif\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + +#endif // Hungarian resources +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Bitmap +// + +IDB_LOGO9 BITMAP "res\\logo9.bmp" +IDB_LOGO10 BITMAP "res\\logo10.bmp" + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_CONFIG DIALOGEX 0, 0, 189, 231 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Settings..." +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + CONTROL 2021,IDC_LOGO9,"Static",SS_BITMAP,7,7,175,44 + CONTROL 2022,IDC_LOGO10,"Static",SS_BITMAP,7,7,175,44 + LTEXT "Resolution:",IDC_STATIC,7,59,37,8 + COMBOBOX IDC_COMBO3,71,57,111,125,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Renderer:",IDC_STATIC,7,74,34,8 + COMBOBOX IDC_COMBO1,71,72,111,118,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Shader:",IDC_STATIC,7,89,26,8 + COMBOBOX IDC_COMBO4,71,87,111,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Interlacing (F5):",IDC_STATIC,7,105,53,8 + COMBOBOX IDC_COMBO2,71,102,111,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Aspect Ratio (F6):",IDC_STATIC,7,120,60,8 + COMBOBOX IDC_COMBO5,71,117,111,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "D3D internal res:",IDC_STATIC,7,135,55,8 + EDITTEXT IDC_EDIT1,71,132,35,13,ES_AUTOHSCROLL | ES_NUMBER + CONTROL "",IDC_SPIN1,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS,99,135,11,14 + EDITTEXT IDC_EDIT2,109,132,35,13,ES_AUTOHSCROLL | ES_NUMBER + CONTROL "",IDC_SPIN2,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS,133,135,11,14 + CONTROL "Native",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,149,134,33,10 + CONTROL "Texture filtering",IDC_CHECK4,"Button",BS_AUTO3STATE | WS_TABSTOP,7,180,67,10 + CONTROL "Enable tv-out",IDC_CHECK3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,80,167,57,10 + CONTROL "Wait vsync",IDC_CHECK2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,193,51,10 + CONTROL "NLOOP hack",IDC_CHECK6,"Button",BS_AUTO3STATE | WS_TABSTOP,7,167,55,10 + CONTROL "Logarithmic Z",IDC_CHECK5,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,80,180,58,10 + CONTROL "Alpha correction (FBA)",IDC_CHECK7,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,80,193,102,10 + DEFPUSHBUTTON "OK",IDOK,43,210,50,14 + PUSHBUTTON "Cancel",IDCANCEL,96,210,50,14 + LTEXT "SW rend. threads:",IDC_STATIC,7,149,60,8 + EDITTEXT IDC_EDIT3,71,147,35,13,ES_AUTOHSCROLL | ES_NUMBER + CONTROL "",IDC_SPIN3,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS,99,150,11,14 +END + +IDD_CAPTURE DIALOGEX 0, 0, 279, 71 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Capture settings" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + EDITTEXT IDC_EDIT1,7,7,207,14,ES_AUTOHSCROLL + PUSHBUTTON "Browse...",IDC_BUTTON1,222,7,50,14 + COMBOBOX IDC_COMBO1,7,27,207,122,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + PUSHBUTTON "Config...",IDC_BUTTON2,222,26,50,14 + LTEXT "Size:",IDC_STATIC,6,50,16,8 + EDITTEXT IDC_EDIT2,30,47,31,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + EDITTEXT IDC_EDIT4,64,47,31,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + PUSHBUTTON "Cancel",IDCANCEL,169,47,50,14 + DEFPUSHBUTTON "OK",IDOK,221,47,50,14 +END + +IDD_GPUCONFIG DIALOGEX 0, 0, 189, 235 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Settings..." +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + CONTROL 2021,IDC_LOGO9,"Static",SS_BITMAP,7,7,175,44 + LTEXT "Resolution:",IDC_STATIC,7,59,37,8 + COMBOBOX IDC_COMBO3,78,57,104,125,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Renderer:",IDC_STATIC,7,74,34,8 + COMBOBOX IDC_COMBO1,78,72,104,118,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Shader:",IDC_STATIC,7,89,26,8 + COMBOBOX IDC_COMBO4,78,87,104,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Texture Filter (Del):",IDC_STATIC,7,105,64,8 + COMBOBOX IDC_COMBO2,78,102,104,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Dithering (End):",IDC_STATIC,7,120,52,8 + COMBOBOX IDC_COMBO5,78,117,104,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Aspect Ratio (PgDn):",IDC_STATIC,7,135,68,8 + COMBOBOX IDC_COMBO6,78,132,104,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + LTEXT "Rendering Threads:",IDC_STATIC,7,165,64,8 + EDITTEXT IDC_EDIT3,78,163,35,13,ES_AUTOHSCROLL | ES_NUMBER + CONTROL "",IDC_SPIN3,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS,99,169,11,14 + DEFPUSHBUTTON "OK",IDOK,43,214,50,14 + PUSHBUTTON "Cancel",IDCANCEL,96,214,50,14 + CONTROL 2022,IDC_LOGO10,"Static",SS_BITMAP,7,7,175,44 + LTEXT "Internal Resolution:",IDC_STATIC,7,150,64,8 + COMBOBOX IDC_COMBO7,78,147,104,98,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO +BEGIN + IDD_CONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 182 + VERTGUIDE, 71 + VERTGUIDE, 80 + VERTGUIDE, 182 + TOPMARGIN, 7 + BOTTOMMARGIN, 224 + END + + IDD_CAPTURE, DIALOG + BEGIN + VERTGUIDE, 6 + VERTGUIDE, 30 + VERTGUIDE, 271 + HORZGUIDE, 54 + END + + IDD_GPUCONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 182 + VERTGUIDE, 78 + VERTGUIDE, 182 + TOPMARGIN, 7 + BOTTOMMARGIN, 228 + END +END +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,1,9 + PRODUCTVERSION 1,0,1,9 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904e4" + BEGIN + VALUE "Comments", "http://guliverkli.sf.net/" + VALUE "CompanyName", "Gabest" + VALUE "FileDescription", "GS plugin for ps2 emulators" + VALUE "FileVersion", "1, 0, 1, 9" + VALUE "InternalName", "GSdx.dll" + VALUE "LegalCopyright", "Copyright (c) 2007-2008 Gabest. All rights reserved." + VALUE "OriginalFilename", "GSdx.dll" + VALUE "ProductName", "GSdx" + VALUE "ProductVersion", "1, 0, 1, 9" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1252 + END +END + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// +#define _AFX_NO_SPLITTER_RESOURCES +#define _AFX_NO_OLE_RESOURCES +#define _AFX_NO_TRACKER_RESOURCES +#define _AFX_NO_PROPERTY_RESOURCES + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 +#pragma code_page(1252) +#include "res\GSdx.rc2" // non-Microsoft Visual C++ edited resources +#include "afxres.rc" // Standard components +#endif + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/plugins/GSdx/GSdx_vs2008.vcproj b/plugins/GSdx/GSdx_vs2008.vcproj new file mode 100644 index 0000000000..545a8735c8 --- /dev/null +++ b/plugins/GSdx/GSdx_vs2008.vcproj @@ -0,0 +1,5054 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/GSdx/baseclasses/activex.rcv b/plugins/GSdx/baseclasses/activex.rcv new file mode 100644 index 0000000000..8aef99bfa5 --- /dev/null +++ b/plugins/GSdx/baseclasses/activex.rcv @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// File: Activex.rcv +// +// Desc: DirectShow base classes - this file defines the version resource +// used for the application. +// +// NOTE: All strings MUST have an explicit \0 for termination! +// +// For a complete description of the Version Resource, search the +// Microsoft Developer's Network (MSDN) CD-ROM for 'version resource'.. +// +// Copyright (c) 1992 - 2002, Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef _ACTIVEX_RCV_ +#define _ACTIVEX_RCV_ + +#ifndef WIN32 +#define WIN32 +#endif +#include + +#ifndef _ACTIVEX_VER_ +#include +#endif + +// +// Version flags. +// +// OFFICIAL and FINAL should be defined when appropriate. +// + +#ifndef OFFICIAL +#define VER_PRIVATEBUILD VS_FF_PRIVATEBUILD +#else +#define VER_PRIVATEBUILD 0 +#endif + +#ifndef FINAL +#define VER_PRERELEASE VS_FF_PRERELEASE +#else +#define VER_PRERELEASE 0 +#endif + +#ifdef DEBUG +#define VER_DEBUG VS_FF_DEBUG +#else +#define VER_DEBUG 0 +#endif + +// +// Version definitions +// + +#define VERSION_RES_FLAGSMASK 0x0030003FL +#define VERSION_RES_FLAGS (VER_PRIVATEBUILD|VER_PRERELEASE|VER_DEBUG) + +#ifndef VERSION_RES_OS +#define VERSION_RES_OS VOS__WINDOWS32 +#endif + +#ifndef VERSION_RES_TYPE +#define VERSION_RES_TYPE VFT_DLL +#endif + +#ifndef VERSION_RES_SUBTYPE +#define VERSION_RES_SUBTYPE VFT2_UNKNOWN +#endif + +#define VERSION_RES_LANGUAGE 0x409 + +#ifndef VERSION_RES_CHARSET +#ifdef UNICODE +#define VERSION_RES_CHARSET 1200 +#else +#define VERSION_RES_CHARSET 1252 +#endif +#endif + +#ifndef VERSION_RES_ACTIVEX +#define VERSION_RES_ACTIVEX "Filter dll\0" +#endif + +#ifdef AMOVIE_SELF_REGISTER +#ifndef OLE_SELF_REGISTER +#define OLE_SELF_REGISTER +#endif +#endif + +#ifdef OLE_SELF_REGISTER +#ifdef AMOVIE_SELF_REGISTER +#define VERSION_RES_SELFREGISTER "AM20\0" +#else +#define VERSION_RES_SELFREGISTER "\0" +#endif +#endif + +// +// Version resource +// + +VS_VERSION_INFO VERSIONINFO +FILEVERSION VERSION_RES_MAJOR_VER, VERSION_RES_MINOR_VER, 0, VERSION_RES_BUILD +PRODUCTVERSION VERSION_RES_MAJOR_VER, VERSION_RES_MINOR_VER, 0, VERSION_RES_BUILD +FILEFLAGSMASK VERSION_RES_FLAGSMASK +FILEFLAGS VERSION_RES_FLAGS +FILEOS VERSION_RES_OS +FILETYPE VERSION_RES_TYPE +FILESUBTYPE VERSION_RES_SUBTYPE +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904E4" + BEGIN + VALUE "CompanyName", VERSION_RES_COMPANY_NAME + VALUE "Comment", VERSION_RES_COMMENT + VALUE "FileDescription", VERSION_RES_BIN_DESCRIPTION + VALUE "FileVersion", VERSION_RES_STRING + VALUE "InternalName", VERSION_RES_BIN_NAME + VALUE "LegalCopyright", VERSION_RES_COPYRIGHT + VALUE "OriginalFilename", VERSION_RES_BIN_NAME + VALUE "ProductName", VERSION_RES_PRODUCT_NAME +#ifdef DEBUG + VALUE "ProductVersion", VERSION_RES_STRING_D +#else + VALUE "ProductVersion", VERSION_RES_STRING +#endif + VALUE "ActiveMovie", VERSION_RES_ACTIVEX +#ifdef OLE_SELF_REGISTER + VALUE "OLESelfRegister", VERSION_RES_SELFREGISTER +#endif + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", VERSION_RES_LANGUAGE, VERSION_RES_CHARSET + END +END + +#endif +// _ACTIVEX_RCV_ diff --git a/plugins/GSdx/baseclasses/activex.ver b/plugins/GSdx/baseclasses/activex.ver new file mode 100644 index 0000000000..4786f68eea --- /dev/null +++ b/plugins/GSdx/baseclasses/activex.ver @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// File: Activex.ver +// +// Desc: DirectShow base classes - common versioning information for +// ACTIVEX binaries. +// +// Copyright (c) 1996-2002, Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef _ACTIVEX_VER_ +#define _ACTIVEX_VER_ + +// NOTE: all string resources that will be used in ACTIVEX.RCV for the +// version resource information *MUST* have an explicit \0 terminator! + +#define VERSION_RES_MAJOR_VER 9 +#define VERSION_RES_MINOR_VER 0 +#define VERSION_RES_BUILD 0 + +#define VERSION_RES_STRING_D "9.00 (Debug)\0" +#define VERSION_RES_STRING "9.00\0" + +#define VERSION_RES_PRODUCT_NAME "DirectX 9.0 Sample\0" +#define VERSION_RES_COMMENT "DirectShow Sample\0" +#define VERSION_RES_COMPANY_NAME "Microsoft Corporation\0" +#define VERSION_RES_COPYRIGHT "Copyright (C) 1992-2002 Microsoft Corporation\0" + +// The following defines are required on a file-by-file basis +// +// #define VERSION_RES_BIN_NAME "sample.ax\0" +// #define VERSION_RES_BIN_DESCRIPTION "Sample Filter\0" +// +// Also required, if you don't want the defaults, are +// +// #define VERSION_RES_ACTIVEX "Filter dll\0" (the default value) +// +// A string defining the type of component. +// +// #define VERSION_RES_TYPE VFT_DLL (default) +// VFT_APP +// VFT_VXD +// VFT_DRV +// VFT_FONT +// VFT_STATIC_LIB +// VFT_UNKNOWN +// +// #define VERSION_RES_SUBTYPE VFT2_UNKNOWN (default) +// VFT2_DRV_INSTALLABLE +// VFT2_DRV_SOUND +// +// +// See winver.h for further details + +#endif + diff --git a/plugins/GSdx/baseclasses/amextra.cpp b/plugins/GSdx/baseclasses/amextra.cpp new file mode 100644 index 0000000000..3330bd74af --- /dev/null +++ b/plugins/GSdx/baseclasses/amextra.cpp @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// File: AMExtra.cpp +// +// Desc: DirectShow base classes - implements CRenderedInputPin class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" // DirectShow base class definitions +#include // Needed for definition of timeGetTime +#include // Standard data type limit definitions +#include "measure.h" // Used for time critical log functions + +#include "amextra.h" + +#pragma warning(disable:4355) + +// Implements CRenderedInputPin class + +CRenderedInputPin::CRenderedInputPin(TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBaseInputPin(pObjectName, pFilter, pLock, phr, pName), + m_bAtEndOfStream(FALSE), + m_bCompleteNotified(FALSE) +{ +} +#ifdef UNICODE +CRenderedInputPin::CRenderedInputPin(CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBaseInputPin(pObjectName, pFilter, pLock, phr, pName), + m_bAtEndOfStream(FALSE), + m_bCompleteNotified(FALSE) +{ +} +#endif + +// Flush end of stream condition - caller should do any +// necessary stream level locking before calling this + +STDMETHODIMP CRenderedInputPin::EndOfStream() +{ + HRESULT hr = CheckStreaming(); + + // Do EC_COMPLETE handling for rendered pins + if (S_OK == hr && !m_bAtEndOfStream) { + m_bAtEndOfStream = TRUE; + FILTER_STATE fs; + EXECUTE_ASSERT(SUCCEEDED(m_pFilter->GetState(0, &fs))); + if (fs == State_Running) { + DoCompleteHandling(); + } + } + return hr; +} + + +// Called to complete the flush + +STDMETHODIMP CRenderedInputPin::EndFlush() +{ + CAutoLock lck(m_pLock); + + // Clean up renderer state + m_bAtEndOfStream = FALSE; + m_bCompleteNotified = FALSE; + + return CBaseInputPin::EndFlush(); +} + + +// Notify of Run() from filter + +HRESULT CRenderedInputPin::Run(REFERENCE_TIME tStart) +{ + UNREFERENCED_PARAMETER(tStart); + m_bCompleteNotified = FALSE; + if (m_bAtEndOfStream) { + DoCompleteHandling(); + } + return S_OK; +} + + +// Clear status on going into paused state + +HRESULT CRenderedInputPin::Active() +{ + m_bAtEndOfStream = FALSE; + m_bCompleteNotified = FALSE; + return CBaseInputPin::Active(); +} + + +// Do stuff to deliver end of stream + +void CRenderedInputPin::DoCompleteHandling() +{ + ASSERT(m_bAtEndOfStream); + if (!m_bCompleteNotified) { + m_bCompleteNotified = TRUE; + m_pFilter->NotifyEvent(EC_COMPLETE, S_OK, (LONG_PTR)(IBaseFilter *)m_pFilter); + } +} + diff --git a/plugins/GSdx/baseclasses/amextra.h b/plugins/GSdx/baseclasses/amextra.h new file mode 100644 index 0000000000..ddb067ffcf --- /dev/null +++ b/plugins/GSdx/baseclasses/amextra.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// File: AMExtra.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __AMEXTRA__ +#define __AMEXTRA__ + +// Simple rendered input pin +// +// NOTE if your filter queues stuff before rendering then it may not be +// appropriate to use this class +// +// In that case queue the end of stream condition until the last sample +// is actually rendered and flush the condition appropriately + +class CRenderedInputPin : public CBaseInputPin +{ +public: + + CRenderedInputPin(TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#ifdef UNICODE + CRenderedInputPin(CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#endif + + // Override methods to track end of stream state + STDMETHODIMP EndOfStream(); + STDMETHODIMP EndFlush(); + + HRESULT Active(); + HRESULT Run(REFERENCE_TIME tStart); + +protected: + + // Member variables to track state + BOOL m_bAtEndOfStream; // Set by EndOfStream + BOOL m_bCompleteNotified; // Set when we notify for EC_COMPLETE + +private: + void DoCompleteHandling(); +}; + +#endif // __AMEXTRA__ + diff --git a/plugins/GSdx/baseclasses/amfilter.cpp b/plugins/GSdx/baseclasses/amfilter.cpp new file mode 100644 index 0000000000..112dba97c1 --- /dev/null +++ b/plugins/GSdx/baseclasses/amfilter.cpp @@ -0,0 +1,5203 @@ +//------------------------------------------------------------------------------ +// File: AMFilter.cpp +// +// Desc: DirectShow base classes - implements class hierarchy for streams +// architecture. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +//===================================================================== +//===================================================================== +// The following classes are declared in this header: +// +// +// CBaseMediaFilter Basic IMediaFilter support (abstract class) +// CBaseFilter Support for IBaseFilter (incl. IMediaFilter) +// CEnumPins Enumerate input and output pins +// CEnumMediaTypes Enumerate the preferred pin formats +// CBasePin Abstract base class for IPin interface +// CBaseOutputPin Adds data provider member functions +// CBaseInputPin Implements IMemInputPin interface +// CMediaSample Basic transport unit for IMemInputPin +// CBaseAllocator General list guff for most allocators +// CMemAllocator Implements memory buffer allocation +// +//===================================================================== +//===================================================================== + +#include "streams.h" + + + +//===================================================================== +// Helpers +//===================================================================== +STDAPI CreateMemoryAllocator(IMemAllocator **ppAllocator) +{ + return CoCreateInstance(CLSID_MemoryAllocator, + 0, + CLSCTX_INPROC_SERVER, + IID_IMemAllocator, + (void **)ppAllocator); +} + +// Put this one here rather than in ctlutil.cpp to avoid linking +// anything brought in by ctlutil.cpp +STDAPI CreatePosPassThru( + LPUNKNOWN pAgg, + BOOL bRenderer, + IPin *pPin, + IUnknown **ppPassThru +) +{ + *ppPassThru = NULL; + IUnknown *pUnkSeek; + HRESULT hr = CoCreateInstance(CLSID_SeekingPassThru, + pAgg, + CLSCTX_INPROC_SERVER, + IID_IUnknown, + (void **)&pUnkSeek + ); + if (FAILED(hr)) { + return hr; + } + + ISeekingPassThru *pPassThru; + hr = pUnkSeek->QueryInterface(IID_ISeekingPassThru, (void**)&pPassThru); + if (FAILED(hr)) { + pUnkSeek->Release(); + return hr; + } + hr = pPassThru->Init(bRenderer, pPin); + pPassThru->Release(); + if (FAILED(hr)) { + pUnkSeek->Release(); + return hr; + } + *ppPassThru = pUnkSeek; + return S_OK; +} + + + +#define CONNECT_TRACE_LEVEL 3 + +//===================================================================== +//===================================================================== +// Implements CBaseMediaFilter +//===================================================================== +//===================================================================== + + +/* Constructor */ + +CBaseMediaFilter::CBaseMediaFilter(const TCHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid) : + CUnknown(pName, pUnk), + m_pLock(pLock), + m_clsid(clsid), + m_State(State_Stopped), + m_pClock(NULL) +{ +} + + +/* Destructor */ + +CBaseMediaFilter::~CBaseMediaFilter() +{ + // must be stopped, but can't call Stop here since + // our critsec has been destroyed. + + /* Release any clock we were using */ + + if (m_pClock) { + m_pClock->Release(); + m_pClock = NULL; + } +} + + +/* Override this to say what interfaces we support and where */ + +STDMETHODIMP +CBaseMediaFilter::NonDelegatingQueryInterface( + REFIID riid, + void ** ppv) +{ + if (riid == IID_IMediaFilter) { + return GetInterface((IMediaFilter *) this, ppv); + } else if (riid == IID_IPersist) { + return GetInterface((IPersist *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + +/* Return the filter's clsid */ +STDMETHODIMP +CBaseMediaFilter::GetClassID(CLSID *pClsID) +{ + CheckPointer(pClsID,E_POINTER); + ValidateReadWritePtr(pClsID,sizeof(CLSID)); + *pClsID = m_clsid; + return NOERROR; +} + +/* Override this if your state changes are not done synchronously */ + +STDMETHODIMP +CBaseMediaFilter::GetState(DWORD dwMSecs, FILTER_STATE *State) +{ + UNREFERENCED_PARAMETER(dwMSecs); + CheckPointer(State,E_POINTER); + ValidateReadWritePtr(State,sizeof(FILTER_STATE)); + + *State = m_State; + return S_OK; +} + + +/* Set the clock we will use for synchronisation */ + +STDMETHODIMP +CBaseMediaFilter::SetSyncSource(IReferenceClock *pClock) +{ + CAutoLock cObjectLock(m_pLock); + + // Ensure the new one does not go away - even if the same as the old + if (pClock) { + pClock->AddRef(); + } + + // if we have a clock, release it + if (m_pClock) { + m_pClock->Release(); + } + + // Set the new reference clock (might be NULL) + // Should we query it to ensure it is a clock? Consider for a debug build. + m_pClock = pClock; + + return NOERROR; +} + +/* Return the clock we are using for synchronisation */ +STDMETHODIMP +CBaseMediaFilter::GetSyncSource(IReferenceClock **pClock) +{ + CheckPointer(pClock,E_POINTER); + ValidateReadWritePtr(pClock,sizeof(IReferenceClock *)); + CAutoLock cObjectLock(m_pLock); + + if (m_pClock) { + // returning an interface... addref it... + m_pClock->AddRef(); + } + *pClock = (IReferenceClock*)m_pClock; + return NOERROR; +} + + +/* Put the filter into a stopped state */ + +STDMETHODIMP +CBaseMediaFilter::Stop() +{ + CAutoLock cObjectLock(m_pLock); + + m_State = State_Stopped; + return S_OK; +} + + +/* Put the filter into a paused state */ + +STDMETHODIMP +CBaseMediaFilter::Pause() +{ + CAutoLock cObjectLock(m_pLock); + + m_State = State_Paused; + return S_OK; +} + + +// Put the filter into a running state. + +// The time parameter is the offset to be added to the samples' +// stream time to get the reference time at which they should be presented. +// +// you can either add these two and compare it against the reference clock, +// or you can call CBaseMediaFilter::StreamTime and compare that against +// the sample timestamp. + +STDMETHODIMP +CBaseMediaFilter::Run(REFERENCE_TIME tStart) +{ + CAutoLock cObjectLock(m_pLock); + + // remember the stream time offset + m_tStart = tStart; + + if (m_State == State_Stopped){ + HRESULT hr = Pause(); + + if (FAILED(hr)) { + return hr; + } + } + m_State = State_Running; + return S_OK; +} + + +// +// return the current stream time - samples with start timestamps of this +// time or before should be rendered by now +HRESULT +CBaseMediaFilter::StreamTime(CRefTime& rtStream) +{ + // Caller must lock for synchronization + // We can't grab the filter lock because we want to be able to call + // this from worker threads without deadlocking + + if (m_pClock == NULL) { + return VFW_E_NO_CLOCK; + } + + // get the current reference time + HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream); + if (FAILED(hr)) { + return hr; + } + + // subtract the stream offset to get stream time + rtStream -= m_tStart; + + return S_OK; +} + + +//===================================================================== +//===================================================================== +// Implements CBaseFilter +//===================================================================== +//===================================================================== + + +/* Override this to say what interfaces we support and where */ + +STDMETHODIMP CBaseFilter::NonDelegatingQueryInterface(REFIID riid, + void **ppv) +{ + /* Do we have this interface */ + + if (riid == IID_IBaseFilter) { + return GetInterface((IBaseFilter *) this, ppv); + } else if (riid == IID_IMediaFilter) { + return GetInterface((IMediaFilter *) this, ppv); + } else if (riid == IID_IPersist) { + return GetInterface((IPersist *) this, ppv); + } else if (riid == IID_IAMovieSetup) { + return GetInterface((IAMovieSetup *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + +#ifdef DEBUG +STDMETHODIMP_(ULONG) CBaseFilter::NonDelegatingRelease() +{ + if (m_cRef == 1) { + KASSERT(m_pGraph == NULL); + } + return CUnknown::NonDelegatingRelease(); +} +#endif + + +/* Constructor */ + +CBaseFilter::CBaseFilter(const TCHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid) : + CUnknown( pName, pUnk ), + m_pLock(pLock), + m_clsid(clsid), + m_State(State_Stopped), + m_pClock(NULL), + m_pGraph(NULL), + m_pSink(NULL), + m_pName(NULL), + m_PinVersion(1) +{ + + ASSERT(pLock != NULL); +} + +/* Passes in a redundant HRESULT argument */ + +CBaseFilter::CBaseFilter(TCHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid, + HRESULT *phr) : + CUnknown( pName, pUnk ), + m_pLock(pLock), + m_clsid(clsid), + m_State(State_Stopped), + m_pClock(NULL), + m_pGraph(NULL), + m_pSink(NULL), + m_pName(NULL), + m_PinVersion(1) +{ + + ASSERT(pLock != NULL); + UNREFERENCED_PARAMETER(phr); +} + +#ifdef UNICODE +CBaseFilter::CBaseFilter(const CHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid) : + CUnknown( pName, pUnk ), + m_pLock(pLock), + m_clsid(clsid), + m_State(State_Stopped), + m_pClock(NULL), + m_pGraph(NULL), + m_pSink(NULL), + m_pName(NULL), + m_PinVersion(1) +{ + + ASSERT(pLock != NULL); +} +CBaseFilter::CBaseFilter(CHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid, + HRESULT *phr) : + CUnknown( pName, pUnk ), + m_pLock(pLock), + m_clsid(clsid), + m_State(State_Stopped), + m_pClock(NULL), + m_pGraph(NULL), + m_pSink(NULL), + m_pName(NULL), + m_PinVersion(1) +{ + + ASSERT(pLock != NULL); + UNREFERENCED_PARAMETER(phr); +} +#endif + +/* Destructor */ + +CBaseFilter::~CBaseFilter() +{ + + // NOTE we do NOT hold references on the filtergraph for m_pGraph or m_pSink + // When we did we had the circular reference problem. Nothing would go away. + + delete[] m_pName; + + // must be stopped, but can't call Stop here since + // our critsec has been destroyed. + + /* Release any clock we were using */ + if (m_pClock) { + m_pClock->Release(); + m_pClock = NULL; + } +} + +/* Return the filter's clsid */ +STDMETHODIMP +CBaseFilter::GetClassID(CLSID *pClsID) +{ + CheckPointer(pClsID,E_POINTER); + ValidateReadWritePtr(pClsID,sizeof(CLSID)); + *pClsID = m_clsid; + return NOERROR; +} + +/* Override this if your state changes are not done synchronously */ +STDMETHODIMP +CBaseFilter::GetState(DWORD dwMSecs, FILTER_STATE *State) +{ + UNREFERENCED_PARAMETER(dwMSecs); + CheckPointer(State,E_POINTER); + ValidateReadWritePtr(State,sizeof(FILTER_STATE)); + + *State = m_State; + return S_OK; +} + + +/* Set the clock we will use for synchronisation */ + +STDMETHODIMP +CBaseFilter::SetSyncSource(IReferenceClock *pClock) +{ + CAutoLock cObjectLock(m_pLock); + + // Ensure the new one does not go away - even if the same as the old + if (pClock) { + pClock->AddRef(); + } + + // if we have a clock, release it + if (m_pClock) { + m_pClock->Release(); + } + + // Set the new reference clock (might be NULL) + // Should we query it to ensure it is a clock? Consider for a debug build. + m_pClock = pClock; + + return NOERROR; +} + +/* Return the clock we are using for synchronisation */ +STDMETHODIMP +CBaseFilter::GetSyncSource(IReferenceClock **pClock) +{ + CheckPointer(pClock,E_POINTER); + ValidateReadWritePtr(pClock,sizeof(IReferenceClock *)); + CAutoLock cObjectLock(m_pLock); + + if (m_pClock) { + // returning an interface... addref it... + m_pClock->AddRef(); + } + *pClock = (IReferenceClock*)m_pClock; + return NOERROR; +} + + + +// override CBaseMediaFilter Stop method, to deactivate any pins this +// filter has. +STDMETHODIMP +CBaseFilter::Stop() +{ + CAutoLock cObjectLock(m_pLock); + HRESULT hr = NOERROR; + + // notify all pins of the state change + if (m_State != State_Stopped) { + int cPins = GetPinCount(); + for (int c = 0; c < cPins; c++) { + + CBasePin *pPin = GetPin(c); + + // Disconnected pins are not activated - this saves pins worrying + // about this state themselves. We ignore the return code to make + // sure everyone is inactivated regardless. The base input pin + // class can return an error if it has no allocator but Stop can + // be used to resync the graph state after something has gone bad + + if (pPin->IsConnected()) { + HRESULT hrTmp = pPin->Inactive(); + if (FAILED(hrTmp) && SUCCEEDED(hr)) { + hr = hrTmp; + } + } + } + } + + + m_State = State_Stopped; + return hr; +} + + +// override CBaseMediaFilter Pause method to activate any pins +// this filter has (also called from Run) + +STDMETHODIMP +CBaseFilter::Pause() +{ + CAutoLock cObjectLock(m_pLock); + + // notify all pins of the change to active state + if (m_State == State_Stopped) { + int cPins = GetPinCount(); + for (int c = 0; c < cPins; c++) { + + CBasePin *pPin = GetPin(c); + + // Disconnected pins are not activated - this saves pins + // worrying about this state themselves + + if (pPin->IsConnected()) { + HRESULT hr = pPin->Active(); + if (FAILED(hr)) { + return hr; + } + } + } + } + + + + m_State = State_Paused; + return S_OK; +} + +// Put the filter into a running state. + +// The time parameter is the offset to be added to the samples' +// stream time to get the reference time at which they should be presented. +// +// you can either add these two and compare it against the reference clock, +// or you can call CBaseFilter::StreamTime and compare that against +// the sample timestamp. + +STDMETHODIMP +CBaseFilter::Run(REFERENCE_TIME tStart) +{ + CAutoLock cObjectLock(m_pLock); + + // remember the stream time offset + m_tStart = tStart; + + if (m_State == State_Stopped){ + HRESULT hr = Pause(); + + if (FAILED(hr)) { + return hr; + } + } + // notify all pins of the change to active state + if (m_State != State_Running) { + int cPins = GetPinCount(); + for (int c = 0; c < cPins; c++) { + + CBasePin *pPin = GetPin(c); + + // Disconnected pins are not activated - this saves pins + // worrying about this state themselves + + if (pPin->IsConnected()) { + HRESULT hr = pPin->Run(tStart); + if (FAILED(hr)) { + return hr; + } + } + } + } + + + m_State = State_Running; + return S_OK; +} + +// +// return the current stream time - samples with start timestamps of this +// time or before should be rendered by now +HRESULT +CBaseFilter::StreamTime(CRefTime& rtStream) +{ + // Caller must lock for synchronization + // We can't grab the filter lock because we want to be able to call + // this from worker threads without deadlocking + + if (m_pClock == NULL) { + return VFW_E_NO_CLOCK; + } + + // get the current reference time + HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream); + if (FAILED(hr)) { + return hr; + } + + // subtract the stream offset to get stream time + rtStream -= m_tStart; + + return S_OK; +} + + +/* Create an enumerator for the pins attached to this filter */ + +STDMETHODIMP +CBaseFilter::EnumPins(IEnumPins **ppEnum) +{ + CheckPointer(ppEnum,E_POINTER); + ValidateReadWritePtr(ppEnum,sizeof(IEnumPins *)); + + /* Create a new ref counted enumerator */ + + *ppEnum = new CEnumPins(this, + NULL); + + return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR; +} + + +// default behaviour of FindPin is to assume pins are named +// by their pin names +STDMETHODIMP +CBaseFilter::FindPin( + LPCWSTR Id, + IPin ** ppPin +) +{ + CheckPointer(ppPin,E_POINTER); + ValidateReadWritePtr(ppPin,sizeof(IPin *)); + + // We're going to search the pin list so maintain integrity + CAutoLock lck(m_pLock); + int iCount = GetPinCount(); + for (int i = 0; i < iCount; i++) { + CBasePin *pPin = GetPin(i); + ASSERT(pPin != NULL); + + if (0 == lstrcmpW(pPin->Name(), Id)) { + // Found one that matches + // + // AddRef() and return it + *ppPin = pPin; + pPin->AddRef(); + return S_OK; + } + } + *ppPin = NULL; + return VFW_E_NOT_FOUND; +} + +/* Return information about this filter */ + +STDMETHODIMP +CBaseFilter::QueryFilterInfo(FILTER_INFO * pInfo) +{ + CheckPointer(pInfo,E_POINTER); + ValidateReadWritePtr(pInfo,sizeof(FILTER_INFO)); + + if (m_pName) { + lstrcpynW(pInfo->achName, m_pName, sizeof(pInfo->achName)/sizeof(WCHAR)); + } else { + pInfo->achName[0] = L'\0'; + } + pInfo->pGraph = m_pGraph; + if (m_pGraph) + m_pGraph->AddRef(); + return NOERROR; +} + + +/* Provide the filter with a filter graph */ + +STDMETHODIMP +CBaseFilter::JoinFilterGraph( + IFilterGraph * pGraph, + LPCWSTR pName) +{ + CAutoLock cObjectLock(m_pLock); + + // NOTE: we no longer hold references on the graph (m_pGraph, m_pSink) + + m_pGraph = pGraph; + if (m_pGraph) { + HRESULT hr = m_pGraph->QueryInterface(IID_IMediaEventSink, + (void**) &m_pSink); + if (FAILED(hr)) { + ASSERT(m_pSink == NULL); + } + else m_pSink->Release(); // we do NOT keep a reference on it. + } else { + // if graph pointer is null, then we should + // also release the IMediaEventSink on the same object - we don't + // refcount it, so just set it to null + m_pSink = NULL; + } + + + if (m_pName) { + delete[] m_pName; + m_pName = NULL; + } + + if (pName) { + DWORD nameLen = lstrlenW(pName)+1; + m_pName = new WCHAR[nameLen]; + if (m_pName) { + CopyMemory(m_pName, pName, nameLen*sizeof(WCHAR)); + } else { + // !!! error here? + ASSERT(FALSE); + } + } + + + return NOERROR; +} + + +// return a Vendor information string. Optional - may return E_NOTIMPL. +// memory returned should be freed using CoTaskMemFree +// default implementation returns E_NOTIMPL +STDMETHODIMP +CBaseFilter::QueryVendorInfo( + LPWSTR* pVendorInfo) +{ + UNREFERENCED_PARAMETER(pVendorInfo); + return E_NOTIMPL; +} + + +// send an event notification to the filter graph if we know about it. +// returns S_OK if delivered, S_FALSE if the filter graph does not sink +// events, or an error otherwise. +HRESULT +CBaseFilter::NotifyEvent( + long EventCode, + LONG_PTR EventParam1, + LONG_PTR EventParam2) +{ + // Snapshot so we don't have to lock up + IMediaEventSink *pSink = m_pSink; + if (pSink) { + if (EC_COMPLETE == EventCode) { + EventParam2 = (LONG_PTR)(IBaseFilter*)this; + } + + return pSink->Notify(EventCode, EventParam1, EventParam2); + } else { + return E_NOTIMPL; + } +} + +// Request reconnect +// pPin is the pin to reconnect +// pmt is the type to reconnect with - can be NULL +// Calls ReconnectEx on the filter graph +HRESULT +CBaseFilter::ReconnectPin( + IPin *pPin, + AM_MEDIA_TYPE const *pmt +) +{ + IFilterGraph2 *pGraph2; + if (m_pGraph != NULL) { + HRESULT hr = m_pGraph->QueryInterface(IID_IFilterGraph2, (void **)&pGraph2); + if (SUCCEEDED(hr)) { + hr = pGraph2->ReconnectEx(pPin, pmt); + pGraph2->Release(); + return hr; + } else { + return m_pGraph->Reconnect(pPin); + } + } else { + return E_NOINTERFACE; + } +} + + + +/* This is the same idea as the media type version does for type enumeration + on pins but for the list of pins available. So if the list of pins you + provide changes dynamically then either override this virtual function + to provide the version number, or more simply call IncrementPinVersion */ + +LONG CBaseFilter::GetPinVersion() +{ + return m_PinVersion; +} + + +/* Increment the current pin version cookie */ + +void CBaseFilter::IncrementPinVersion() +{ + InterlockedIncrement(&m_PinVersion); +} + +/* register filter */ + +STDMETHODIMP CBaseFilter::Register() +{ + // get setup data, if it exists + // + LPAMOVIESETUP_FILTER psetupdata = GetSetupData(); + + // check we've got data + // + if( NULL == psetupdata ) return S_FALSE; + + // init is ref counted so call just in case + // we're being called cold. + // + HRESULT hr = CoInitialize( (LPVOID)NULL ); + ASSERT( SUCCEEDED(hr) ); + + // get hold of IFilterMapper + // + IFilterMapper *pIFM; + hr = CoCreateInstance( CLSID_FilterMapper + , NULL + , CLSCTX_INPROC_SERVER + , IID_IFilterMapper + , (void **)&pIFM ); + if( SUCCEEDED(hr) ) + { + hr = AMovieSetupRegisterFilter( psetupdata, pIFM, TRUE ); + pIFM->Release(); + } + + // and clear up + // + CoFreeUnusedLibraries(); + CoUninitialize(); + + return NOERROR; +} + + +/* unregister filter */ + +STDMETHODIMP CBaseFilter::Unregister() +{ + // get setup data, if it exists + // + LPAMOVIESETUP_FILTER psetupdata = GetSetupData(); + + // check we've got data + // + if( NULL == psetupdata ) return S_FALSE; + + // OLE init is ref counted so call + // just in case we're being called cold. + // + HRESULT hr = CoInitialize( (LPVOID)NULL ); + ASSERT( SUCCEEDED(hr) ); + + // get hold of IFilterMapper + // + IFilterMapper *pIFM; + hr = CoCreateInstance( CLSID_FilterMapper + , NULL + , CLSCTX_INPROC_SERVER + , IID_IFilterMapper + , (void **)&pIFM ); + if( SUCCEEDED(hr) ) + { + hr = AMovieSetupRegisterFilter( psetupdata, pIFM, FALSE ); + + // release interface + // + pIFM->Release(); + } + + // clear up + // + CoFreeUnusedLibraries(); + CoUninitialize(); + + // handle one acceptable "error" - that + // of filter not being registered! + // (couldn't find a suitable #define'd + // name for the error!) + // + if( 0x80070002 == hr) + return NOERROR; + else + return hr; +} + + +//===================================================================== +//===================================================================== +// Implements CEnumPins +//===================================================================== +//===================================================================== + + +CEnumPins::CEnumPins(CBaseFilter *pFilter, + CEnumPins *pEnumPins) : + m_Position(0), + m_PinCount(0), + m_pFilter(pFilter), + m_cRef(1), // Already ref counted + m_PinCache(NAME("Pin Cache")) +{ + +#ifdef DEBUG + m_dwCookie = DbgRegisterObjectCreation("CEnumPins", 0); +#endif + + /* We must be owned by a filter derived from CBaseFilter */ + + ASSERT(pFilter != NULL); + + /* Hold a reference count on our filter */ + m_pFilter->AddRef(); + + /* Are we creating a new enumerator */ + + if (pEnumPins == NULL) { + m_Version = m_pFilter->GetPinVersion(); + m_PinCount = m_pFilter->GetPinCount(); + } else { + ASSERT(m_Position <= m_PinCount); + m_Position = pEnumPins->m_Position; + m_PinCount = pEnumPins->m_PinCount; + m_Version = pEnumPins->m_Version; + m_PinCache.AddTail(&(pEnumPins->m_PinCache)); + } +} + + +/* Destructor releases the reference count on our filter NOTE since we hold + a reference count on the filter who created us we know it is safe to + release it, no access can be made to it afterwards though as we have just + caused the last reference count to go and the object to be deleted */ + +CEnumPins::~CEnumPins() +{ + m_pFilter->Release(); + +#ifdef DEBUG + DbgRegisterObjectDestruction(m_dwCookie); +#endif +} + + +/* Override this to say what interfaces we support where */ + +STDMETHODIMP +CEnumPins::QueryInterface(REFIID riid,void **ppv) +{ + CheckPointer(ppv, E_POINTER); + + /* Do we have this interface */ + + if (riid == IID_IEnumPins || riid == IID_IUnknown) { + return GetInterface((IEnumPins *) this, ppv); + } else { + *ppv = NULL; + return E_NOINTERFACE; + } +} + +STDMETHODIMP_(ULONG) +CEnumPins::AddRef() +{ + return InterlockedIncrement(&m_cRef); +} + +STDMETHODIMP_(ULONG) +CEnumPins::Release() +{ + ULONG cRef = InterlockedDecrement(&m_cRef); + if (cRef == 0) { + delete this; + } + return cRef; +} + +/* One of an enumerator's basic member functions allows us to create a cloned + interface that initially has the same state. Since we are taking a snapshot + of an object (current position and all) we must lock access at the start */ + +STDMETHODIMP +CEnumPins::Clone(IEnumPins **ppEnum) +{ + CheckPointer(ppEnum,E_POINTER); + ValidateReadWritePtr(ppEnum,sizeof(IEnumPins *)); + HRESULT hr = NOERROR; + + /* Check we are still in sync with the filter */ + if (AreWeOutOfSync() == TRUE) { + *ppEnum = NULL; + hr = VFW_E_ENUM_OUT_OF_SYNC; + } else { + + *ppEnum = new CEnumPins(m_pFilter, + this); + if (*ppEnum == NULL) { + hr = E_OUTOFMEMORY; + } + } + return hr; +} + + +/* Return the next pin after the current position */ + +STDMETHODIMP +CEnumPins::Next(ULONG cPins, // place this many pins... + IPin **ppPins, // ...in this array + ULONG *pcFetched) // actual count passed returned here +{ + CheckPointer(ppPins,E_POINTER); + ValidateReadWritePtr(ppPins,cPins * sizeof(IPin *)); + + ASSERT(ppPins); + + if (pcFetched!=NULL) { + ValidateWritePtr(pcFetched, sizeof(ULONG)); + *pcFetched = 0; // default unless we succeed + } + // now check that the parameter is valid + else if (cPins>1) { // pcFetched == NULL + return E_INVALIDARG; + } + ULONG cFetched = 0; // increment as we get each one. + + /* Check we are still in sync with the filter */ + if (AreWeOutOfSync() == TRUE) { + // If we are out of sync, we should refresh the enumerator. + // This will reset the position and update the other members, but + // will not clear cache of pins we have already returned. + Refresh(); + } + + /* Calculate the number of available pins */ + + int cRealPins = min(m_PinCount - m_Position, (int) cPins); + if (cRealPins == 0) { + return S_FALSE; + } + + /* Return each pin interface NOTE GetPin returns CBasePin * not addrefed + so we must QI for the IPin (which increments its reference count) + If while we are retrieving a pin from the filter an error occurs we + assume that our internal state is stale with respect to the filter + (for example someone has deleted a pin) so we + return VFW_E_ENUM_OUT_OF_SYNC */ + + while (cRealPins && (m_PinCount - m_Position)) { + + /* Get the next pin object from the filter */ + + CBasePin *pPin = m_pFilter->GetPin(m_Position++); + if (pPin == NULL) { + // If this happend, and it's not the first time through, then we've got a problem, + // since we should really go back and release the iPins, which we have previously + // AddRef'ed. + ASSERT( cFetched==0 ); + return VFW_E_ENUM_OUT_OF_SYNC; + } + + /* We only want to return this pin, if it is not in our cache */ + if (0 == m_PinCache.Find(pPin)) + { + /* From the object get an IPin interface */ + + *ppPins = pPin; + pPin->AddRef(); + + cFetched++; + ppPins++; + + m_PinCache.AddTail(pPin); + + cRealPins--; + + } + } + + if (pcFetched!=NULL) { + *pcFetched = cFetched; + } + + return (cPins==cFetched ? NOERROR : S_FALSE); +} + + +/* Skip over one or more entries in the enumerator */ + +STDMETHODIMP +CEnumPins::Skip(ULONG cPins) +{ + /* Check we are still in sync with the filter */ + if (AreWeOutOfSync() == TRUE) { + return VFW_E_ENUM_OUT_OF_SYNC; + } + + /* Work out how many pins are left to skip over */ + /* We could position at the end if we are asked to skip too many... */ + /* ..which would match the base implementation for CEnumMediaTypes::Skip */ + + ULONG PinsLeft = m_PinCount - m_Position; + if (cPins > PinsLeft) { + return S_FALSE; + } + m_Position += cPins; + return NOERROR; +} + + +/* Set the current position back to the start */ +/* Reset has 4 simple steps: + * + * Set position to head of list + * Sync enumerator with object being enumerated + * Clear the cache of pins already returned + * return S_OK + */ + +STDMETHODIMP +CEnumPins::Reset() +{ + m_Version = m_pFilter->GetPinVersion(); + m_PinCount = m_pFilter->GetPinCount(); + + m_Position = 0; + + // Clear the cache + m_PinCache.RemoveAll(); + + return S_OK; +} + + +/* Set the current position back to the start */ +/* Refresh has 3 simple steps: + * + * Set position to head of list + * Sync enumerator with object being enumerated + * return S_OK + */ + +STDMETHODIMP +CEnumPins::Refresh() +{ + m_Version = m_pFilter->GetPinVersion(); + m_PinCount = m_pFilter->GetPinCount(); + + m_Position = 0; + return S_OK; +} + + +//===================================================================== +//===================================================================== +// Implements CEnumMediaTypes +//===================================================================== +//===================================================================== + + +CEnumMediaTypes::CEnumMediaTypes(CBasePin *pPin, + CEnumMediaTypes *pEnumMediaTypes) : + m_Position(0), + m_pPin(pPin), + m_cRef(1) +{ + +#ifdef DEBUG + m_dwCookie = DbgRegisterObjectCreation("CEnumMediaTypes", 0); +#endif + + /* We must be owned by a pin derived from CBasePin */ + + ASSERT(pPin != NULL); + + /* Hold a reference count on our pin */ + m_pPin->AddRef(); + + /* Are we creating a new enumerator */ + + if (pEnumMediaTypes == NULL) { + m_Version = m_pPin->GetMediaTypeVersion(); + return; + } + + m_Position = pEnumMediaTypes->m_Position; + m_Version = pEnumMediaTypes->m_Version; +} + + +/* Destructor releases the reference count on our base pin. NOTE since we hold + a reference count on the pin who created us we know it is safe to release + it, no access can be made to it afterwards though as we might have just + caused the last reference count to go and the object to be deleted */ + +CEnumMediaTypes::~CEnumMediaTypes() +{ +#ifdef DEBUG + DbgRegisterObjectDestruction(m_dwCookie); +#endif + m_pPin->Release(); +} + + +/* Override this to say what interfaces we support where */ + +STDMETHODIMP +CEnumMediaTypes::QueryInterface(REFIID riid,void **ppv) +{ + CheckPointer(ppv, E_POINTER); + + /* Do we have this interface */ + + if (riid == IID_IEnumMediaTypes || riid == IID_IUnknown) { + return GetInterface((IEnumMediaTypes *) this, ppv); + } else { + *ppv = NULL; + return E_NOINTERFACE; + } +} + +STDMETHODIMP_(ULONG) +CEnumMediaTypes::AddRef() +{ + return InterlockedIncrement(&m_cRef); +} + +STDMETHODIMP_(ULONG) +CEnumMediaTypes::Release() +{ + ULONG cRef = InterlockedDecrement(&m_cRef); + if (cRef == 0) { + delete this; + } + return cRef; +} + +/* One of an enumerator's basic member functions allows us to create a cloned + interface that initially has the same state. Since we are taking a snapshot + of an object (current position and all) we must lock access at the start */ + +STDMETHODIMP +CEnumMediaTypes::Clone(IEnumMediaTypes **ppEnum) +{ + CheckPointer(ppEnum,E_POINTER); + ValidateReadWritePtr(ppEnum,sizeof(IEnumMediaTypes *)); + HRESULT hr = NOERROR; + + /* Check we are still in sync with the pin */ + if (AreWeOutOfSync() == TRUE) { + *ppEnum = NULL; + hr = VFW_E_ENUM_OUT_OF_SYNC; + } else { + + *ppEnum = new CEnumMediaTypes(m_pPin, + this); + + if (*ppEnum == NULL) { + hr = E_OUTOFMEMORY; + } + } + return hr; +} + + +/* Enumerate the next pin(s) after the current position. The client using this + interface passes in a pointer to an array of pointers each of which will + be filled in with a pointer to a fully initialised media type format + Return NOERROR if it all works, + S_FALSE if fewer than cMediaTypes were enumerated. + VFW_E_ENUM_OUT_OF_SYNC if the enumerator has been broken by + state changes in the filter + The actual count always correctly reflects the number of types in the array. +*/ + +STDMETHODIMP +CEnumMediaTypes::Next(ULONG cMediaTypes, // place this many types... + AM_MEDIA_TYPE **ppMediaTypes, // ...in this array + ULONG *pcFetched) // actual count passed +{ + CheckPointer(ppMediaTypes,E_POINTER); + ValidateReadWritePtr(ppMediaTypes,cMediaTypes * sizeof(AM_MEDIA_TYPE *)); + /* Check we are still in sync with the pin */ + if (AreWeOutOfSync() == TRUE) { + return VFW_E_ENUM_OUT_OF_SYNC; + } + + if (pcFetched!=NULL) { + ValidateWritePtr(pcFetched, sizeof(ULONG)); + *pcFetched = 0; // default unless we succeed + } + // now check that the parameter is valid + else if (cMediaTypes>1) { // pcFetched == NULL + return E_INVALIDARG; + } + ULONG cFetched = 0; // increment as we get each one. + + /* Return each media type by asking the filter for them in turn - If we + have an error code retured to us while we are retrieving a media type + we assume that our internal state is stale with respect to the filter + (for example the window size changing) so we return + VFW_E_ENUM_OUT_OF_SYNC */ + + while (cMediaTypes) { + + CMediaType cmt; + + HRESULT hr = m_pPin->GetMediaType(m_Position++, &cmt); + if (S_OK != hr) { + break; + } + + /* We now have a CMediaType object that contains the next media type + but when we assign it to the array position we CANNOT just assign + the AM_MEDIA_TYPE structure because as soon as the object goes out of + scope it will delete the memory we have just copied. The function + we use is CreateMediaType which allocates a task memory block */ + + /* Transfer across the format block manually to save an allocate + and free on the format block and generally go faster */ + + *ppMediaTypes = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE)); + if (*ppMediaTypes == NULL) { + break; + } + + /* Do a regular copy */ + **ppMediaTypes = (AM_MEDIA_TYPE)cmt; + + /* Make sure the destructor doesn't free these */ + cmt.pbFormat = NULL; + cmt.cbFormat = NULL; + cmt.pUnk = NULL; + + + ppMediaTypes++; + cFetched++; + cMediaTypes--; + } + + if (pcFetched!=NULL) { + *pcFetched = cFetched; + } + + return ( cMediaTypes==0 ? NOERROR : S_FALSE ); +} + + +/* Skip over one or more entries in the enumerator */ + +STDMETHODIMP +CEnumMediaTypes::Skip(ULONG cMediaTypes) +{ + // If we're skipping 0 elements we're guaranteed to skip the + // correct number of elements + if (cMediaTypes == 0) { + return S_OK; + } + + /* Check we are still in sync with the pin */ + if (AreWeOutOfSync() == TRUE) { + return VFW_E_ENUM_OUT_OF_SYNC; + } + + m_Position += cMediaTypes; + + /* See if we're over the end */ + CMediaType cmt; + return S_OK == m_pPin->GetMediaType(m_Position - 1, &cmt) ? S_OK : S_FALSE; +} + + +/* Set the current position back to the start */ +/* Reset has 3 simple steps: + * + * set position to head of list + * sync enumerator with object being enumerated + * return S_OK + */ + +STDMETHODIMP +CEnumMediaTypes::Reset() + +{ + m_Position = 0; + + // Bring the enumerator back into step with the current state. This + // may be a noop but ensures that the enumerator will be valid on the + // next call. + m_Version = m_pPin->GetMediaTypeVersion(); + return NOERROR; +} + + +//===================================================================== +//===================================================================== +// Implements CBasePin +//===================================================================== +//===================================================================== + + +/* NOTE The implementation of this class calls the CUnknown constructor with + a NULL outer unknown pointer. This has the effect of making us a self + contained class, ie any QueryInterface, AddRef or Release calls will be + routed to the class's NonDelegatingUnknown methods. You will typically + find that the classes that do this then override one or more of these + virtual functions to provide more specialised behaviour. A good example + of this is where a class wants to keep the QueryInterface internal but + still wants its lifetime controlled by the external object */ + +/* Constructor */ + +CBasePin::CBasePin(TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName, + PIN_DIRECTION dir) : + CUnknown( pObjectName, NULL ), + m_pFilter(pFilter), + m_pLock(pLock), + m_pName(NULL), + m_Connected(NULL), + m_dir(dir), + m_bRunTimeError(FALSE), + m_pQSink(NULL), + m_TypeVersion(1), + m_tStart(), + m_tStop(MAX_TIME), + m_bCanReconnectWhenActive(false), + m_bTryMyTypesFirst(false), + m_dRate(1.0) +{ + /* WARNING - pFilter is often not a properly constituted object at + this state (in particular QueryInterface may not work) - this + is because its owner is often its containing object and we + have been called from the containing object's constructor so + the filter's owner has not yet had its CUnknown constructor + called + */ + + ASSERT(pFilter != NULL); + ASSERT(pLock != NULL); + + if (pName) { + DWORD nameLen = lstrlenW(pName)+1; + m_pName = new WCHAR[nameLen]; + if (m_pName) { + CopyMemory(m_pName, pName, nameLen*sizeof(WCHAR)); + } + } + +#ifdef DEBUG + m_cRef = 0; +#endif +} + +#ifdef UNICODE +CBasePin::CBasePin(CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName, + PIN_DIRECTION dir) : + CUnknown( pObjectName, NULL ), + m_pFilter(pFilter), + m_pLock(pLock), + m_pName(NULL), + m_Connected(NULL), + m_dir(dir), + m_bRunTimeError(FALSE), + m_pQSink(NULL), + m_TypeVersion(1), + m_tStart(), + m_tStop(MAX_TIME), + m_bCanReconnectWhenActive(false), + m_bTryMyTypesFirst(false), + m_dRate(1.0) +{ + /* WARNING - pFilter is often not a properly constituted object at + this state (in particular QueryInterface may not work) - this + is because its owner is often its containing object and we + have been called from the containing object's constructor so + the filter's owner has not yet had its CUnknown constructor + called + */ + + ASSERT(pFilter != NULL); + ASSERT(pLock != NULL); + + if (pName) { + DWORD nameLen = lstrlenW(pName)+1; + m_pName = new WCHAR[nameLen]; + if (m_pName) { + CopyMemory(m_pName, pName, nameLen*sizeof(WCHAR)); + } + } + +#ifdef DEBUG + m_cRef = 0; +#endif +} +#endif + +/* Destructor since a connected pin holds a reference count on us there is + no way that we can be deleted unless we are not currently connected */ + +CBasePin::~CBasePin() +{ + + // We don't call disconnect because if the filter is going away + // all the pins must have a reference count of zero so they must + // have been disconnected anyway - (but check the assumption) + ASSERT(m_Connected == FALSE); + + delete[] m_pName; + + // check the internal reference count is consistent + ASSERT(m_cRef == 0); +} + + +/* Override this to say what interfaces we support and where */ + +STDMETHODIMP +CBasePin::NonDelegatingQueryInterface(REFIID riid, void ** ppv) +{ + /* Do we have this interface */ + + if (riid == IID_IPin) { + return GetInterface((IPin *) this, ppv); + } else if (riid == IID_IQualityControl) { + return GetInterface((IQualityControl *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +/* Override to increment the owning filter's reference count */ + +STDMETHODIMP_(ULONG) +CBasePin::NonDelegatingAddRef() +{ + ASSERT(InterlockedIncrement(&m_cRef) > 0); + return m_pFilter->AddRef(); +} + + +/* Override to decrement the owning filter's reference count */ + +STDMETHODIMP_(ULONG) +CBasePin::NonDelegatingRelease() +{ + ASSERT(InterlockedDecrement(&m_cRef) >= 0); + return m_pFilter->Release(); +} + + +/* Displays pin connection information */ + +#ifdef DEBUG +void +CBasePin::DisplayPinInfo(IPin *pReceivePin) +{ + + if (DbgCheckModuleLevel(LOG_TRACE, CONNECT_TRACE_LEVEL)) { + PIN_INFO ConnectPinInfo; + PIN_INFO ReceivePinInfo; + + if (FAILED(QueryPinInfo(&ConnectPinInfo))) { + (void)StringCchCopyW(ConnectPinInfo.achName, NUMELMS(ConnectPinInfo.achName),L"Bad Pin"); + } else { + QueryPinInfoReleaseFilter(ConnectPinInfo); + } + + if (FAILED(pReceivePin->QueryPinInfo(&ReceivePinInfo))) { + (void)StringCchCopyW(ReceivePinInfo.achName, NUMELMS(ReceivePinInfo.achName),L"Bad Pin"); + } else { + QueryPinInfoReleaseFilter(ReceivePinInfo); + } + + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Trying to connect Pins :"))); + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" <%ls>"), ConnectPinInfo.achName)); + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" <%ls>"), ReceivePinInfo.achName)); + } +} +#endif + + +/* Displays general information on the pin media type */ + +#ifdef DEBUG +void CBasePin::DisplayTypeInfo(IPin *pPin, const CMediaType *pmt) +{ + UNREFERENCED_PARAMETER(pPin); + if (DbgCheckModuleLevel(LOG_TRACE, CONNECT_TRACE_LEVEL)) { + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Trying media type:"))); + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" major type: %hs"), + GuidNames[*pmt->Type()])); + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" sub type : %hs"), + GuidNames[*pmt->Subtype()])); + } +} +#endif + +/* Asked to connect to a pin. A pin is always attached to an owning filter + object so we always delegate our locking to that object. We first of all + retrieve a media type enumerator for the input pin and see if we accept + any of the formats that it would ideally like, failing that we retrieve + our enumerator and see if it will accept any of our preferred types */ + +STDMETHODIMP +CBasePin::Connect( + IPin * pReceivePin, + const AM_MEDIA_TYPE *pmt // optional media type +) +{ + CheckPointer(pReceivePin,E_POINTER); + ValidateReadPtr(pReceivePin,sizeof(IPin)); + CAutoLock cObjectLock(m_pLock); + DisplayPinInfo(pReceivePin); + + /* See if we are already connected */ + + if (m_Connected) { + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Already connected"))); + return VFW_E_ALREADY_CONNECTED; + } + + /* See if the filter is active */ + if (!IsStopped() && !m_bCanReconnectWhenActive) { + return VFW_E_NOT_STOPPED; + } + + + // Find a mutually agreeable media type - + // Pass in the template media type. If this is partially specified, + // each of the enumerated media types will need to be checked against + // it. If it is non-null and fully specified, we will just try to connect + // with this. + + const CMediaType * ptype = (CMediaType*)pmt; + HRESULT hr = AgreeMediaType(pReceivePin, ptype); + if (FAILED(hr)) { + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Failed to agree type"))); + + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + + return hr; + } + + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Connection succeeded"))); + + + return NOERROR; +} + +// given a specific media type, attempt a connection (includes +// checking that the type is acceptable to this pin) +HRESULT +CBasePin::AttemptConnection( + IPin* pReceivePin, // connect to this pin + const CMediaType* pmt // using this type +) +{ + // The caller should hold the filter lock becasue this function + // uses m_Connected. The caller should also hold the filter lock + // because this function calls SetMediaType(), IsStopped() and + // CompleteConnect(). + ASSERT(CritCheckIn(m_pLock)); + + // Check that the connection is valid -- need to do this for every + // connect attempt since BreakConnect will undo it. + HRESULT hr = CheckConnect(pReceivePin); + if (FAILED(hr)) { + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("CheckConnect failed"))); + + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + return hr; + } + + DisplayTypeInfo(pReceivePin, pmt); + + /* Check we will accept this media type */ + + hr = CheckMediaType(pmt); + if (hr == NOERROR) { + + /* Make ourselves look connected otherwise ReceiveConnection + may not be able to complete the connection + */ + m_Connected = pReceivePin; + m_Connected->AddRef(); + hr = SetMediaType(pmt); + if (SUCCEEDED(hr)) { + /* See if the other pin will accept this type */ + + hr = pReceivePin->ReceiveConnection((IPin *)this, pmt); + if (SUCCEEDED(hr)) { + /* Complete the connection */ + + hr = CompleteConnect(pReceivePin); + if (SUCCEEDED(hr)) { + return hr; + } else { + DbgLog((LOG_TRACE, + CONNECT_TRACE_LEVEL, + TEXT("Failed to complete connection"))); + pReceivePin->Disconnect(); + } + } + } + } else { + // we cannot use this media type + + // return a specific media type error if there is one + // or map a general failure code to something more helpful + // (in particular S_FALSE gets changed to an error code) + if (SUCCEEDED(hr) || + (hr == E_FAIL) || + (hr == E_INVALIDARG)) { + hr = VFW_E_TYPE_NOT_ACCEPTED; + } + } + + // BreakConnect and release any connection here in case CheckMediaType + // failed, or if we set anything up during a call back during + // ReceiveConnection. + + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + /* If failed then undo our state */ + if (m_Connected) { + m_Connected->Release(); + m_Connected = NULL; + } + + return hr; +} + +/* Given an enumerator we cycle through all the media types it proposes and + firstly suggest them to our derived pin class and if that succeeds try + them with the pin in a ReceiveConnection call. This means that if our pin + proposes a media type we still check in here that we can support it. This + is deliberate so that in simple cases the enumerator can hold all of the + media types even if some of them are not really currently available */ + +HRESULT CBasePin::TryMediaTypes( + IPin *pReceivePin, + const CMediaType *pmt, + IEnumMediaTypes *pEnum) +{ + /* Reset the current enumerator position */ + + HRESULT hr = pEnum->Reset(); + if (FAILED(hr)) { + return hr; + } + + CMediaType *pMediaType = NULL; + ULONG ulMediaCount = 0; + + // attempt to remember a specific error code if there is one + HRESULT hrFailure = S_OK; + + for (;;) { + + /* Retrieve the next media type NOTE each time round the loop the + enumerator interface will allocate another AM_MEDIA_TYPE structure + If we are successful then we copy it into our output object, if + not then we must delete the memory allocated before returning */ + + hr = pEnum->Next(1, (AM_MEDIA_TYPE**)&pMediaType,&ulMediaCount); + if (hr != S_OK) { + if (S_OK == hrFailure) { + hrFailure = VFW_E_NO_ACCEPTABLE_TYPES; + } + return hrFailure; + } + + + ASSERT(ulMediaCount == 1); + ASSERT(pMediaType); + + // check that this matches the partial type (if any) + + if ((pmt == NULL) || + pMediaType->MatchesPartial(pmt)) { + + hr = AttemptConnection(pReceivePin, pMediaType); + + // attempt to remember a specific error code + if (FAILED(hr) && + SUCCEEDED(hrFailure) && + (hr != E_FAIL) && + (hr != E_INVALIDARG) && + (hr != VFW_E_TYPE_NOT_ACCEPTED)) { + hrFailure = hr; + } + } else { + hr = VFW_E_NO_ACCEPTABLE_TYPES; + } + + DeleteMediaType(pMediaType); + + if (S_OK == hr) { + return hr; + } + } +} + + +/* This is called to make the connection, including the taask of finding + a media type for the pin connection. pmt is the proposed media type + from the Connect call: if this is fully specified, we will try that. + Otherwise we enumerate and try all the input pin's types first and + if that fails we then enumerate and try all our preferred media types. + For each media type we check it against pmt (if non-null and partially + specified) as well as checking that both pins will accept it. + */ + +HRESULT CBasePin::AgreeMediaType( + IPin *pReceivePin, + const CMediaType *pmt) +{ + ASSERT(pReceivePin); + IEnumMediaTypes *pEnumMediaTypes = NULL; + + // if the media type is fully specified then use that + if ( (pmt != NULL) && (!pmt->IsPartiallySpecified())) { + + // if this media type fails, then we must fail the connection + // since if pmt is nonnull we are only allowed to connect + // using a type that matches it. + + return AttemptConnection(pReceivePin, pmt); + } + + + /* Try the other pin's enumerator */ + + HRESULT hrFailure = VFW_E_NO_ACCEPTABLE_TYPES; + + for (int i = 0; i < 2; i++) { + HRESULT hr; + if (i == (int)m_bTryMyTypesFirst) { + hr = pReceivePin->EnumMediaTypes(&pEnumMediaTypes); + } else { + hr = EnumMediaTypes(&pEnumMediaTypes); + } + if (SUCCEEDED(hr)) { + ASSERT(pEnumMediaTypes); + hr = TryMediaTypes(pReceivePin,pmt,pEnumMediaTypes); + pEnumMediaTypes->Release(); + if (SUCCEEDED(hr)) { + return NOERROR; + } else { + // try to remember specific error codes if there are any + if ((hr != E_FAIL) && + (hr != E_INVALIDARG) && + (hr != VFW_E_TYPE_NOT_ACCEPTED)) { + hrFailure = hr; + } + } + } + } + + return hrFailure; +} + + +/* Called when we want to complete a connection to another filter. Failing + this will also fail the connection and disconnect the other pin as well */ + +HRESULT +CBasePin::CompleteConnect(IPin *pReceivePin) +{ + UNREFERENCED_PARAMETER(pReceivePin); + return NOERROR; +} + + +/* This is called to set the format for a pin connection - CheckMediaType + will have been called to check the connection format and if it didn't + return an error code then this (virtual) function will be invoked */ + +HRESULT +CBasePin::SetMediaType(const CMediaType *pmt) +{ + HRESULT hr = m_mt.Set(*pmt); + if (FAILED(hr)) { + return hr; + } + + return NOERROR; +} + + +/* This is called during Connect() to provide a virtual method that can do + any specific check needed for connection such as QueryInterface. This + base class method just checks that the pin directions don't match */ + +HRESULT +CBasePin::CheckConnect(IPin * pPin) +{ + /* Check that pin directions DONT match */ + + PIN_DIRECTION pd; + pPin->QueryDirection(&pd); + + ASSERT((pd == PINDIR_OUTPUT) || (pd == PINDIR_INPUT)); + ASSERT((m_dir == PINDIR_OUTPUT) || (m_dir == PINDIR_INPUT)); + + // we should allow for non-input and non-output connections? + if (pd == m_dir) { + return VFW_E_INVALID_DIRECTION; + } + return NOERROR; +} + + +/* This is called when we realise we can't make a connection to the pin and + must undo anything we did in CheckConnect - override to release QIs done */ + +HRESULT +CBasePin::BreakConnect() +{ + return NOERROR; +} + + +/* Called normally by an output pin on an input pin to try and establish a + connection. +*/ + +STDMETHODIMP +CBasePin::ReceiveConnection( + IPin * pConnector, // this is the pin who we will connect to + const AM_MEDIA_TYPE *pmt // this is the media type we will exchange +) +{ + CheckPointer(pConnector,E_POINTER); + CheckPointer(pmt,E_POINTER); + ValidateReadPtr(pConnector,sizeof(IPin)); + ValidateReadPtr(pmt,sizeof(AM_MEDIA_TYPE)); + CAutoLock cObjectLock(m_pLock); + + /* Are we already connected */ + if (m_Connected) { + return VFW_E_ALREADY_CONNECTED; + } + + /* See if the filter is active */ + if (!IsStopped() && !m_bCanReconnectWhenActive) { + return VFW_E_NOT_STOPPED; + } + + HRESULT hr = CheckConnect(pConnector); + if (FAILED(hr)) { + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + + return hr; + } + + /* Ask derived class if this media type is ok */ + + CMediaType * pcmt = (CMediaType*) pmt; + hr = CheckMediaType(pcmt); + if (hr != NOERROR) { + // no -we don't support this media type + + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + // return a specific media type error if there is one + // or map a general failure code to something more helpful + // (in particular S_FALSE gets changed to an error code) + if (SUCCEEDED(hr) || + (hr == E_FAIL) || + (hr == E_INVALIDARG)) { + hr = VFW_E_TYPE_NOT_ACCEPTED; + } + + + return hr; + } + + /* Complete the connection */ + + m_Connected = pConnector; + m_Connected->AddRef(); + hr = SetMediaType(pcmt); + if (SUCCEEDED(hr)) { + hr = CompleteConnect(pConnector); + if (SUCCEEDED(hr)) { + + + return NOERROR; + } + } + + DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Failed to set the media type or failed to complete the connection."))); + m_Connected->Release(); + m_Connected = NULL; + + // Since the procedure is already returning an error code, there + // is nothing else this function can do to report the error. + EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) ); + + + return hr; +} + + +/* Called when we want to terminate a pin connection */ + +STDMETHODIMP +CBasePin::Disconnect() +{ + CAutoLock cObjectLock(m_pLock); + + /* See if the filter is active */ + if (!IsStopped()) { + return VFW_E_NOT_STOPPED; + } + + return DisconnectInternal(); +} + +STDMETHODIMP +CBasePin::DisconnectInternal() +{ + ASSERT(CritCheckIn(m_pLock)); + + if (m_Connected) { + HRESULT hr = BreakConnect(); + if( FAILED( hr ) ) { + + + // There is usually a bug in the program if BreakConnect() fails. + DbgBreak( "WARNING: BreakConnect() failed in CBasePin::Disconnect()." ); + return hr; + } + + m_Connected->Release(); + m_Connected = NULL; + + + return S_OK; + } else { + // no connection - not an error + + + return S_FALSE; + } +} + + +/* Return an AddRef()'d pointer to the connected pin if there is one */ +STDMETHODIMP +CBasePin::ConnectedTo( + IPin **ppPin +) +{ + CheckPointer(ppPin,E_POINTER); + ValidateReadWritePtr(ppPin,sizeof(IPin *)); + // + // It's pointless to lock here. + // The caller should ensure integrity. + // + + IPin *pPin = m_Connected; + *ppPin = pPin; + if (pPin != NULL) { + pPin->AddRef(); + return S_OK; + } else { + ASSERT(*ppPin == NULL); + return VFW_E_NOT_CONNECTED; + } +} + +/* Return the media type of the connection */ +STDMETHODIMP +CBasePin::ConnectionMediaType( + AM_MEDIA_TYPE *pmt +) +{ + CheckPointer(pmt,E_POINTER); + ValidateReadWritePtr(pmt,sizeof(AM_MEDIA_TYPE)); + CAutoLock cObjectLock(m_pLock); + + /* Copy constructor of m_mt allocates the memory */ + if (IsConnected()) { + CopyMediaType( pmt, &m_mt ); + return S_OK; + } else { + ((CMediaType *)pmt)->InitMediaType(); + return VFW_E_NOT_CONNECTED; + } +} + +/* Return information about the filter we are connect to */ + +STDMETHODIMP +CBasePin::QueryPinInfo( + PIN_INFO * pInfo +) +{ + CheckPointer(pInfo,E_POINTER); + ValidateReadWritePtr(pInfo,sizeof(PIN_INFO)); + + pInfo->pFilter = m_pFilter; + if (m_pFilter) { + m_pFilter->AddRef(); + } + + if (m_pName) { + lstrcpynW(pInfo->achName, m_pName, sizeof(pInfo->achName)/sizeof(WCHAR)); + } else { + pInfo->achName[0] = L'\0'; + } + + pInfo->dir = m_dir; + + return NOERROR; +} + +STDMETHODIMP +CBasePin::QueryDirection( + PIN_DIRECTION * pPinDir +) +{ + CheckPointer(pPinDir,E_POINTER); + ValidateReadWritePtr(pPinDir,sizeof(PIN_DIRECTION)); + + *pPinDir = m_dir; + return NOERROR; +} + +// Default QueryId to return the pin's name +STDMETHODIMP +CBasePin::QueryId( + LPWSTR * Id +) +{ + // We're not going away because someone's got a pointer to us + // so there's no need to lock + + return AMGetWideString(Name(), Id); +} + +/* Does this pin support this media type WARNING this interface function does + not lock the main object as it is meant to be asynchronous by nature - if + the media types you support depend on some internal state that is updated + dynamically then you will need to implement locking in a derived class */ + +STDMETHODIMP +CBasePin::QueryAccept( + const AM_MEDIA_TYPE *pmt +) +{ + CheckPointer(pmt,E_POINTER); + ValidateReadPtr(pmt,sizeof(AM_MEDIA_TYPE)); + + /* The CheckMediaType method is valid to return error codes if the media + type is horrible, an example might be E_INVALIDARG. What we do here + is map all the error codes into either S_OK or S_FALSE regardless */ + + HRESULT hr = CheckMediaType((CMediaType*)pmt); + if (FAILED(hr)) { + return S_FALSE; + } + // note that the only defined success codes should be S_OK and S_FALSE... + return hr; +} + + +/* This can be called to return an enumerator for the pin's list of preferred + media types. An input pin is not obliged to have any preferred formats + although it can do. For example, the window renderer has a preferred type + which describes a video image that matches the current window size. All + output pins should expose at least one preferred format otherwise it is + possible that neither pin has any types and so no connection is possible */ + +STDMETHODIMP +CBasePin::EnumMediaTypes( + IEnumMediaTypes **ppEnum +) +{ + CheckPointer(ppEnum,E_POINTER); + ValidateReadWritePtr(ppEnum,sizeof(IEnumMediaTypes *)); + + /* Create a new ref counted enumerator */ + + *ppEnum = new CEnumMediaTypes(this, + NULL); + + if (*ppEnum == NULL) { + return E_OUTOFMEMORY; + } + + return NOERROR; +} + + + +/* This is a virtual function that returns a media type corresponding with + place iPosition in the list. This base class simply returns an error as + we support no media types by default but derived classes should override */ + +HRESULT CBasePin::GetMediaType(int iPosition, CMediaType *pMediaType) +{ + UNREFERENCED_PARAMETER(iPosition); + UNREFERENCED_PARAMETER(pMediaType); + return E_UNEXPECTED; +} + + +/* This is a virtual function that returns the current media type version. + The base class initialises the media type enumerators with the value 1 + By default we always returns that same value. A Derived class may change + the list of media types available and after doing so it should increment + the version either in a method derived from this, or more simply by just + incrementing the m_TypeVersion base pin variable. The type enumerators + call this when they want to see if their enumerations are out of date */ + +LONG CBasePin::GetMediaTypeVersion() +{ + return m_TypeVersion; +} + + +/* Increment the cookie representing the current media type version */ + +void CBasePin::IncrementTypeVersion() +{ + InterlockedIncrement(&m_TypeVersion); +} + + +/* Called by IMediaFilter implementation when the state changes from Stopped + to either paused or running and in derived classes could do things like + commit memory and grab hardware resource (the default is to do nothing) */ + +HRESULT +CBasePin::Active(void) +{ + return NOERROR; +} + +/* Called by IMediaFilter implementation when the state changes from + to either paused to running and in derived classes could do things like + commit memory and grab hardware resource (the default is to do nothing) */ + +HRESULT +CBasePin::Run(REFERENCE_TIME tStart) +{ + UNREFERENCED_PARAMETER(tStart); + return NOERROR; +} + + +/* Also called by the IMediaFilter implementation when the state changes to + Stopped at which point you should decommit allocators and free hardware + resources you grabbed in the Active call (default is also to do nothing) */ + +HRESULT +CBasePin::Inactive(void) +{ + m_bRunTimeError = FALSE; + return NOERROR; +} + + +// Called when no more data will arrive +STDMETHODIMP +CBasePin::EndOfStream(void) +{ + return S_OK; +} + + +STDMETHODIMP +CBasePin::SetSink(IQualityControl * piqc) +{ + CAutoLock cObjectLock(m_pLock); + if (piqc) ValidateReadPtr(piqc,sizeof(IQualityControl)); + m_pQSink = piqc; + return NOERROR; +} // SetSink + + +STDMETHODIMP +CBasePin::Notify(IBaseFilter * pSender, Quality q) +{ + UNREFERENCED_PARAMETER(q); + UNREFERENCED_PARAMETER(pSender); + DbgBreak("IQualityControl::Notify not over-ridden from CBasePin. (IGNORE is OK)"); + return E_NOTIMPL; +} //Notify + + +// NewSegment notifies of the start/stop/rate applying to the data +// about to be received. Default implementation records data and +// returns S_OK. +// Override this to pass downstream. +STDMETHODIMP +CBasePin::NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate) +{ + m_tStart = tStart; + m_tStop = tStop; + m_dRate = dRate; + + return S_OK; +} + + +//===================================================================== +//===================================================================== +// Implements CBaseOutputPin +//===================================================================== +//===================================================================== + + +CBaseOutputPin::CBaseOutputPin(TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBasePin(pObjectName, pFilter, pLock, phr, pName, PINDIR_OUTPUT), + m_pAllocator(NULL), + m_pInputPin(NULL) +{ + ASSERT(pFilter); +} + +#ifdef UNICODE +CBaseOutputPin::CBaseOutputPin(CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBasePin(pObjectName, pFilter, pLock, phr, pName, PINDIR_OUTPUT), + m_pAllocator(NULL), + m_pInputPin(NULL) +{ + ASSERT(pFilter); +} +#endif + +/* This is called after a media type has been proposed + + Try to complete the connection by agreeing the allocator +*/ +HRESULT +CBaseOutputPin::CompleteConnect(IPin *pReceivePin) +{ + UNREFERENCED_PARAMETER(pReceivePin); + return DecideAllocator(m_pInputPin, &m_pAllocator); +} + + +/* This method is called when the output pin is about to try and connect to + an input pin. It is at this point that you should try and grab any extra + interfaces that you need, in this case IMemInputPin. Because this is + only called if we are not currently connected we do NOT need to call + BreakConnect. This also makes it easier to derive classes from us as + BreakConnect is only called when we actually have to break a connection + (or a partly made connection) and not when we are checking a connection */ + +/* Overriden from CBasePin */ + +HRESULT +CBaseOutputPin::CheckConnect(IPin * pPin) +{ + HRESULT hr = CBasePin::CheckConnect(pPin); + if (FAILED(hr)) { + return hr; + } + + // get an input pin and an allocator interface + hr = pPin->QueryInterface(IID_IMemInputPin, (void **) &m_pInputPin); + if (FAILED(hr)) { + return hr; + } + return NOERROR; +} + + +/* Overriden from CBasePin */ + +HRESULT +CBaseOutputPin::BreakConnect() +{ + /* Release any allocator we hold */ + + if (m_pAllocator) { + // Always decommit the allocator because a downstream filter may or + // may not decommit the connection's allocator. A memory leak could + // occur if the allocator is not decommited when a connection is broken. + HRESULT hr = m_pAllocator->Decommit(); + if( FAILED( hr ) ) { + return hr; + } + + m_pAllocator->Release(); + m_pAllocator = NULL; + } + + /* Release any input pin interface we hold */ + + if (m_pInputPin) { + m_pInputPin->Release(); + m_pInputPin = NULL; + } + return NOERROR; +} + + +/* This is called when the input pin didn't give us a valid allocator */ + +HRESULT +CBaseOutputPin::InitAllocator(IMemAllocator **ppAlloc) +{ + return CreateMemoryAllocator(ppAlloc); +} + + +/* Decide on an allocator, override this if you want to use your own allocator + Override DecideBufferSize to call SetProperties. If the input pin fails + the GetAllocator call then this will construct a CMemAllocator and call + DecideBufferSize on that, and if that fails then we are completely hosed. + If the you succeed the DecideBufferSize call, we will notify the input + pin of the selected allocator. NOTE this is called during Connect() which + therefore looks after grabbing and locking the object's critical section */ + +// We query the input pin for its requested properties and pass this to +// DecideBufferSize to allow it to fulfill requests that it is happy +// with (eg most people don't care about alignment and are thus happy to +// use the downstream pin's alignment request). + +HRESULT +CBaseOutputPin::DecideAllocator(IMemInputPin *pPin, IMemAllocator **ppAlloc) +{ + HRESULT hr = NOERROR; + *ppAlloc = NULL; + + // get downstream prop request + // the derived class may modify this in DecideBufferSize, but + // we assume that he will consistently modify it the same way, + // so we only get it once + ALLOCATOR_PROPERTIES prop; + ZeroMemory(&prop, sizeof(prop)); + + // whatever he returns, we assume prop is either all zeros + // or he has filled it out. + pPin->GetAllocatorRequirements(&prop); + + // if he doesn't care about alignment, then set it to 1 + if (prop.cbAlign == 0) { + prop.cbAlign = 1; + } + + /* Try the allocator provided by the input pin */ + + hr = pPin->GetAllocator(ppAlloc); + if (SUCCEEDED(hr)) { + + hr = DecideBufferSize(*ppAlloc, &prop); + if (SUCCEEDED(hr)) { + hr = pPin->NotifyAllocator(*ppAlloc, FALSE); + if (SUCCEEDED(hr)) { + return NOERROR; + } + } + } + + /* If the GetAllocator failed we may not have an interface */ + + if (*ppAlloc) { + (*ppAlloc)->Release(); + *ppAlloc = NULL; + } + + /* Try the output pin's allocator by the same method */ + + hr = InitAllocator(ppAlloc); + if (SUCCEEDED(hr)) { + + // note - the properties passed here are in the same + // structure as above and may have been modified by + // the previous call to DecideBufferSize + hr = DecideBufferSize(*ppAlloc, &prop); + if (SUCCEEDED(hr)) { + hr = pPin->NotifyAllocator(*ppAlloc, FALSE); + if (SUCCEEDED(hr)) { + return NOERROR; + } + } + } + + /* Likewise we may not have an interface to release */ + + if (*ppAlloc) { + (*ppAlloc)->Release(); + *ppAlloc = NULL; + } + return hr; +} + + +/* This returns an empty sample buffer from the allocator WARNING the same + dangers and restrictions apply here as described below for Deliver() */ + +HRESULT +CBaseOutputPin::GetDeliveryBuffer(IMediaSample ** ppSample, + REFERENCE_TIME * pStartTime, + REFERENCE_TIME * pEndTime, + DWORD dwFlags) +{ + if (m_pAllocator != NULL) { + return m_pAllocator->GetBuffer(ppSample,pStartTime,pEndTime,dwFlags); + } else { + return E_NOINTERFACE; + } +} + + +/* Deliver a filled-in sample to the connected input pin. NOTE the object must + have locked itself before calling us otherwise we may get halfway through + executing this method only to find the filter graph has got in and + disconnected us from the input pin. If the filter has no worker threads + then the lock is best applied on Receive(), otherwise it should be done + when the worker thread is ready to deliver. There is a wee snag to worker + threads that this shows up. The worker thread must lock the object when + it is ready to deliver a sample, but it may have to wait until a state + change has completed, but that may never complete because the state change + is waiting for the worker thread to complete. The way to handle this is for + the state change code to grab the critical section, then set an abort event + for the worker thread, then release the critical section and wait for the + worker thread to see the event we set and then signal that it has finished + (with another event). At which point the state change code can complete */ + +// note (if you've still got any breath left after reading that) that you +// need to release the sample yourself after this call. if the connected +// input pin needs to hold onto the sample beyond the call, it will addref +// the sample itself. + +// of course you must release this one and call GetDeliveryBuffer for the +// next. You cannot reuse it directly. + +HRESULT +CBaseOutputPin::Deliver(IMediaSample * pSample) +{ + if (m_pInputPin == NULL) { + return VFW_E_NOT_CONNECTED; + } + + + return m_pInputPin->Receive(pSample); +} + + +// called from elsewhere in our filter to pass EOS downstream to +// our connected input pin +HRESULT +CBaseOutputPin::DeliverEndOfStream(void) +{ + // remember this is on IPin not IMemInputPin + if (m_Connected == NULL) { + return VFW_E_NOT_CONNECTED; + } + return m_Connected->EndOfStream(); +} + + +/* Commit the allocator's memory, this is called through IMediaFilter + which is responsible for locking the object before calling us */ + +HRESULT +CBaseOutputPin::Active(void) +{ + if (m_pAllocator == NULL) { + return VFW_E_NO_ALLOCATOR; + } + return m_pAllocator->Commit(); +} + + +/* Free up or unprepare allocator's memory, this is called through + IMediaFilter which is responsible for locking the object first */ + +HRESULT +CBaseOutputPin::Inactive(void) +{ + m_bRunTimeError = FALSE; + if (m_pAllocator == NULL) { + return VFW_E_NO_ALLOCATOR; + } + return m_pAllocator->Decommit(); +} + +// we have a default handling of EndOfStream which is to return +// an error, since this should be called on input pins only +STDMETHODIMP +CBaseOutputPin::EndOfStream(void) +{ + return E_UNEXPECTED; +} + + +// BeginFlush should be called on input pins only +STDMETHODIMP +CBaseOutputPin::BeginFlush(void) +{ + return E_UNEXPECTED; +} + +// EndFlush should be called on input pins only +STDMETHODIMP +CBaseOutputPin::EndFlush(void) +{ + return E_UNEXPECTED; +} + +// call BeginFlush on the connected input pin +HRESULT +CBaseOutputPin::DeliverBeginFlush(void) +{ + // remember this is on IPin not IMemInputPin + if (m_Connected == NULL) { + return VFW_E_NOT_CONNECTED; + } + return m_Connected->BeginFlush(); +} + +// call EndFlush on the connected input pin +HRESULT +CBaseOutputPin::DeliverEndFlush(void) +{ + // remember this is on IPin not IMemInputPin + if (m_Connected == NULL) { + return VFW_E_NOT_CONNECTED; + } + return m_Connected->EndFlush(); +} +// deliver NewSegment to connected pin +HRESULT +CBaseOutputPin::DeliverNewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate) +{ + if (m_Connected == NULL) { + return VFW_E_NOT_CONNECTED; + } + return m_Connected->NewSegment(tStart, tStop, dRate); +} + + +//===================================================================== +//===================================================================== +// Implements CBaseInputPin +//===================================================================== +//===================================================================== + + +/* Constructor creates a default allocator object */ + +CBaseInputPin::CBaseInputPin(TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pPinName) : + CBasePin(pObjectName, pFilter, pLock, phr, pPinName, PINDIR_INPUT), + m_pAllocator(NULL), + m_bReadOnly(FALSE), + m_bFlushing(FALSE) +{ + ZeroMemory(&m_SampleProps, sizeof(m_SampleProps)); +} + +#ifdef UNICODE +CBaseInputPin::CBaseInputPin(CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pPinName) : + CBasePin(pObjectName, pFilter, pLock, phr, pPinName, PINDIR_INPUT), + m_pAllocator(NULL), + m_bReadOnly(FALSE), + m_bFlushing(FALSE) +{ + ZeroMemory(&m_SampleProps, sizeof(m_SampleProps)); +} +#endif + +/* Destructor releases it's reference count on the default allocator */ + +CBaseInputPin::~CBaseInputPin() +{ + if (m_pAllocator != NULL) { + m_pAllocator->Release(); + m_pAllocator = NULL; + } +} + + +// override this to publicise our interfaces +STDMETHODIMP +CBaseInputPin::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + /* Do we know about this interface */ + + if (riid == IID_IMemInputPin) { + return GetInterface((IMemInputPin *) this, ppv); + } else { + return CBasePin::NonDelegatingQueryInterface(riid, ppv); + } +} + + +/* Return the allocator interface that this input pin would like the output + pin to use. NOTE subsequent calls to GetAllocator should all return an + interface onto the SAME object so we create one object at the start + + Note: + The allocator is Release()'d on disconnect and replaced on + NotifyAllocator(). + + Override this to provide your own allocator. +*/ + +STDMETHODIMP +CBaseInputPin::GetAllocator( + IMemAllocator **ppAllocator) +{ + CheckPointer(ppAllocator,E_POINTER); + ValidateReadWritePtr(ppAllocator,sizeof(IMemAllocator *)); + CAutoLock cObjectLock(m_pLock); + + if (m_pAllocator == NULL) { + HRESULT hr = CreateMemoryAllocator(&m_pAllocator); + if (FAILED(hr)) { + return hr; + } + } + ASSERT(m_pAllocator != NULL); + *ppAllocator = m_pAllocator; + m_pAllocator->AddRef(); + return NOERROR; +} + + +/* Tell the input pin which allocator the output pin is actually going to use + Override this if you care - NOTE the locking we do both here and also in + GetAllocator is unnecessary but derived classes that do something useful + will undoubtedly have to lock the object so this might help remind people */ + +STDMETHODIMP +CBaseInputPin::NotifyAllocator( + IMemAllocator * pAllocator, + BOOL bReadOnly) +{ + CheckPointer(pAllocator,E_POINTER); + ValidateReadPtr(pAllocator,sizeof(IMemAllocator)); + CAutoLock cObjectLock(m_pLock); + + IMemAllocator *pOldAllocator = m_pAllocator; + pAllocator->AddRef(); + m_pAllocator = pAllocator; + + if (pOldAllocator != NULL) { + pOldAllocator->Release(); + } + + // the readonly flag indicates whether samples from this allocator should + // be regarded as readonly - if true, then inplace transforms will not be + // allowed. + m_bReadOnly = (BYTE)bReadOnly; + return NOERROR; +} + + +HRESULT +CBaseInputPin::BreakConnect() +{ + /* We don't need our allocator any more */ + if (m_pAllocator) { + // Always decommit the allocator because a downstream filter may or + // may not decommit the connection's allocator. A memory leak could + // occur if the allocator is not decommited when a pin is disconnected. + HRESULT hr = m_pAllocator->Decommit(); + if( FAILED( hr ) ) { + return hr; + } + + m_pAllocator->Release(); + m_pAllocator = NULL; + } + + return S_OK; +} + + +/* Do something with this media sample - this base class checks to see if the + format has changed with this media sample and if so checks that the filter + will accept it, generating a run time error if not. Once we have raised a + run time error we set a flag so that no more samples will be accepted + + It is important that any filter should override this method and implement + synchronization so that samples are not processed when the pin is + disconnected etc +*/ + +STDMETHODIMP +CBaseInputPin::Receive(IMediaSample *pSample) +{ + CheckPointer(pSample,E_POINTER); + ValidateReadPtr(pSample,sizeof(IMediaSample)); + ASSERT(pSample); + + HRESULT hr = CheckStreaming(); + if (S_OK != hr) { + return hr; + } + + + + /* Check for IMediaSample2 */ + IMediaSample2 *pSample2; + if (SUCCEEDED(pSample->QueryInterface(IID_IMediaSample2, (void **)&pSample2))) { + hr = pSample2->GetProperties(sizeof(m_SampleProps), (PBYTE)&m_SampleProps); + pSample2->Release(); + if (FAILED(hr)) { + return hr; + } + } else { + /* Get the properties the hard way */ + m_SampleProps.cbData = sizeof(m_SampleProps); + m_SampleProps.dwTypeSpecificFlags = 0; + m_SampleProps.dwStreamId = AM_STREAM_MEDIA; + m_SampleProps.dwSampleFlags = 0; + if (S_OK == pSample->IsDiscontinuity()) { + m_SampleProps.dwSampleFlags |= AM_SAMPLE_DATADISCONTINUITY; + } + if (S_OK == pSample->IsPreroll()) { + m_SampleProps.dwSampleFlags |= AM_SAMPLE_PREROLL; + } + if (S_OK == pSample->IsSyncPoint()) { + m_SampleProps.dwSampleFlags |= AM_SAMPLE_SPLICEPOINT; + } + if (SUCCEEDED(pSample->GetTime(&m_SampleProps.tStart, + &m_SampleProps.tStop))) { + m_SampleProps.dwSampleFlags |= AM_SAMPLE_TIMEVALID | + AM_SAMPLE_STOPVALID; + } + if (S_OK == pSample->GetMediaType(&m_SampleProps.pMediaType)) { + m_SampleProps.dwSampleFlags |= AM_SAMPLE_TYPECHANGED; + } + pSample->GetPointer(&m_SampleProps.pbBuffer); + m_SampleProps.lActual = pSample->GetActualDataLength(); + m_SampleProps.cbBuffer = pSample->GetSize(); + } + + /* Has the format changed in this sample */ + + if (!(m_SampleProps.dwSampleFlags & AM_SAMPLE_TYPECHANGED)) { + return NOERROR; + } + + /* Check the derived class accepts this format */ + /* This shouldn't fail as the source must call QueryAccept first */ + + hr = CheckMediaType((CMediaType *)m_SampleProps.pMediaType); + + if (hr == NOERROR) { + return NOERROR; + } + + /* Raise a runtime error if we fail the media type */ + + m_bRunTimeError = TRUE; + EndOfStream(); + m_pFilter->NotifyEvent(EC_ERRORABORT,VFW_E_TYPE_NOT_ACCEPTED,0); + return VFW_E_INVALIDMEDIATYPE; +} + + +/* Receive multiple samples */ +STDMETHODIMP +CBaseInputPin::ReceiveMultiple ( + IMediaSample **pSamples, + long nSamples, + long *nSamplesProcessed) +{ + CheckPointer(pSamples,E_POINTER); + ValidateReadPtr(pSamples,nSamples * sizeof(IMediaSample *)); + + HRESULT hr = S_OK; + *nSamplesProcessed = 0; + while (nSamples-- > 0) { + hr = Receive(pSamples[*nSamplesProcessed]); + + /* S_FALSE means don't send any more */ + if (hr != S_OK) { + break; + } + (*nSamplesProcessed)++; + } + return hr; +} + +/* See if Receive() might block */ +STDMETHODIMP +CBaseInputPin::ReceiveCanBlock() +{ + /* Ask all the output pins if they block + If there are no output pin assume we do block + */ + int cPins = m_pFilter->GetPinCount(); + int cOutputPins = 0; + for (int c = 0; c < cPins; c++) { + CBasePin *pPin = m_pFilter->GetPin(c); + PIN_DIRECTION pd; + HRESULT hr = pPin->QueryDirection(&pd); + if (FAILED(hr)) { + return hr; + } + + if (pd == PINDIR_OUTPUT) { + + IPin *pConnected; + hr = pPin->ConnectedTo(&pConnected); + if (SUCCEEDED(hr)) { + ASSERT(pConnected != NULL); + cOutputPins++; + IMemInputPin *pInputPin; + hr = pConnected->QueryInterface( + IID_IMemInputPin, + (void **)&pInputPin); + pConnected->Release(); + if (SUCCEEDED(hr)) { + hr = pInputPin->ReceiveCanBlock(); + pInputPin->Release(); + if (hr != S_FALSE) { + return S_OK; + } + } else { + /* There's a transport we don't understand here */ + return S_OK; + } + } + } + } + return cOutputPins == 0 ? S_OK : S_FALSE; +} + +// Default handling for BeginFlush - call at the beginning +// of your implementation (makes sure that all Receive calls +// fail). After calling this, you need to free any queued data +// and then call downstream. +STDMETHODIMP +CBaseInputPin::BeginFlush(void) +{ + // BeginFlush is NOT synchronized with streaming but is part of + // a control action - hence we synchronize with the filter + CAutoLock lck(m_pLock); + + // if we are already in mid-flush, this is probably a mistake + // though not harmful - try to pick it up for now so I can think about it + ASSERT(!m_bFlushing); + + // first thing to do is ensure that no further Receive calls succeed + m_bFlushing = TRUE; + + // now discard any data and call downstream - must do that + // in derived classes + return S_OK; +} + +// default handling for EndFlush - call at end of your implementation +// - before calling this, ensure that there is no queued data and no thread +// pushing any more without a further receive, then call downstream, +// then call this method to clear the m_bFlushing flag and re-enable +// receives +STDMETHODIMP +CBaseInputPin::EndFlush(void) +{ + // Endlush is NOT synchronized with streaming but is part of + // a control action - hence we synchronize with the filter + CAutoLock lck(m_pLock); + + // almost certainly a mistake if we are not in mid-flush + ASSERT(m_bFlushing); + + // before calling, sync with pushing thread and ensure + // no more data is going downstream, then call EndFlush on + // downstream pins. + + // now re-enable Receives + m_bFlushing = FALSE; + + // No more errors + m_bRunTimeError = FALSE; + + return S_OK; +} + + +STDMETHODIMP +CBaseInputPin::Notify(IBaseFilter * pSender, Quality q) +{ + UNREFERENCED_PARAMETER(q); + CheckPointer(pSender,E_POINTER); + ValidateReadPtr(pSender,sizeof(IBaseFilter)); + DbgBreak("IQuality::Notify called on an input pin"); + return NOERROR; +} // Notify + +/* Free up or unprepare allocator's memory, this is called through + IMediaFilter which is responsible for locking the object first */ + +HRESULT +CBaseInputPin::Inactive(void) +{ + m_bRunTimeError = FALSE; + if (m_pAllocator == NULL) { + return VFW_E_NO_ALLOCATOR; + } + + m_bFlushing = FALSE; + + return m_pAllocator->Decommit(); +} + +// what requirements do we have of the allocator - override if you want +// to support other people's allocators but need a specific alignment +// or prefix. +STDMETHODIMP +CBaseInputPin::GetAllocatorRequirements(ALLOCATOR_PROPERTIES*pProps) +{ + UNREFERENCED_PARAMETER(pProps); + return E_NOTIMPL; +} + +// Check if it's OK to process data +// +HRESULT +CBaseInputPin::CheckStreaming() +{ + // Shouldn't be able to get any data if we're not connected! + ASSERT(IsConnected()); + + // Don't process stuff in Stopped state + if (IsStopped()) { + return VFW_E_WRONG_STATE; + } + if (m_bFlushing) { + return S_FALSE; + } + if (m_bRunTimeError) { + return VFW_E_RUNTIME_ERROR; + } + return S_OK; +} + +// Pass on the Quality notification q to +// a. Our QualityControl sink (if we have one) or else +// b. to our upstream filter +// and if that doesn't work, throw it away with a bad return code +HRESULT +CBaseInputPin::PassNotify(Quality& q) +{ + // We pass the message on, which means that we find the quality sink + // for our input pin and send it there + + DbgLog((LOG_TRACE,3,TEXT("Passing Quality notification through transform"))); + if (m_pQSink!=NULL) { + return m_pQSink->Notify(m_pFilter, q); + } else { + // no sink set, so pass it upstream + HRESULT hr; + IQualityControl * pIQC; + + hr = VFW_E_NOT_FOUND; // default + if (m_Connected) { + m_Connected->QueryInterface(IID_IQualityControl, (void**)&pIQC); + + if (pIQC!=NULL) { + hr = pIQC->Notify(m_pFilter, q); + pIQC->Release(); + } + } + return hr; + } + +} // PassNotify + +//===================================================================== +//===================================================================== +// Memory allocation class, implements CMediaSample +//===================================================================== +//===================================================================== + + +/* NOTE The implementation of this class calls the CUnknown constructor with + a NULL outer unknown pointer. This has the effect of making us a self + contained class, ie any QueryInterface, AddRef or Release calls will be + routed to the class's NonDelegatingUnknown methods. You will typically + find that the classes that do this then override one or more of these + virtual functions to provide more specialised behaviour. A good example + of this is where a class wants to keep the QueryInterface internal but + still wants it's lifetime controlled by the external object */ + +/* The last two parameters have default values of NULL and zero */ + +CMediaSample::CMediaSample(TCHAR *pName, + CBaseAllocator *pAllocator, + HRESULT *phr, + LPBYTE pBuffer, + LONG length) : + m_pBuffer(pBuffer), // Initialise the buffer + m_cbBuffer(length), // And it's length + m_lActual(length), // By default, actual = length + m_pMediaType(NULL), // No media type change + m_dwFlags(0), // Nothing set + m_cRef(0), // 0 ref count + m_dwTypeSpecificFlags(0), // Type specific flags + m_dwStreamId(AM_STREAM_MEDIA), // Stream id + m_pAllocator(pAllocator) // Allocator +{ + + /* We must have an owner and it must also be derived from class + CBaseAllocator BUT we do not hold a reference count on it */ + + ASSERT(pAllocator); +} + +#ifdef UNICODE +CMediaSample::CMediaSample(CHAR *pName, + CBaseAllocator *pAllocator, + HRESULT *phr, + LPBYTE pBuffer, + LONG length) : + m_pBuffer(pBuffer), // Initialise the buffer + m_cbBuffer(length), // And it's length + m_lActual(length), // By default, actual = length + m_pMediaType(NULL), // No media type change + m_dwFlags(0), // Nothing set + m_cRef(0), // 0 ref count + m_dwTypeSpecificFlags(0), // Type specific flags + m_dwStreamId(AM_STREAM_MEDIA), // Stream id + m_pAllocator(pAllocator) // Allocator +{ + + /* We must have an owner and it must also be derived from class + CBaseAllocator BUT we do not hold a reference count on it */ + + ASSERT(pAllocator); +} +#endif + +/* Destructor deletes the media type memory */ + +CMediaSample::~CMediaSample() +{ + + if (m_pMediaType) { + DeleteMediaType(m_pMediaType); + } +} + +/* Override this to publicise our interfaces */ + +STDMETHODIMP +CMediaSample::QueryInterface(REFIID riid, void **ppv) +{ + if (riid == IID_IMediaSample || + riid == IID_IMediaSample2 || + riid == IID_IUnknown) { + return GetInterface((IMediaSample *) this, ppv); + } else { + return E_NOINTERFACE; + } +} + +STDMETHODIMP_(ULONG) +CMediaSample::AddRef() +{ + return InterlockedIncrement(&m_cRef); +} + + +// -- CMediaSample lifetimes -- +// +// On final release of this sample buffer it is not deleted but +// returned to the freelist of the owning memory allocator +// +// The allocator may be waiting for the last buffer to be placed on the free +// list in order to decommit all the memory, so the ReleaseBuffer() call may +// result in this sample being deleted. We also need to hold a refcount on +// the allocator to stop that going away until we have finished with this. +// However, we cannot release the allocator before the ReleaseBuffer, as the +// release may cause us to be deleted. Similarly we can't do it afterwards. +// +// Thus we must leave it to the allocator to hold an addref on our behalf. +// When he issues us in GetBuffer, he addref's himself. When ReleaseBuffer +// is called, he releases himself, possibly causing us and him to be deleted. + + +STDMETHODIMP_(ULONG) +CMediaSample::Release() +{ + /* Decrement our own private reference count */ + LONG lRef; + if (m_cRef == 1) { + lRef = 0; + m_cRef = 0; + } else { + lRef = InterlockedDecrement(&m_cRef); + } + ASSERT(lRef >= 0); + + DbgLog((LOG_MEMORY,3,TEXT(" Unknown %X ref-- = %d"), + this, m_cRef)); + + /* Did we release our final reference count */ + if (lRef == 0) { + /* Free all resources */ + if (m_dwFlags & Sample_TypeChanged) { + SetMediaType(NULL); + } + ASSERT(m_pMediaType == NULL); + m_dwFlags = 0; + m_dwTypeSpecificFlags = 0; + m_dwStreamId = AM_STREAM_MEDIA; + + /* This may cause us to be deleted */ + // Our refcount is reliably 0 thus no-one will mess with us + m_pAllocator->ReleaseBuffer(this); + } + return (ULONG)lRef; +} + + +// set the buffer pointer and length. Used by allocators that +// want variable sized pointers or pointers into already-read data. +// This is only available through a CMediaSample* not an IMediaSample* +// and so cannot be changed by clients. +HRESULT +CMediaSample::SetPointer(BYTE * ptr, LONG cBytes) +{ + m_pBuffer = ptr; // new buffer area (could be null) + m_cbBuffer = cBytes; // length of buffer + m_lActual = cBytes; // length of data in buffer (assume full) + + return S_OK; +} + + +// get me a read/write pointer to this buffer's memory. I will actually +// want to use sizeUsed bytes. +STDMETHODIMP +CMediaSample::GetPointer(BYTE ** ppBuffer) +{ + ValidateReadWritePtr(ppBuffer,sizeof(BYTE *)); + + // creator must have set pointer either during + // constructor or by SetPointer + ASSERT(m_pBuffer); + + *ppBuffer = m_pBuffer; + return NOERROR; +} + + +// return the size in bytes of this buffer +STDMETHODIMP_(LONG) +CMediaSample::GetSize(void) +{ + return m_cbBuffer; +} + + +// get the stream time at which this sample should start and finish. +STDMETHODIMP +CMediaSample::GetTime( + REFERENCE_TIME * pTimeStart, // put time here + REFERENCE_TIME * pTimeEnd +) +{ + ValidateReadWritePtr(pTimeStart,sizeof(REFERENCE_TIME)); + ValidateReadWritePtr(pTimeEnd,sizeof(REFERENCE_TIME)); + + if (!(m_dwFlags & Sample_StopValid)) { + if (!(m_dwFlags & Sample_TimeValid)) { + return VFW_E_SAMPLE_TIME_NOT_SET; + } else { + *pTimeStart = m_Start; + + // Make sure old stuff works + *pTimeEnd = m_Start + 1; + return VFW_S_NO_STOP_TIME; + } + } + + *pTimeStart = m_Start; + *pTimeEnd = m_End; + return NOERROR; +} + + +// Set the stream time at which this sample should start and finish. +// NULL pointers means the time is reset +STDMETHODIMP +CMediaSample::SetTime( + REFERENCE_TIME * pTimeStart, + REFERENCE_TIME * pTimeEnd +) +{ + if (pTimeStart == NULL) { + ASSERT(pTimeEnd == NULL); + m_dwFlags &= ~(Sample_TimeValid | Sample_StopValid); + } else { + if (pTimeEnd == NULL) { + m_Start = *pTimeStart; + m_dwFlags |= Sample_TimeValid; + m_dwFlags &= ~Sample_StopValid; + } else { + ValidateReadPtr(pTimeStart,sizeof(REFERENCE_TIME)); + ValidateReadPtr(pTimeEnd,sizeof(REFERENCE_TIME)); + ASSERT(*pTimeEnd >= *pTimeStart); + + m_Start = *pTimeStart; + m_End = *pTimeEnd; + m_dwFlags |= Sample_TimeValid | Sample_StopValid; + } + } + return NOERROR; +} + + +// get the media times (eg bytes) for this sample +STDMETHODIMP +CMediaSample::GetMediaTime( + LONGLONG * pTimeStart, + LONGLONG * pTimeEnd +) +{ + ValidateReadWritePtr(pTimeStart,sizeof(LONGLONG)); + ValidateReadWritePtr(pTimeEnd,sizeof(LONGLONG)); + + if (!(m_dwFlags & Sample_MediaTimeValid)) { + return VFW_E_MEDIA_TIME_NOT_SET; + } + + *pTimeStart = m_MediaStart; + *pTimeEnd = (m_MediaStart + m_MediaEnd); + return NOERROR; +} + + +// Set the media times for this sample +STDMETHODIMP +CMediaSample::SetMediaTime( + LONGLONG * pTimeStart, + LONGLONG * pTimeEnd +) +{ + if (pTimeStart == NULL) { + ASSERT(pTimeEnd == NULL); + m_dwFlags &= ~Sample_MediaTimeValid; + } else { + ValidateReadPtr(pTimeStart,sizeof(LONGLONG)); + ValidateReadPtr(pTimeEnd,sizeof(LONGLONG)); + ASSERT(*pTimeEnd >= *pTimeStart); + + m_MediaStart = *pTimeStart; + m_MediaEnd = (LONG)(*pTimeEnd - *pTimeStart); + m_dwFlags |= Sample_MediaTimeValid; + } + return NOERROR; +} + + +STDMETHODIMP +CMediaSample::IsSyncPoint(void) +{ + if (m_dwFlags & Sample_SyncPoint) { + return S_OK; + } else { + return S_FALSE; + } +} + + +STDMETHODIMP +CMediaSample::SetSyncPoint(BOOL bIsSyncPoint) +{ + if (bIsSyncPoint) { + m_dwFlags |= Sample_SyncPoint; + } else { + m_dwFlags &= ~Sample_SyncPoint; + } + return NOERROR; +} + +// returns S_OK if there is a discontinuity in the data (this same is +// not a continuation of the previous stream of data +// - there has been a seek). +STDMETHODIMP +CMediaSample::IsDiscontinuity(void) +{ + if (m_dwFlags & Sample_Discontinuity) { + return S_OK; + } else { + return S_FALSE; + } +} + +// set the discontinuity property - TRUE if this sample is not a +// continuation, but a new sample after a seek. +STDMETHODIMP +CMediaSample::SetDiscontinuity(BOOL bDiscont) +{ + // should be TRUE or FALSE + if (bDiscont) { + m_dwFlags |= Sample_Discontinuity; + } else { + m_dwFlags &= ~Sample_Discontinuity; + } + return S_OK; +} + +STDMETHODIMP +CMediaSample::IsPreroll(void) +{ + if (m_dwFlags & Sample_Preroll) { + return S_OK; + } else { + return S_FALSE; + } +} + + +STDMETHODIMP +CMediaSample::SetPreroll(BOOL bIsPreroll) +{ + if (bIsPreroll) { + m_dwFlags |= Sample_Preroll; + } else { + m_dwFlags &= ~Sample_Preroll; + } + return NOERROR; +} + +STDMETHODIMP_(LONG) +CMediaSample::GetActualDataLength(void) +{ + return m_lActual; +} + + +STDMETHODIMP +CMediaSample::SetActualDataLength(LONG lActual) +{ + if (lActual > m_cbBuffer) { + ASSERT(lActual <= GetSize()); + return VFW_E_BUFFER_OVERFLOW; + } + m_lActual = lActual; + return NOERROR; +} + + +/* These allow for limited format changes in band */ + +STDMETHODIMP +CMediaSample::GetMediaType(AM_MEDIA_TYPE **ppMediaType) +{ + ValidateReadWritePtr(ppMediaType,sizeof(AM_MEDIA_TYPE *)); + ASSERT(ppMediaType); + + /* Do we have a new media type for them */ + + if (!(m_dwFlags & Sample_TypeChanged)) { + ASSERT(m_pMediaType == NULL); + *ppMediaType = NULL; + return S_FALSE; + } + + ASSERT(m_pMediaType); + + /* Create a copy of our media type */ + + *ppMediaType = CreateMediaType(m_pMediaType); + if (*ppMediaType == NULL) { + return E_OUTOFMEMORY; + } + return NOERROR; +} + + +/* Mark this sample as having a different format type */ + +STDMETHODIMP +CMediaSample::SetMediaType(AM_MEDIA_TYPE *pMediaType) +{ + /* Delete the current media type */ + + if (m_pMediaType) { + DeleteMediaType(m_pMediaType); + m_pMediaType = NULL; + } + + /* Mechanism for resetting the format type */ + + if (pMediaType == NULL) { + m_dwFlags &= ~Sample_TypeChanged; + return NOERROR; + } + + ASSERT(pMediaType); + ValidateReadPtr(pMediaType,sizeof(AM_MEDIA_TYPE)); + + /* Take a copy of the media type */ + + m_pMediaType = CreateMediaType(pMediaType); + if (m_pMediaType == NULL) { + m_dwFlags &= ~Sample_TypeChanged; + return E_OUTOFMEMORY; + } + + m_dwFlags |= Sample_TypeChanged; + return NOERROR; +} + +// Set and get properties (IMediaSample2) +STDMETHODIMP CMediaSample::GetProperties( + DWORD cbProperties, + BYTE * pbProperties +) +{ + if (0 != cbProperties) { + CheckPointer(pbProperties, E_POINTER); + // Return generic stuff up to the length + AM_SAMPLE2_PROPERTIES Props; + Props.cbData = (DWORD) (min(cbProperties, sizeof(Props))); + Props.dwSampleFlags = m_dwFlags & ~Sample_MediaTimeValid; + Props.dwTypeSpecificFlags = m_dwTypeSpecificFlags; + Props.pbBuffer = m_pBuffer; + Props.cbBuffer = m_cbBuffer; + Props.lActual = m_lActual; + Props.tStart = m_Start; + Props.tStop = m_End; + Props.dwStreamId = m_dwStreamId; + if (m_dwFlags & AM_SAMPLE_TYPECHANGED) { + Props.pMediaType = m_pMediaType; + } else { + Props.pMediaType = NULL; + } + CopyMemory(pbProperties, &Props, Props.cbData); + } + return S_OK; +} + +#define CONTAINS_FIELD(type, field, offset) \ + ((FIELD_OFFSET(type, field) + sizeof(((type *)0)->field)) <= offset) + +HRESULT CMediaSample::SetProperties( + DWORD cbProperties, + const BYTE * pbProperties +) +{ + + /* Generic properties */ + AM_MEDIA_TYPE *pMediaType = NULL; + + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbData, cbProperties)) { + CheckPointer(pbProperties, E_POINTER); + AM_SAMPLE2_PROPERTIES *pProps = + (AM_SAMPLE2_PROPERTIES *)pbProperties; + + /* Don't use more data than is actually there */ + if (pProps->cbData < cbProperties) { + cbProperties = pProps->cbData; + } + /* We only handle IMediaSample2 */ + if (cbProperties > sizeof(*pProps) || + pProps->cbData > sizeof(*pProps)) { + return E_INVALIDARG; + } + /* Do checks first, the assignments (for backout) */ + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwSampleFlags, cbProperties)) { + /* Check the flags */ + if (pProps->dwSampleFlags & + (~Sample_ValidFlags | Sample_MediaTimeValid)) { + return E_INVALIDARG; + } + /* Check a flag isn't being set for a property + not being provided + */ + if ((pProps->dwSampleFlags & AM_SAMPLE_TIMEVALID) && + !(m_dwFlags & AM_SAMPLE_TIMEVALID) && + !CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStop, cbProperties)) { + return E_INVALIDARG; + } + } + /* NB - can't SET the pointer or size */ + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pbBuffer, cbProperties)) { + + /* Check pbBuffer */ + if (pProps->pbBuffer != 0 && pProps->pbBuffer != m_pBuffer) { + return E_INVALIDARG; + } + } + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbBuffer, cbProperties)) { + + /* Check cbBuffer */ + if (pProps->cbBuffer != 0 && pProps->cbBuffer != m_cbBuffer) { + return E_INVALIDARG; + } + } + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbBuffer, cbProperties) && + CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, lActual, cbProperties)) { + + /* Check lActual */ + if (pProps->cbBuffer < pProps->lActual) { + return E_INVALIDARG; + } + } + + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pMediaType, cbProperties)) { + + /* Check pMediaType */ + if (pProps->dwSampleFlags & AM_SAMPLE_TYPECHANGED) { + CheckPointer(pProps->pMediaType, E_POINTER); + pMediaType = CreateMediaType(pProps->pMediaType); + if (pMediaType == NULL) { + return E_OUTOFMEMORY; + } + } + } + + /* Now do the assignments */ + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwStreamId, cbProperties)) { + m_dwStreamId = pProps->dwStreamId; + } + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwSampleFlags, cbProperties)) { + /* Set the flags */ + m_dwFlags = pProps->dwSampleFlags | + (m_dwFlags & Sample_MediaTimeValid); + m_dwTypeSpecificFlags = pProps->dwTypeSpecificFlags; + } else { + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwTypeSpecificFlags, cbProperties)) { + m_dwTypeSpecificFlags = pProps->dwTypeSpecificFlags; + } + } + + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, lActual, cbProperties)) { + /* Set lActual */ + m_lActual = pProps->lActual; + } + + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStop, cbProperties)) { + + /* Set the times */ + m_End = pProps->tStop; + } + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStart, cbProperties)) { + + /* Set the times */ + m_Start = pProps->tStart; + } + + if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pMediaType, cbProperties)) { + /* Set pMediaType */ + if (pProps->dwSampleFlags & AM_SAMPLE_TYPECHANGED) { + if (m_pMediaType != NULL) { + DeleteMediaType(m_pMediaType); + } + m_pMediaType = pMediaType; + } + } + + /* Fix up the type changed flag to correctly reflect the current state + If, for instance the input contained no type change but the + output does then if we don't do this we'd lose the + output media type. + */ + if (m_pMediaType) { + m_dwFlags |= Sample_TypeChanged; + } else { + m_dwFlags &= ~Sample_TypeChanged; + } + } + + return S_OK; +} + + +// +// The streaming thread calls IPin::NewSegment(), IPin::EndOfStream(), +// IMemInputPin::Receive() and IMemInputPin::ReceiveMultiple() on the +// connected input pin. The application thread calls Block(). The +// following class members can only be called by the streaming thread. +// +// Deliver() +// DeliverNewSegment() +// StartUsingOutputPin() +// StopUsingOutputPin() +// ChangeOutputFormat() +// ChangeMediaType() +// DynamicReconnect() +// +// The following class members can only be called by the application thread. +// +// Block() +// SynchronousBlockOutputPin() +// AsynchronousBlockOutputPin() +// + +CDynamicOutputPin::CDynamicOutputPin( + TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBaseOutputPin(pObjectName, pFilter, pLock, phr, pName), + m_hStopEvent(NULL), + m_pGraphConfig(NULL), + m_bPinUsesReadOnlyAllocator(FALSE), + m_BlockState(NOT_BLOCKED), + m_hUnblockOutputPinEvent(NULL), + m_hNotifyCallerPinBlockedEvent(NULL), + m_dwBlockCallerThreadID(0), + m_dwNumOutstandingOutputPinUsers(0) +{ + HRESULT hr = Initialize(); + if( FAILED( hr ) ) { + *phr = hr; + return; + } +} + +#ifdef UNICODE +CDynamicOutputPin::CDynamicOutputPin( + CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName) : + CBaseOutputPin(pObjectName, pFilter, pLock, phr, pName), + m_hStopEvent(NULL), + m_pGraphConfig(NULL), + m_bPinUsesReadOnlyAllocator(FALSE), + m_BlockState(NOT_BLOCKED), + m_hUnblockOutputPinEvent(NULL), + m_hNotifyCallerPinBlockedEvent(NULL), + m_dwBlockCallerThreadID(0), + m_dwNumOutstandingOutputPinUsers(0) +{ + HRESULT hr = Initialize(); + if( FAILED( hr ) ) { + *phr = hr; + return; + } +} +#endif + +CDynamicOutputPin::~CDynamicOutputPin() +{ + if(NULL != m_hUnblockOutputPinEvent) { + // This call should not fail because we have access to m_hUnblockOutputPinEvent + // and m_hUnblockOutputPinEvent is a valid event. + EXECUTE_ASSERT(::CloseHandle(m_hUnblockOutputPinEvent)); + } + + if(NULL != m_hNotifyCallerPinBlockedEvent) { + // This call should not fail because we have access to m_hNotifyCallerPinBlockedEvent + // and m_hNotifyCallerPinBlockedEvent is a valid event. + EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent)); + } +} + +HRESULT CDynamicOutputPin::Initialize(void) +{ + m_hUnblockOutputPinEvent = ::CreateEvent( NULL, // The event will have the default security descriptor. + TRUE, // This is a manual reset event. + TRUE, // The event is initially signaled. + NULL ); // The event is not named. + + // CreateEvent() returns NULL if an error occurs. + if(NULL == m_hUnblockOutputPinEvent) { + return AmGetLastErrorToHResult(); + } + + // Set flag to say we can reconnect while streaming. + SetReconnectWhenActive(true); + + return S_OK; +} + +STDMETHODIMP CDynamicOutputPin::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + if(riid == IID_IPinFlowControl) { + return GetInterface(static_cast(this), ppv); + } else { + return CBaseOutputPin::NonDelegatingQueryInterface(riid, ppv); + } +} + +STDMETHODIMP CDynamicOutputPin::Disconnect(void) +{ + CAutoLock cObjectLock(m_pLock); + return DisconnectInternal(); +} + +STDMETHODIMP CDynamicOutputPin::Block(DWORD dwBlockFlags, HANDLE hEvent) +{ + const DWORD VALID_FLAGS = AM_PIN_FLOW_CONTROL_BLOCK; + + // Check for illegal flags. + if(dwBlockFlags & ~VALID_FLAGS) { + return E_INVALIDARG; + } + + // Make sure the event is unsignaled. + if((dwBlockFlags & AM_PIN_FLOW_CONTROL_BLOCK) && (NULL != hEvent)) { + if( !::ResetEvent( hEvent ) ) { + return AmGetLastErrorToHResult(); + } + } + + // No flags are set if we are unblocking the output pin. + if(0 == dwBlockFlags) { + + // This parameter should be NULL because unblock operations are always synchronous. + // There is no need to notify the caller when the event is done. + if(NULL != hEvent) { + return E_INVALIDARG; + } + } + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + HRESULT hr; + + if(dwBlockFlags & AM_PIN_FLOW_CONTROL_BLOCK) { + // IPinFlowControl::Block()'s hEvent parameter is NULL if the block is synchronous. + // If hEvent is not NULL, the block is asynchronous. + if(NULL == hEvent) { + hr = SynchronousBlockOutputPin(); + } else { + hr = AsynchronousBlockOutputPin(hEvent); + } + } else { + hr = UnblockOutputPin(); + } + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + if(FAILED(hr)) { + return hr; + } + + return S_OK; +} + +HRESULT CDynamicOutputPin::SynchronousBlockOutputPin(void) +{ + HANDLE hNotifyCallerPinBlockedEvent = :: CreateEvent( NULL, // The event will have the default security attributes. + FALSE, // This is an automatic reset event. + FALSE, // The event is initially unsignaled. + NULL ); // The event is not named. + + // CreateEvent() returns NULL if an error occurs. + if(NULL == hNotifyCallerPinBlockedEvent) { + return AmGetLastErrorToHResult(); + } + + HRESULT hr = AsynchronousBlockOutputPin(hNotifyCallerPinBlockedEvent); + if(FAILED(hr)) { + // This call should not fail because we have access to hNotifyCallerPinBlockedEvent + // and hNotifyCallerPinBlockedEvent is a valid event. + EXECUTE_ASSERT(::CloseHandle(hNotifyCallerPinBlockedEvent)); + + return hr; + } + + hr = WaitEvent(hNotifyCallerPinBlockedEvent); + + // This call should not fail because we have access to hNotifyCallerPinBlockedEvent + // and hNotifyCallerPinBlockedEvent is a valid event. + EXECUTE_ASSERT(::CloseHandle(hNotifyCallerPinBlockedEvent)); + + if(FAILED(hr)) { + return hr; + } + + return S_OK; +} + +HRESULT CDynamicOutputPin::AsynchronousBlockOutputPin(HANDLE hNotifyCallerPinBlockedEvent) +{ + // This function holds the m_BlockStateLock because it uses + // m_dwBlockCallerThreadID, m_BlockState and + // m_hNotifyCallerPinBlockedEvent. + CAutoLock alBlockStateLock(&m_BlockStateLock); + + if(NOT_BLOCKED != m_BlockState) { + if(m_dwBlockCallerThreadID == ::GetCurrentThreadId()) { + return VFW_E_PIN_ALREADY_BLOCKED_ON_THIS_THREAD; + } else { + return VFW_E_PIN_ALREADY_BLOCKED; + } + } + + BOOL fSuccess = ::DuplicateHandle( ::GetCurrentProcess(), + hNotifyCallerPinBlockedEvent, + ::GetCurrentProcess(), + &m_hNotifyCallerPinBlockedEvent, + EVENT_MODIFY_STATE, + FALSE, + 0 ); + if( !fSuccess ) { + return AmGetLastErrorToHResult(); + } + + m_BlockState = PENDING; + m_dwBlockCallerThreadID = ::GetCurrentThreadId(); + + // The output pin cannot be blocked if the streaming thread is + // calling IPin::NewSegment(), IPin::EndOfStream(), IMemInputPin::Receive() + // or IMemInputPin::ReceiveMultiple() on the connected input pin. Also, it + // cannot be blocked if the streaming thread is calling DynamicReconnect(), + // ChangeMediaType() or ChangeOutputFormat(). + if(!StreamingThreadUsingOutputPin()) { + + // The output pin can be immediately blocked. + BlockOutputPin(); + } + + return S_OK; +} + +void CDynamicOutputPin::BlockOutputPin(void) +{ + // The caller should always hold the m_BlockStateLock because this function + // uses m_BlockState and m_hNotifyCallerPinBlockedEvent. + ASSERT(CritCheckIn(&m_BlockStateLock)); + + // This function should not be called if the streaming thread is modifying + // the connection state or it's passing data downstream. + ASSERT(!StreamingThreadUsingOutputPin()); + + // This should not fail because we successfully created the event + // and we have the security permissions to change it's state. + EXECUTE_ASSERT(::ResetEvent(m_hUnblockOutputPinEvent)); + + // This event should not fail because AsynchronousBlockOutputPin() successfully + // duplicated this handle and we have the appropriate security permissions. + EXECUTE_ASSERT(::SetEvent(m_hNotifyCallerPinBlockedEvent)); + EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent)); + + m_BlockState = BLOCKED; + m_hNotifyCallerPinBlockedEvent = NULL; +} + +HRESULT CDynamicOutputPin::UnblockOutputPin(void) +{ + // UnblockOutputPin() holds the m_BlockStateLock because it + // uses m_BlockState, m_dwBlockCallerThreadID and + // m_hNotifyCallerPinBlockedEvent. + CAutoLock alBlockStateLock(&m_BlockStateLock); + + if(NOT_BLOCKED == m_BlockState) { + return S_FALSE; + } + + // This should not fail because we successfully created the event + // and we have the security permissions to change it's state. + EXECUTE_ASSERT(::SetEvent(m_hUnblockOutputPinEvent)); + + // Cancel the block operation if it's still pending. + if(NULL != m_hNotifyCallerPinBlockedEvent) { + // This event should not fail because AsynchronousBlockOutputPin() successfully + // duplicated this handle and we have the appropriate security permissions. + EXECUTE_ASSERT(::SetEvent(m_hNotifyCallerPinBlockedEvent)); + EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent)); + } + + m_BlockState = NOT_BLOCKED; + m_dwBlockCallerThreadID = 0; + m_hNotifyCallerPinBlockedEvent = NULL; + + return S_OK; +} + +HRESULT CDynamicOutputPin::StartUsingOutputPin(void) +{ + // The caller should not hold m_BlockStateLock. If the caller does, + // a deadlock could occur. + ASSERT(CritCheckOut(&m_BlockStateLock)); + + CAutoLock alBlockStateLock(&m_BlockStateLock); + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + // Are we in the middle of a block operation? + while(BLOCKED == m_BlockState) { + m_BlockStateLock.Unlock(); + + // If this ASSERT fires, a deadlock could occur. The caller should make sure + // that this thread never acquires the Block State lock more than once. + ASSERT(CritCheckOut( &m_BlockStateLock )); + + // WaitForMultipleObjects() returns WAIT_OBJECT_0 if the unblock event + // is fired. It returns WAIT_OBJECT_0 + 1 if the stop event if fired. + // See the Windows SDK documentation for more information on + // WaitForMultipleObjects(). + const DWORD UNBLOCK = WAIT_OBJECT_0; + const DWORD STOP = WAIT_OBJECT_0 + 1; + + HANDLE ahWaitEvents[] = { m_hUnblockOutputPinEvent, m_hStopEvent }; + DWORD dwNumWaitEvents = sizeof(ahWaitEvents)/sizeof(HANDLE); + + DWORD dwReturnValue = ::WaitForMultipleObjects( dwNumWaitEvents, ahWaitEvents, FALSE, INFINITE ); + + m_BlockStateLock.Lock(); + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + switch( dwReturnValue ) { + case UNBLOCK: + break; + + case STOP: + return VFW_E_STATE_CHANGED; + + case WAIT_FAILED: + return AmGetLastErrorToHResult(); + + default: + DbgBreak( "An Unexpected case occured in CDynamicOutputPin::StartUsingOutputPin()." ); + return E_UNEXPECTED; + } + } + + m_dwNumOutstandingOutputPinUsers++; + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + return S_OK; +} + +void CDynamicOutputPin::StopUsingOutputPin(void) +{ + CAutoLock alBlockStateLock(&m_BlockStateLock); + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG + + m_dwNumOutstandingOutputPinUsers--; + + if((m_dwNumOutstandingOutputPinUsers == 0) && (NOT_BLOCKED != m_BlockState)) { + BlockOutputPin(); + } + + #ifdef DEBUG + AssertValid(); + #endif // DEBUG +} + +bool CDynamicOutputPin::StreamingThreadUsingOutputPin(void) +{ + CAutoLock alBlockStateLock(&m_BlockStateLock); + + return (m_dwNumOutstandingOutputPinUsers > 0); +} + +void CDynamicOutputPin::SetConfigInfo(IGraphConfig *pGraphConfig, HANDLE hStopEvent) +{ + // This pointer is not addrefed because filters are not allowed to + // hold references to the filter graph manager. See the documentation for + // IBaseFilter::JoinFilterGraph() in the Direct Show SDK for more information. + m_pGraphConfig = pGraphConfig; + + m_hStopEvent = hStopEvent; +} + +HRESULT CDynamicOutputPin::Active(void) +{ + // Make sure the user initialized the object by calling SetConfigInfo(). + if((NULL == m_hStopEvent) || (NULL == m_pGraphConfig)) { + DbgBreak( ERROR: CDynamicOutputPin::Active() failed because m_pGraphConfig and m_hStopEvent were not initialized. Call SetConfigInfo() to initialize them. ); + return E_FAIL; + } + + // If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo(). + // The ASSERT can also fire if the event if destroyed and then Active() is called. An event + // handle is invalid if 1) the event does not exist or the user does not have the security + // permissions to use the event. + EXECUTE_ASSERT(ResetEvent(m_hStopEvent)); + + return CBaseOutputPin::Active(); +} + +HRESULT CDynamicOutputPin::Inactive(void) +{ + // If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo(). + // The ASSERT can also fire if the event if destroyed and then Active() is called. An event + // handle is invalid if 1) the event does not exist or the user does not have the security + // permissions to use the event. + EXECUTE_ASSERT(SetEvent(m_hStopEvent)); + + return CBaseOutputPin::Inactive(); +} + +HRESULT CDynamicOutputPin::DeliverBeginFlush(void) +{ + // If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo(). + // The ASSERT can also fire if the event if destroyed and then DeliverBeginFlush() is called. + // An event handle is invalid if 1) the event does not exist or the user does not have the security + // permissions to use the event. + EXECUTE_ASSERT(SetEvent(m_hStopEvent)); + + return CBaseOutputPin::DeliverBeginFlush(); +} + +HRESULT CDynamicOutputPin::DeliverEndFlush(void) +{ + // If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo(). + // The ASSERT can also fire if the event if destroyed and then DeliverBeginFlush() is called. + // An event handle is invalid if 1) the event does not exist or the user does not have the security + // permissions to use the event. + EXECUTE_ASSERT(ResetEvent(m_hStopEvent)); + + return CBaseOutputPin::DeliverEndFlush(); +} + + +// ChangeOutputFormat() either dynamicly changes the connection's format type or it dynamicly +// reconnects the output pin. +HRESULT CDynamicOutputPin::ChangeOutputFormat + ( + const AM_MEDIA_TYPE *pmt, + REFERENCE_TIME tSegmentStart, + REFERENCE_TIME tSegmentStop, + double dSegmentRate + ) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + // Callers should always pass a valid media type to ChangeOutputFormat() . + ASSERT(NULL != pmt); + + CMediaType cmt(*pmt); + HRESULT hr = ChangeMediaType(&cmt); + if (FAILED(hr)) { + return hr; + } + + hr = DeliverNewSegment(tSegmentStart, tSegmentStop, dSegmentRate); + if( FAILED( hr ) ) { + return hr; + } + + return S_OK; +} + +HRESULT CDynamicOutputPin::ChangeMediaType(const CMediaType *pmt) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + // This function assumes the filter graph is running. + ASSERT(!IsStopped()); + + if(!IsConnected()) { + return VFW_E_NOT_CONNECTED; + } + + /* First check if the downstream pin will accept a dynamic + format change + */ + QzCComPtr pConnection; + + m_Connected->QueryInterface(IID_IPinConnection, (void **)&pConnection); + if(pConnection != NULL) { + + if(S_OK == pConnection->DynamicQueryAccept(pmt)) { + + HRESULT hr = ChangeMediaTypeHelper(pmt); + if(FAILED(hr)) { + return hr; + } + + return S_OK; + } + } + + /* Can't do the dynamic connection */ + return DynamicReconnect(pmt); +} + +HRESULT CDynamicOutputPin::ChangeMediaTypeHelper(const CMediaType *pmt) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + HRESULT hr = m_Connected->ReceiveConnection(this, pmt); + if(FAILED(hr)) { + return hr; + } + + hr = SetMediaType(pmt); + if(FAILED(hr)) { + return hr; + } + + // Does this pin use the local memory transport? + if(NULL != m_pInputPin) { + // This function assumes that m_pInputPin and m_Connected are + // two different interfaces to the same object. + ASSERT(::IsEqualObject(m_Connected, m_pInputPin)); + + ALLOCATOR_PROPERTIES apInputPinRequirements; + apInputPinRequirements.cbAlign = 0; + apInputPinRequirements.cbBuffer = 0; + apInputPinRequirements.cbPrefix = 0; + apInputPinRequirements.cBuffers = 0; + + m_pInputPin->GetAllocatorRequirements(&apInputPinRequirements); + + // A zero allignment does not make any sense. + if(0 == apInputPinRequirements.cbAlign) { + apInputPinRequirements.cbAlign = 1; + } + + hr = m_pAllocator->Decommit(); + if(FAILED(hr)) { + return hr; + } + + hr = DecideBufferSize(m_pAllocator, &apInputPinRequirements); + if(FAILED(hr)) { + return hr; + } + + hr = m_pAllocator->Commit(); + if(FAILED(hr)) { + return hr; + } + + hr = m_pInputPin->NotifyAllocator(m_pAllocator, m_bPinUsesReadOnlyAllocator); + if(FAILED(hr)) { + return hr; + } + } + + return S_OK; +} + +// this method has to be called from the thread that is pushing data, +// and it's the caller's responsibility to make sure that the thread +// has no outstand samples because they cannot be delivered after a +// reconnect +// +HRESULT CDynamicOutputPin::DynamicReconnect( const CMediaType* pmt ) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + if((m_pGraphConfig == NULL) || (NULL == m_hStopEvent)) { + return E_FAIL; + } + + HRESULT hr = m_pGraphConfig->Reconnect( + this, + NULL, + pmt, + NULL, + m_hStopEvent, + AM_GRAPH_CONFIG_RECONNECT_CACHE_REMOVED_FILTERS ); + + return hr; +} + +HRESULT CDynamicOutputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = CBaseOutputPin::CompleteConnect(pReceivePin); + if(SUCCEEDED(hr)) { + if(!IsStopped() && m_pAllocator) { + hr = m_pAllocator->Commit(); + ASSERT(hr != VFW_E_ALREADY_COMMITTED); + } + } + + return hr; +} + +#ifdef DEBUG +void CDynamicOutputPin::AssertValid(void) +{ + // Make sure the object was correctly initialized. + + // This ASSERT only fires if the object failed to initialize + // and the user ignored the constructor's return code (phr). + ASSERT(NULL != m_hUnblockOutputPinEvent); + + // If either of these ASSERTs fire, the user did not correctly call + // SetConfigInfo(). + ASSERT(NULL != m_hStopEvent); + ASSERT(NULL != m_pGraphConfig); + + // Make sure the block state is consistent. + + CAutoLock alBlockStateLock(&m_BlockStateLock); + + // BLOCK_STATE variables only have three legal values: PENDING, BLOCKED and NOT_BLOCKED. + ASSERT((NOT_BLOCKED == m_BlockState) || (PENDING == m_BlockState) || (BLOCKED == m_BlockState)); + + // m_hNotifyCallerPinBlockedEvent is only needed when a block operation cannot complete + // immediately. + ASSERT(((NULL == m_hNotifyCallerPinBlockedEvent) && (PENDING != m_BlockState)) || + ((NULL != m_hNotifyCallerPinBlockedEvent) && (PENDING == m_BlockState)) ); + + // m_dwBlockCallerThreadID should always be 0 if the pin is not blocked and + // the user is not trying to block the pin. + ASSERT((0 == m_dwBlockCallerThreadID) || (NOT_BLOCKED != m_BlockState)); + + // If this ASSERT fires, the streaming thread is using the output pin and the + // output pin is blocked. + ASSERT(((0 != m_dwNumOutstandingOutputPinUsers) && (BLOCKED != m_BlockState)) || + ((0 == m_dwNumOutstandingOutputPinUsers) && (NOT_BLOCKED != m_BlockState)) || + ((0 == m_dwNumOutstandingOutputPinUsers) && (NOT_BLOCKED == m_BlockState)) ); +} +#endif // DEBUG + +HRESULT CDynamicOutputPin::WaitEvent(HANDLE hEvent) +{ + const DWORD EVENT_SIGNALED = WAIT_OBJECT_0; + + DWORD dwReturnValue = ::WaitForSingleObject(hEvent, INFINITE); + + switch( dwReturnValue ) { + case EVENT_SIGNALED: + return S_OK; + + case WAIT_FAILED: + return AmGetLastErrorToHResult(); + + default: + DbgBreak( "An Unexpected case occured in CDynamicOutputPin::WaitEvent()." ); + return E_UNEXPECTED; + } +} + +//===================================================================== +//===================================================================== +// Implements CBaseAllocator +//===================================================================== +//===================================================================== + + +/* Constructor overrides the default settings for the free list to request + that it be alertable (ie the list can be cast to a handle which can be + passed to WaitForSingleObject). Both of the allocator lists also ask for + object locking, the all list matches the object default settings but I + have included them here just so it is obvious what kind of list it is */ + +CBaseAllocator::CBaseAllocator(TCHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr, + BOOL bEvent, + BOOL fEnableReleaseCallback + ) : + CUnknown(pName, pUnk), + m_lAllocated(0), + m_bChanged(FALSE), + m_bCommitted(FALSE), + m_bDecommitInProgress(FALSE), + m_lSize(0), + m_lCount(0), + m_lAlignment(0), + m_lPrefix(0), + m_hSem(NULL), + m_lWaiting(0), + m_fEnableReleaseCallback(fEnableReleaseCallback), + m_pNotify(NULL) +{ + + if (bEvent) { + m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL); + if (m_hSem == NULL) { + *phr = E_OUTOFMEMORY; + return; + } + } +} + +#ifdef UNICODE +CBaseAllocator::CBaseAllocator(CHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr, + BOOL bEvent, + BOOL fEnableReleaseCallback) : + CUnknown(pName, pUnk), + m_lAllocated(0), + m_bChanged(FALSE), + m_bCommitted(FALSE), + m_bDecommitInProgress(FALSE), + m_lSize(0), + m_lCount(0), + m_lAlignment(0), + m_lPrefix(0), + m_hSem(NULL), + m_lWaiting(0), + m_fEnableReleaseCallback(fEnableReleaseCallback), + m_pNotify(NULL) +{ + + if (bEvent) { + m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL); + if (m_hSem == NULL) { + *phr = E_OUTOFMEMORY; + return; + } + } +} +#endif + +/* Destructor */ + +CBaseAllocator::~CBaseAllocator() +{ + // we can't call Decommit here since that would mean a call to a + // pure virtual in destructor. + // We must assume that the derived class has gone into decommit state in + // its destructor. + + ASSERT(!m_bCommitted); + if (m_hSem != NULL) { + EXECUTE_ASSERT(CloseHandle(m_hSem)); + } + if (m_pNotify) { + m_pNotify->Release(); + } +} + + +/* Override this to publicise our interfaces */ + +STDMETHODIMP +CBaseAllocator::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + /* Do we know about this interface */ + + if (riid == IID_IMemAllocator || + riid == IID_IMemAllocatorCallbackTemp && m_fEnableReleaseCallback) { + return GetInterface((IMemAllocatorCallbackTemp *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +/* This sets the size and count of the required samples. The memory isn't + actually allocated until Commit() is called, if memory has already been + allocated then assuming no samples are outstanding the user may call us + to change the buffering, the memory will be released in Commit() */ + +STDMETHODIMP +CBaseAllocator::SetProperties( + ALLOCATOR_PROPERTIES* pRequest, + ALLOCATOR_PROPERTIES* pActual) +{ + CheckPointer(pRequest, E_POINTER); + CheckPointer(pActual, E_POINTER); + ValidateReadWritePtr(pActual, sizeof(ALLOCATOR_PROPERTIES)); + CAutoLock cObjectLock(this); + + ZeroMemory(pActual, sizeof(ALLOCATOR_PROPERTIES)); + + ASSERT(pRequest->cbBuffer > 0); + + /* Check the alignment requested */ + if (pRequest->cbAlign != 1) { + DbgLog((LOG_ERROR, 2, TEXT("Alignment requested was 0x%x, not 1"), + pRequest->cbAlign)); + return VFW_E_BADALIGN; + } + + /* Can't do this if already committed, there is an argument that says we + should not reject the SetProperties call if there are buffers still + active. However this is called by the source filter, which is the same + person who is holding the samples. Therefore it is not unreasonable + for them to free all their samples before changing the requirements */ + + if (m_bCommitted) { + return VFW_E_ALREADY_COMMITTED; + } + + /* Must be no outstanding buffers */ + + if (m_lAllocated != m_lFree.GetCount()) { + return VFW_E_BUFFERS_OUTSTANDING; + } + + /* There isn't any real need to check the parameters as they + will just be rejected when the user finally calls Commit */ + + pActual->cbBuffer = m_lSize = pRequest->cbBuffer; + pActual->cBuffers = m_lCount = pRequest->cBuffers; + pActual->cbAlign = m_lAlignment = pRequest->cbAlign; + pActual->cbPrefix = m_lPrefix = pRequest->cbPrefix; + + m_bChanged = TRUE; + return NOERROR; +} + +STDMETHODIMP +CBaseAllocator::GetProperties( + ALLOCATOR_PROPERTIES * pActual) +{ + CheckPointer(pActual,E_POINTER); + ValidateReadWritePtr(pActual,sizeof(ALLOCATOR_PROPERTIES)); + + CAutoLock cObjectLock(this); + pActual->cbBuffer = m_lSize; + pActual->cBuffers = m_lCount; + pActual->cbAlign = m_lAlignment; + pActual->cbPrefix = m_lPrefix; + return NOERROR; +} + +// get container for a sample. Blocking, synchronous call to get the +// next free buffer (as represented by an IMediaSample interface). +// on return, the time etc properties will be invalid, but the buffer +// pointer and size will be correct. + +HRESULT CBaseAllocator::GetBuffer(IMediaSample **ppBuffer, + REFERENCE_TIME *pStartTime, + REFERENCE_TIME *pEndTime, + DWORD dwFlags + ) +{ + UNREFERENCED_PARAMETER(pStartTime); + UNREFERENCED_PARAMETER(pEndTime); + UNREFERENCED_PARAMETER(dwFlags); + CMediaSample *pSample; + + *ppBuffer = NULL; + for (;;) + { + { // scope for lock + CAutoLock cObjectLock(this); + + /* Check we are committed */ + if (!m_bCommitted) { + return VFW_E_NOT_COMMITTED; + } + pSample = (CMediaSample *) m_lFree.RemoveHead(); + if (pSample == NULL) { + SetWaiting(); + } + } + + /* If we didn't get a sample then wait for the list to signal */ + + if (pSample) { + break; + } + if (dwFlags & AM_GBF_NOWAIT) { + return VFW_E_TIMEOUT; + } + ASSERT(m_hSem != NULL); + WaitForSingleObject(m_hSem, INFINITE); + } + + /* Addref the buffer up to one. On release + back to zero instead of being deleted, it will requeue itself by + calling the ReleaseBuffer member function. NOTE the owner of a + media sample must always be derived from CBaseAllocator */ + + + ASSERT(pSample->m_cRef == 0); + pSample->m_cRef = 1; + *ppBuffer = pSample; + + + return NOERROR; +} + + +/* Final release of a CMediaSample will call this */ + +STDMETHODIMP +CBaseAllocator::ReleaseBuffer(IMediaSample * pSample) +{ + CheckPointer(pSample,E_POINTER); + ValidateReadPtr(pSample,sizeof(IMediaSample)); + + + + BOOL bRelease = FALSE; + { + CAutoLock cal(this); + + /* Put back on the free list */ + + m_lFree.Add((CMediaSample *)pSample); + if (m_lWaiting != 0) { + NotifySample(); + } + + // if there is a pending Decommit, then we need to complete it by + // calling Free() when the last buffer is placed on the free list + + LONG l1 = m_lFree.GetCount(); + if (m_bDecommitInProgress && (l1 == m_lAllocated)) { + Free(); + m_bDecommitInProgress = FALSE; + bRelease = TRUE; + } + } + + if (m_pNotify) { + + ASSERT(m_fEnableReleaseCallback); + + // + // Note that this is not synchronized with setting up a notification + // method. + // + m_pNotify->NotifyRelease(); + } + + /* For each buffer there is one AddRef, made in GetBuffer and released + here. This may cause the allocator and all samples to be deleted */ + + if (bRelease) { + Release(); + } + return NOERROR; +} + +STDMETHODIMP +CBaseAllocator::SetNotify( + IMemAllocatorNotifyCallbackTemp* pNotify + ) +{ + ASSERT(m_fEnableReleaseCallback); + CAutoLock lck(this); + if (pNotify) { + pNotify->AddRef(); + } + if (m_pNotify) { + m_pNotify->Release(); + } + m_pNotify = pNotify; + return S_OK; +} + +STDMETHODIMP +CBaseAllocator::GetFreeCount( + LONG* plBuffersFree + ) +{ + ASSERT(m_fEnableReleaseCallback); + CAutoLock cObjectLock(this); + *plBuffersFree = m_lCount - m_lAllocated + m_lFree.GetCount(); + return NOERROR; +} + +void +CBaseAllocator::NotifySample() +{ + if (m_lWaiting != 0) { + ASSERT(m_hSem != NULL); + ReleaseSemaphore(m_hSem, m_lWaiting, 0); + m_lWaiting = 0; + } +} + +STDMETHODIMP +CBaseAllocator::Commit() +{ + /* Check we are not decommitted */ + CAutoLock cObjectLock(this); + + // cannot need to alloc or re-alloc if we are committed + if (m_bCommitted) { + return NOERROR; + } + + /* Allow GetBuffer calls */ + + m_bCommitted = TRUE; + + // is there a pending decommit ? if so, just cancel it + if (m_bDecommitInProgress) { + m_bDecommitInProgress = FALSE; + + // don't call Alloc at this point. He cannot allow SetProperties + // between Decommit and the last free, so the buffer size cannot have + // changed. And because some of the buffers are not free yet, he + // cannot re-alloc anyway. + return NOERROR; + } + + DbgLog((LOG_MEMORY, 1, TEXT("Allocating: %ldx%ld"), m_lCount, m_lSize)); + + // actually need to allocate the samples + HRESULT hr = Alloc(); + if (FAILED(hr)) { + m_bCommitted = FALSE; + return hr; + } + AddRef(); + return NOERROR; +} + + +STDMETHODIMP +CBaseAllocator::Decommit() +{ + BOOL bRelease = FALSE; + { + /* Check we are not already decommitted */ + CAutoLock cObjectLock(this); + if (m_bCommitted == FALSE) { + if (m_bDecommitInProgress == FALSE) { + return NOERROR; + } + } + + /* No more GetBuffer calls will succeed */ + m_bCommitted = FALSE; + + // are any buffers outstanding? + if (m_lFree.GetCount() < m_lAllocated) { + // please complete the decommit when last buffer is freed + m_bDecommitInProgress = TRUE; + } else { + m_bDecommitInProgress = FALSE; + + // need to complete the decommit here as there are no + // outstanding buffers + + Free(); + bRelease = TRUE; + } + + // Tell anyone waiting that they can go now so we can + // reject their call + NotifySample(); + } + + if (bRelease) { + Release(); + } + return NOERROR; +} + + +/* Base definition of allocation which checks we are ok to go ahead and do + the full allocation. We return S_FALSE if the requirements are the same */ + +HRESULT +CBaseAllocator::Alloc(void) +{ + /* Error if he hasn't set the size yet */ + if (m_lCount <= 0 || m_lSize <= 0 || m_lAlignment <= 0) { + return VFW_E_SIZENOTSET; + } + + /* should never get here while buffers outstanding */ + ASSERT(m_lFree.GetCount() == m_lAllocated); + + /* If the requirements haven't changed then don't reallocate */ + if (m_bChanged == FALSE) { + return S_FALSE; + } + + return NOERROR; +} + +/* Implement CBaseAllocator::CSampleList::Remove(pSample) + Removes pSample from the list +*/ +void +CBaseAllocator::CSampleList::Remove(CMediaSample * pSample) +{ + CMediaSample **pSearch; + for (pSearch = &m_List; + *pSearch != NULL; + pSearch = &(CBaseAllocator::NextSample(*pSearch))) { + if (*pSearch == pSample) { + *pSearch = CBaseAllocator::NextSample(pSample); + CBaseAllocator::NextSample(pSample) = NULL; + m_nOnList--; + return; + } + } + DbgBreak("Couldn't find sample in list"); +} + +//===================================================================== +//===================================================================== +// Implements CMemAllocator +//===================================================================== +//===================================================================== + + +/* This goes in the factory template table to create new instances */ +CUnknown *CMemAllocator::CreateInstance(LPUNKNOWN pUnk, HRESULT *phr) +{ + CUnknown *pUnkRet = new CMemAllocator(NAME("CMemAllocator"), pUnk, phr); + return pUnkRet; +} + +CMemAllocator::CMemAllocator( + TCHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr) + : CBaseAllocator(pName, pUnk, phr, TRUE, TRUE), + m_pBuffer(NULL) +{ +} + +#ifdef UNICODE +CMemAllocator::CMemAllocator( + CHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr) + : CBaseAllocator(pName, pUnk, phr, TRUE, TRUE), + m_pBuffer(NULL) +{ +} +#endif + +/* This sets the size and count of the required samples. The memory isn't + actually allocated until Commit() is called, if memory has already been + allocated then assuming no samples are outstanding the user may call us + to change the buffering, the memory will be released in Commit() */ +STDMETHODIMP +CMemAllocator::SetProperties( + ALLOCATOR_PROPERTIES* pRequest, + ALLOCATOR_PROPERTIES* pActual) +{ + CheckPointer(pActual,E_POINTER); + ValidateReadWritePtr(pActual,sizeof(ALLOCATOR_PROPERTIES)); + CAutoLock cObjectLock(this); + + ZeroMemory(pActual, sizeof(ALLOCATOR_PROPERTIES)); + + ASSERT(pRequest->cbBuffer > 0); + + SYSTEM_INFO SysInfo; + GetSystemInfo(&SysInfo); + + /* Check the alignment request is a power of 2 */ + if ((-pRequest->cbAlign & pRequest->cbAlign) != pRequest->cbAlign) { + DbgLog((LOG_ERROR, 1, TEXT("Alignment requested 0x%x not a power of 2!"), + pRequest->cbAlign)); + } + /* Check the alignment requested */ + if (pRequest->cbAlign == 0 || + (SysInfo.dwAllocationGranularity & (pRequest->cbAlign - 1)) != 0) { + DbgLog((LOG_ERROR, 1, TEXT("Invalid alignment 0x%x requested - granularity = 0x%x"), + pRequest->cbAlign, SysInfo.dwAllocationGranularity)); + return VFW_E_BADALIGN; + } + + /* Can't do this if already committed, there is an argument that says we + should not reject the SetProperties call if there are buffers still + active. However this is called by the source filter, which is the same + person who is holding the samples. Therefore it is not unreasonable + for them to free all their samples before changing the requirements */ + + if (m_bCommitted == TRUE) { + return VFW_E_ALREADY_COMMITTED; + } + + /* Must be no outstanding buffers */ + + if (m_lFree.GetCount() < m_lAllocated) { + return VFW_E_BUFFERS_OUTSTANDING; + } + + /* There isn't any real need to check the parameters as they + will just be rejected when the user finally calls Commit */ + + // round length up to alignment - remember that prefix is included in + // the alignment + LONG lSize = pRequest->cbBuffer + pRequest->cbPrefix; + LONG lRemainder = lSize % pRequest->cbAlign; + if (lRemainder != 0) { + lSize = lSize - lRemainder + pRequest->cbAlign; + } + pActual->cbBuffer = m_lSize = (lSize - pRequest->cbPrefix); + + pActual->cBuffers = m_lCount = pRequest->cBuffers; + pActual->cbAlign = m_lAlignment = pRequest->cbAlign; + pActual->cbPrefix = m_lPrefix = pRequest->cbPrefix; + + m_bChanged = TRUE; + return NOERROR; +} + +// override this to allocate our resources when Commit is called. +// +// note that our resources may be already allocated when this is called, +// since we don't free them on Decommit. We will only be called when in +// decommit state with all buffers free. +// +// object locked by caller +HRESULT +CMemAllocator::Alloc(void) +{ + CAutoLock lck(this); + + /* Check he has called SetProperties */ + HRESULT hr = CBaseAllocator::Alloc(); + if (FAILED(hr)) { + return hr; + } + + /* If the requirements haven't changed then don't reallocate */ + if (hr == S_FALSE) { + ASSERT(m_pBuffer); + return NOERROR; + } + ASSERT(hr == S_OK); // we use this fact in the loop below + + /* Free the old resources */ + if (m_pBuffer) { + ReallyFree(); + } + + /* Compute the aligned size */ + LONG lAlignedSize = m_lSize + m_lPrefix; + if (m_lAlignment > 1) { + LONG lRemainder = lAlignedSize % m_lAlignment; + if (lRemainder != 0) { + lAlignedSize += (m_lAlignment - lRemainder); + } + } + + /* Create the contiguous memory block for the samples + making sure it's properly aligned (64K should be enough!) + */ + ASSERT(lAlignedSize % m_lAlignment == 0); + + m_pBuffer = (PBYTE)VirtualAlloc(NULL, + m_lCount * lAlignedSize, + MEM_COMMIT, + PAGE_READWRITE); + + if (m_pBuffer == NULL) { + return E_OUTOFMEMORY; + } + + LPBYTE pNext = m_pBuffer; + CMediaSample *pSample; + + ASSERT(m_lAllocated == 0); + + // Create the new samples - we have allocated m_lSize bytes for each sample + // plus m_lPrefix bytes per sample as a prefix. We set the pointer to + // the memory after the prefix - so that GetPointer() will return a pointer + // to m_lSize bytes. + for (; m_lAllocated < m_lCount; m_lAllocated++, pNext += lAlignedSize) { + + + pSample = new CMediaSample( + NAME("Default memory media sample"), + this, + &hr, + pNext + m_lPrefix, // GetPointer() value + m_lSize); // not including prefix + + ASSERT(SUCCEEDED(hr)); + if (pSample == NULL) { + return E_OUTOFMEMORY; + } + + // This CANNOT fail + m_lFree.Add(pSample); + } + + m_bChanged = FALSE; + return NOERROR; +} + + +// override this to free up any resources we have allocated. +// called from the base class on Decommit when all buffers have been +// returned to the free list. +// +// caller has already locked the object. + +// in our case, we keep the memory until we are deleted, so +// we do nothing here. The memory is deleted in the destructor by +// calling ReallyFree() +void +CMemAllocator::Free(void) +{ + return; +} + + +// called from the destructor (and from Alloc if changing size/count) to +// actually free up the memory +void +CMemAllocator::ReallyFree(void) +{ + /* Should never be deleting this unless all buffers are freed */ + + ASSERT(m_lAllocated == m_lFree.GetCount()); + + /* Free up all the CMediaSamples */ + + CMediaSample *pSample; + for (;;) { + pSample = m_lFree.RemoveHead(); + if (pSample != NULL) { + delete pSample; + } else { + break; + } + } + + m_lAllocated = 0; + + // free the block of buffer memory + if (m_pBuffer) { + EXECUTE_ASSERT(VirtualFree(m_pBuffer, 0, MEM_RELEASE)); + m_pBuffer = NULL; + } +} + + +/* Destructor frees our memory resources */ + +CMemAllocator::~CMemAllocator() +{ + Decommit(); + ReallyFree(); +} + +// ------------------------------------------------------------------------ +// filter registration through IFilterMapper. used if IFilterMapper is +// not found (Quartz 1.0 install) + +STDAPI +AMovieSetupRegisterFilter( const AMOVIESETUP_FILTER * const psetupdata + , IFilterMapper * pIFM + , BOOL bRegister ) +{ + DbgLog((LOG_TRACE, 3, TEXT("= AMovieSetupRegisterFilter"))); + + // check we've got data + // + if( NULL == psetupdata ) return S_FALSE; + + + // unregister filter + // (as pins are subkeys of filter's CLSID key + // they do not need to be removed separately). + // + DbgLog((LOG_TRACE, 3, TEXT("= = unregister filter"))); + HRESULT hr = pIFM->UnregisterFilter( *(psetupdata->clsID) ); + + + if( bRegister ) + { + // register filter + // + DbgLog((LOG_TRACE, 3, TEXT("= = register filter"))); + hr = pIFM->RegisterFilter( *(psetupdata->clsID) + , psetupdata->strName + , psetupdata->dwMerit ); + if( SUCCEEDED(hr) ) + { + // all its pins + // + DbgLog((LOG_TRACE, 3, TEXT("= = register filter pins"))); + for( UINT m1=0; m1 < psetupdata->nPins; m1++ ) + { + hr = pIFM->RegisterPin( *(psetupdata->clsID) + , psetupdata->lpPin[m1].strName + , psetupdata->lpPin[m1].bRendered + , psetupdata->lpPin[m1].bOutput + , psetupdata->lpPin[m1].bZero + , psetupdata->lpPin[m1].bMany + , *(psetupdata->lpPin[m1].clsConnectsToFilter) + , psetupdata->lpPin[m1].strConnectsToPin ); + + if( SUCCEEDED(hr) ) + { + // and each pin's media types + // + DbgLog((LOG_TRACE, 3, TEXT("= = register filter pin types"))); + for( UINT m2=0; m2 < psetupdata->lpPin[m1].nMediaTypes; m2++ ) + { + hr = pIFM->RegisterPinType( *(psetupdata->clsID) + , psetupdata->lpPin[m1].strName + , *(psetupdata->lpPin[m1].lpMediaType[m2].clsMajorType) + , *(psetupdata->lpPin[m1].lpMediaType[m2].clsMinorType) ); + if( FAILED(hr) ) break; + } + if( FAILED(hr) ) break; + } + if( FAILED(hr) ) break; + } + } + } + + // handle one acceptable "error" - that + // of filter not being registered! + // (couldn't find a suitable #define'd + // name for the error!) + // + if( 0x80070002 == hr) + return NOERROR; + else + return hr; +} + +// Remove warnings about unreferenced inline functions +#pragma warning(disable:4514) + diff --git a/plugins/GSdx/baseclasses/amfilter.h b/plugins/GSdx/baseclasses/amfilter.h new file mode 100644 index 0000000000..47ffc0a5f0 --- /dev/null +++ b/plugins/GSdx/baseclasses/amfilter.h @@ -0,0 +1,1587 @@ +//------------------------------------------------------------------------------ +// File: AMFilter.h +// +// Desc: DirectShow base classes - efines class hierarchy for streams +// architecture. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __FILTER__ +#define __FILTER__ + +/* The following classes are declared in this header: */ + +class CBaseMediaFilter; // IMediaFilter support +class CBaseFilter; // IBaseFilter,IMediaFilter support +class CBasePin; // Abstract base class for IPin interface +class CEnumPins; // Enumerate input and output pins +class CEnumMediaTypes; // Enumerate the pin's preferred formats +class CBaseOutputPin; // Adds data provider member functions +class CBaseInputPin; // Implements IMemInputPin interface +class CMediaSample; // Basic transport unit for IMemInputPin +class CBaseAllocator; // General list guff for most allocators +class CMemAllocator; // Implements memory buffer allocation + + +//===================================================================== +//===================================================================== +// +// QueryFilterInfo and QueryPinInfo AddRef the interface pointers +// they return. You can use the macro below to release the interface. +// +//===================================================================== +//===================================================================== + +#define QueryFilterInfoReleaseGraph(fi) if ((fi).pGraph) (fi).pGraph->Release(); + +#define QueryPinInfoReleaseFilter(pi) if ((pi).pFilter) (pi).pFilter->Release(); + +//===================================================================== +//===================================================================== +// Defines CBaseMediaFilter +// +// Abstract base class implementing IMediaFilter. +// +// Typically you will derive your filter from CBaseFilter rather than +// this, unless you are implementing an object such as a plug-in +// distributor that needs to support IMediaFilter but not IBaseFilter. +// +// Note that IMediaFilter is derived from IPersist to allow query of +// class id. +//===================================================================== +//===================================================================== + +class AM_NOVTABLE CBaseMediaFilter : public CUnknown, + public IMediaFilter +{ + +protected: + + FILTER_STATE m_State; // current state: running, paused + IReferenceClock *m_pClock; // this filter's reference clock + // note: all filters in a filter graph use the same clock + + // offset from stream time to reference time + CRefTime m_tStart; + + CLSID m_clsid; // This filters clsid + // used for serialization + CCritSec *m_pLock; // Object we use for locking + +public: + + CBaseMediaFilter( + const TCHAR *pName, + LPUNKNOWN pUnk, + CCritSec *pLock, + REFCLSID clsid); + + virtual ~CBaseMediaFilter(); + + DECLARE_IUNKNOWN + + // override this to say what interfaces we support where + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv); + + // + // --- IPersist method --- + // + + STDMETHODIMP GetClassID(CLSID *pClsID); + + // --- IMediaFilter methods --- + + STDMETHODIMP GetState(DWORD dwMSecs, FILTER_STATE *State); + + STDMETHODIMP SetSyncSource(IReferenceClock *pClock); + + STDMETHODIMP GetSyncSource(IReferenceClock **pClock); + + // default implementation of Stop and Pause just record the + // state. Override to activate or de-activate your filter. + // Note that Run when called from Stopped state will call Pause + // to ensure activation, so if you are a source or transform + // you will probably not need to override Run. + STDMETHODIMP Stop(); + STDMETHODIMP Pause(); + + + // the start parameter is the difference to be added to the + // sample's stream time to get the reference time for + // its presentation + STDMETHODIMP Run(REFERENCE_TIME tStart); + + // --- helper methods --- + + // return the current stream time - ie find out what + // stream time should be appearing now + virtual HRESULT StreamTime(CRefTime& rtStream); + + // Is the filter currently active? (running or paused) + BOOL IsActive() { + CAutoLock cObjectLock(m_pLock); + return ((m_State == State_Paused) || (m_State == State_Running)); + }; +}; + +//===================================================================== +//===================================================================== +// Defines CBaseFilter +// +// An abstract class providing basic IBaseFilter support for pin +// enumeration and filter information reading. +// +// We cannot derive from CBaseMediaFilter since methods in IMediaFilter +// are also in IBaseFilter and would be ambiguous. Since much of the code +// assumes that they derive from a class that has m_State and other state +// directly available, we duplicate code from CBaseMediaFilter rather than +// having a member variable. +// +// Derive your filter from this, or from a derived object such as +// CTransformFilter. +//===================================================================== +//===================================================================== + + +class AM_NOVTABLE CBaseFilter : public CUnknown, // Handles an IUnknown + public IBaseFilter, // The Filter Interface + public IAMovieSetup // For un/registration +{ + +friend class CBasePin; + +protected: + FILTER_STATE m_State; // current state: running, paused + IReferenceClock *m_pClock; // this graph's ref clock + CRefTime m_tStart; // offset from stream time to reference time + CLSID m_clsid; // This filters clsid + // used for serialization + CCritSec *m_pLock; // Object we use for locking + + WCHAR *m_pName; // Full filter name + IFilterGraph *m_pGraph; // Graph we belong to + IMediaEventSink *m_pSink; // Called with notify events + LONG m_PinVersion; // Current pin version + +public: + + CBaseFilter( + const TCHAR *pName, // Object description + LPUNKNOWN pUnk, // IUnknown of delegating object + CCritSec *pLock, // Object who maintains lock + REFCLSID clsid); // The clsid to be used to serialize this filter + + CBaseFilter( + TCHAR *pName, // Object description + LPUNKNOWN pUnk, // IUnknown of delegating object + CCritSec *pLock, // Object who maintains lock + REFCLSID clsid, // The clsid to be used to serialize this filter + HRESULT *phr); // General OLE return code +#ifdef UNICODE + CBaseFilter( + const CHAR *pName, // Object description + LPUNKNOWN pUnk, // IUnknown of delegating object + CCritSec *pLock, // Object who maintains lock + REFCLSID clsid); // The clsid to be used to serialize this filter + + CBaseFilter( + CHAR *pName, // Object description + LPUNKNOWN pUnk, // IUnknown of delegating object + CCritSec *pLock, // Object who maintains lock + REFCLSID clsid, // The clsid to be used to serialize this filter + HRESULT *phr); // General OLE return code +#endif + ~CBaseFilter(); + + DECLARE_IUNKNOWN + + // override this to say what interfaces we support where + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv); +#ifdef DEBUG + STDMETHODIMP_(ULONG) NonDelegatingRelease(); +#endif + + // + // --- IPersist method --- + // + + STDMETHODIMP GetClassID(CLSID *pClsID); + + // --- IMediaFilter methods --- + + STDMETHODIMP GetState(DWORD dwMSecs, FILTER_STATE *State); + + STDMETHODIMP SetSyncSource(IReferenceClock *pClock); + + STDMETHODIMP GetSyncSource(IReferenceClock **pClock); + + + // override Stop and Pause so we can activate the pins. + // Note that Run will call Pause first if activation needed. + // Override these if you want to activate your filter rather than + // your pins. + STDMETHODIMP Stop(); + STDMETHODIMP Pause(); + + // the start parameter is the difference to be added to the + // sample's stream time to get the reference time for + // its presentation + STDMETHODIMP Run(REFERENCE_TIME tStart); + + // --- helper methods --- + + // return the current stream time - ie find out what + // stream time should be appearing now + virtual HRESULT StreamTime(CRefTime& rtStream); + + // Is the filter currently active? + BOOL IsActive() { + CAutoLock cObjectLock(m_pLock); + return ((m_State == State_Paused) || (m_State == State_Running)); + }; + + // Is this filter stopped (without locking) + BOOL IsStopped() { + return (m_State == State_Stopped); + }; + + // + // --- IBaseFilter methods --- + // + + // pin enumerator + STDMETHODIMP EnumPins( + IEnumPins ** ppEnum); + + + // default behaviour of FindPin assumes pin ids are their names + STDMETHODIMP FindPin( + LPCWSTR Id, + IPin ** ppPin + ); + + STDMETHODIMP QueryFilterInfo( + FILTER_INFO * pInfo); + + STDMETHODIMP JoinFilterGraph( + IFilterGraph * pGraph, + LPCWSTR pName); + + // return a Vendor information string. Optional - may return E_NOTIMPL. + // memory returned should be freed using CoTaskMemFree + // default implementation returns E_NOTIMPL + STDMETHODIMP QueryVendorInfo( + LPWSTR* pVendorInfo + ); + + // --- helper methods --- + + // send an event notification to the filter graph if we know about it. + // returns S_OK if delivered, S_FALSE if the filter graph does not sink + // events, or an error otherwise. + HRESULT NotifyEvent( + long EventCode, + LONG_PTR EventParam1, + LONG_PTR EventParam2); + + // return the filter graph we belong to + IFilterGraph *GetFilterGraph() { + return m_pGraph; + } + + // Request reconnect + // pPin is the pin to reconnect + // pmt is the type to reconnect with - can be NULL + // Calls ReconnectEx on the filter graph + HRESULT ReconnectPin(IPin *pPin, AM_MEDIA_TYPE const *pmt); + + // find out the current pin version (used by enumerators) + virtual LONG GetPinVersion(); + void IncrementPinVersion(); + + // you need to supply these to access the pins from the enumerator + // and for default Stop and Pause/Run activation. + virtual int GetPinCount() PURE; + virtual CBasePin *GetPin(int n) PURE; + + // --- IAMovieSetup methods --- + + STDMETHODIMP Register(); // ask filter to register itself + STDMETHODIMP Unregister(); // and unregister itself + + // --- setup helper methods --- + // (override to return filters setup data) + + virtual LPAMOVIESETUP_FILTER GetSetupData(){ return NULL; } + +}; + + +//===================================================================== +//===================================================================== +// Defines CBasePin +// +// Abstract class that supports the basics of IPin +//===================================================================== +//===================================================================== + +class AM_NOVTABLE CBasePin : public CUnknown, public IPin, public IQualityControl +{ + +protected: + + WCHAR * m_pName; // This pin's name + IPin *m_Connected; // Pin we have connected to + PIN_DIRECTION m_dir; // Direction of this pin + CCritSec *m_pLock; // Object we use for locking + bool m_bRunTimeError; // Run time error generated + bool m_bCanReconnectWhenActive; // OK to reconnect when active + bool m_bTryMyTypesFirst; // When connecting enumerate + // this pin's types first + CBaseFilter *m_pFilter; // Filter we were created by + IQualityControl *m_pQSink; // Target for Quality messages + LONG m_TypeVersion; // Holds current type version + CMediaType m_mt; // Media type of connection + + CRefTime m_tStart; // time from NewSegment call + CRefTime m_tStop; // time from NewSegment + double m_dRate; // rate from NewSegment + +#ifdef DEBUG + LONG m_cRef; // Ref count tracing +#endif + + // displays pin connection information + +#ifdef DEBUG + void DisplayPinInfo(IPin *pReceivePin); + void DisplayTypeInfo(IPin *pPin, const CMediaType *pmt); +#else + void DisplayPinInfo(IPin *pReceivePin) {}; + void DisplayTypeInfo(IPin *pPin, const CMediaType *pmt) {}; +#endif + + // used to agree a media type for a pin connection + + // given a specific media type, attempt a connection (includes + // checking that the type is acceptable to this pin) + HRESULT + AttemptConnection( + IPin* pReceivePin, // connect to this pin + const CMediaType* pmt // using this type + ); + + // try all the media types in this enumerator - for each that + // we accept, try to connect using ReceiveConnection. + HRESULT TryMediaTypes( + IPin *pReceivePin, // connect to this pin + const CMediaType *pmt, // proposed type from Connect + IEnumMediaTypes *pEnum); // try this enumerator + + // establish a connection with a suitable mediatype. Needs to + // propose a media type if the pmt pointer is null or partially + // specified - use TryMediaTypes on both our and then the other pin's + // enumerator until we find one that works. + HRESULT AgreeMediaType( + IPin *pReceivePin, // connect to this pin + const CMediaType *pmt); // proposed type from Connect + +public: + + CBasePin( + TCHAR *pObjectName, // Object description + CBaseFilter *pFilter, // Owning filter who knows about pins + CCritSec *pLock, // Object who implements the lock + HRESULT *phr, // General OLE return code + LPCWSTR pName, // Pin name for us + PIN_DIRECTION dir); // Either PINDIR_INPUT or PINDIR_OUTPUT +#ifdef UNICODE + CBasePin( + CHAR *pObjectName, // Object description + CBaseFilter *pFilter, // Owning filter who knows about pins + CCritSec *pLock, // Object who implements the lock + HRESULT *phr, // General OLE return code + LPCWSTR pName, // Pin name for us + PIN_DIRECTION dir); // Either PINDIR_INPUT or PINDIR_OUTPUT +#endif + virtual ~CBasePin(); + + DECLARE_IUNKNOWN + + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv); + STDMETHODIMP_(ULONG) NonDelegatingRelease(); + STDMETHODIMP_(ULONG) NonDelegatingAddRef(); + + // --- IPin methods --- + + // take lead role in establishing a connection. Media type pointer + // may be null, or may point to partially-specified mediatype + // (subtype or format type may be GUID_NULL). + STDMETHODIMP Connect( + IPin * pReceivePin, + const AM_MEDIA_TYPE *pmt // optional media type + ); + + // (passive) accept a connection from another pin + STDMETHODIMP ReceiveConnection( + IPin * pConnector, // this is the initiating connecting pin + const AM_MEDIA_TYPE *pmt // this is the media type we will exchange + ); + + STDMETHODIMP Disconnect(); + + STDMETHODIMP ConnectedTo(IPin **pPin); + + STDMETHODIMP ConnectionMediaType(AM_MEDIA_TYPE *pmt); + + STDMETHODIMP QueryPinInfo( + PIN_INFO * pInfo + ); + + STDMETHODIMP QueryDirection( + PIN_DIRECTION * pPinDir + ); + + STDMETHODIMP QueryId( + LPWSTR * Id + ); + + // does the pin support this media type + STDMETHODIMP QueryAccept( + const AM_MEDIA_TYPE *pmt + ); + + // return an enumerator for this pins preferred media types + STDMETHODIMP EnumMediaTypes( + IEnumMediaTypes **ppEnum + ); + + // return an array of IPin* - the pins that this pin internally connects to + // All pins put in the array must be AddReffed (but no others) + // Errors: "Can't say" - FAIL, not enough slots - return S_FALSE + // Default: return E_NOTIMPL + // The filter graph will interpret NOT_IMPL as any input pin connects to + // all visible output pins and vice versa. + // apPin can be NULL if nPin==0 (not otherwise). + STDMETHODIMP QueryInternalConnections( + IPin* *apPin, // array of IPin* + ULONG *nPin // on input, the number of slots + // on output the number of pins + ) { return E_NOTIMPL; } + + // Called when no more data will be sent + STDMETHODIMP EndOfStream(void); + + // Begin/EndFlush still PURE + + // NewSegment notifies of the start/stop/rate applying to the data + // about to be received. Default implementation records data and + // returns S_OK. + // Override this to pass downstream. + STDMETHODIMP NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate); + + //================================================================================ + // IQualityControl methods + //================================================================================ + + STDMETHODIMP Notify(IBaseFilter * pSender, Quality q); + + STDMETHODIMP SetSink(IQualityControl * piqc); + + // --- helper methods --- + + // Returns true if the pin is connected. false otherwise. + BOOL IsConnected(void) {return (m_Connected != NULL); }; + // Return the pin this is connected to (if any) + IPin * GetConnected() { return m_Connected; }; + + // Check if our filter is currently stopped + BOOL IsStopped() { + return (m_pFilter->m_State == State_Stopped); + }; + + // find out the current type version (used by enumerators) + virtual LONG GetMediaTypeVersion(); + void IncrementTypeVersion(); + + // switch the pin to active (paused or running) mode + // not an error to call this if already active + virtual HRESULT Active(void); + + // switch the pin to inactive state - may already be inactive + virtual HRESULT Inactive(void); + + // Notify of Run() from filter + virtual HRESULT Run(REFERENCE_TIME tStart); + + // check if the pin can support this specific proposed type and format + virtual HRESULT CheckMediaType(const CMediaType *) PURE; + + // set the connection to use this format (previously agreed) + virtual HRESULT SetMediaType(const CMediaType *); + + // check that the connection is ok before verifying it + // can be overridden eg to check what interfaces will be supported. + virtual HRESULT CheckConnect(IPin *); + + // Set and release resources required for a connection + virtual HRESULT BreakConnect(); + virtual HRESULT CompleteConnect(IPin *pReceivePin); + + // returns the preferred formats for a pin + virtual HRESULT GetMediaType(int iPosition,CMediaType *pMediaType); + + // access to NewSegment values + REFERENCE_TIME CurrentStopTime() { + return m_tStop; + } + REFERENCE_TIME CurrentStartTime() { + return m_tStart; + } + double CurrentRate() { + return m_dRate; + } + + // Access name + LPWSTR Name() { return m_pName; }; + + // Can reconnectwhen active? + void SetReconnectWhenActive(bool bCanReconnect) + { + m_bCanReconnectWhenActive = bCanReconnect; + } + + bool CanReconnectWhenActive() + { + return m_bCanReconnectWhenActive; + } + +protected: + STDMETHODIMP DisconnectInternal(); +}; + + +//===================================================================== +//===================================================================== +// Defines CEnumPins +// +// Pin enumerator class that works by calling CBaseFilter. This interface +// is provided by CBaseFilter::EnumPins and calls GetPinCount() and +// GetPin() to enumerate existing pins. Needs to be a separate object so +// that it can be cloned (creating an existing object at the same +// position in the enumeration) +// +//===================================================================== +//===================================================================== + +class CEnumPins : public IEnumPins // The interface we support +{ + int m_Position; // Current ordinal position + int m_PinCount; // Number of pins available + CBaseFilter *m_pFilter; // The filter who owns us + LONG m_Version; // Pin version information + LONG m_cRef; + + typedef CGenericList CPinList; + + CPinList m_PinCache; // These pointers have not been AddRef'ed and + // so they should not be dereferenced. They are + // merely kept to ID which pins have been enumerated. + +#ifdef DEBUG + DWORD m_dwCookie; +#endif + + /* If while we are retrieving a pin for example from the filter an error + occurs we assume that our internal state is stale with respect to the + filter (someone may have deleted all the pins). We can check before + starting whether or not the operation is likely to fail by asking the + filter what it's current version number is. If the filter has not + overriden the GetPinVersion method then this will always match */ + + BOOL AreWeOutOfSync() { + return (m_pFilter->GetPinVersion() == m_Version ? FALSE : TRUE); + }; + + /* This method performs the same operations as Reset, except is does not clear + the cache of pins already enumerated. */ + + STDMETHODIMP Refresh(); + +public: + + CEnumPins( + CBaseFilter *pFilter, + CEnumPins *pEnumPins); + + virtual ~CEnumPins(); + + // IUnknown + STDMETHODIMP QueryInterface(REFIID riid, void **ppv); + STDMETHODIMP_(ULONG) AddRef(); + STDMETHODIMP_(ULONG) Release(); + + // IEnumPins + STDMETHODIMP Next( + ULONG cPins, // place this many pins... + IPin ** ppPins, // ...in this array of IPin* + ULONG * pcFetched // actual count passed returned here + ); + + STDMETHODIMP Skip(ULONG cPins); + STDMETHODIMP Reset(); + STDMETHODIMP Clone(IEnumPins **ppEnum); + + +}; + + +//===================================================================== +//===================================================================== +// Defines CEnumMediaTypes +// +// Enumerates the preferred formats for input and output pins +//===================================================================== +//===================================================================== + +class CEnumMediaTypes : public IEnumMediaTypes // The interface we support +{ + int m_Position; // Current ordinal position + CBasePin *m_pPin; // The pin who owns us + LONG m_Version; // Media type version value + LONG m_cRef; +#ifdef DEBUG + DWORD m_dwCookie; +#endif + + /* The media types a filter supports can be quite dynamic so we add to + the general IEnumXXXX interface the ability to be signaled when they + change via an event handle the connected filter supplies. Until the + Reset method is called after the state changes all further calls to + the enumerator (except Reset) will return E_UNEXPECTED error code */ + + BOOL AreWeOutOfSync() { + return (m_pPin->GetMediaTypeVersion() == m_Version ? FALSE : TRUE); + }; + +public: + + CEnumMediaTypes( + CBasePin *pPin, + CEnumMediaTypes *pEnumMediaTypes); + + virtual ~CEnumMediaTypes(); + + // IUnknown + STDMETHODIMP QueryInterface(REFIID riid, void **ppv); + STDMETHODIMP_(ULONG) AddRef(); + STDMETHODIMP_(ULONG) Release(); + + // IEnumMediaTypes + STDMETHODIMP Next( + ULONG cMediaTypes, // place this many pins... + AM_MEDIA_TYPE ** ppMediaTypes, // ...in this array + ULONG * pcFetched // actual count passed + ); + + STDMETHODIMP Skip(ULONG cMediaTypes); + STDMETHODIMP Reset(); + STDMETHODIMP Clone(IEnumMediaTypes **ppEnum); +}; + + + + +//===================================================================== +//===================================================================== +// Defines CBaseOutputPin +// +// class derived from CBasePin that can pass buffers to a connected pin +// that supports IMemInputPin. Supports IPin. +// +// Derive your output pin from this. +// +//===================================================================== +//===================================================================== + +class AM_NOVTABLE CBaseOutputPin : public CBasePin +{ + +protected: + + IMemAllocator *m_pAllocator; + IMemInputPin *m_pInputPin; // interface on the downstreaminput pin + // set up in CheckConnect when we connect. + +public: + + CBaseOutputPin( + TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#ifdef UNICODE + CBaseOutputPin( + CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#endif + // override CompleteConnect() so we can negotiate an allocator + virtual HRESULT CompleteConnect(IPin *pReceivePin); + + // negotiate the allocator and its buffer size/count and other properties + // Calls DecideBufferSize to set properties + virtual HRESULT DecideAllocator(IMemInputPin * pPin, IMemAllocator ** pAlloc); + + // override this to set the buffer size and count. Return an error + // if the size/count is not to your liking. + // The allocator properties passed in are those requested by the + // input pin - use eg the alignment and prefix members if you have + // no preference on these. + virtual HRESULT DecideBufferSize( + IMemAllocator * pAlloc, + ALLOCATOR_PROPERTIES * ppropInputRequest + ) PURE; + + // returns an empty sample buffer from the allocator + virtual HRESULT GetDeliveryBuffer(IMediaSample ** ppSample, + REFERENCE_TIME * pStartTime, + REFERENCE_TIME * pEndTime, + DWORD dwFlags); + + // deliver a filled-in sample to the connected input pin + // note - you need to release it after calling this. The receiving + // pin will addref the sample if it needs to hold it beyond the + // call. + virtual HRESULT Deliver(IMediaSample *); + + // override this to control the connection + virtual HRESULT InitAllocator(IMemAllocator **ppAlloc); + HRESULT CheckConnect(IPin *pPin); + HRESULT BreakConnect(); + + // override to call Commit and Decommit + HRESULT Active(void); + HRESULT Inactive(void); + + // we have a default handling of EndOfStream which is to return + // an error, since this should be called on input pins only + STDMETHODIMP EndOfStream(void); + + // called from elsewhere in our filter to pass EOS downstream to + // our connected input pin + virtual HRESULT DeliverEndOfStream(void); + + // same for Begin/EndFlush - we handle Begin/EndFlush since it + // is an error on an output pin, and we have Deliver methods to + // call the methods on the connected pin + STDMETHODIMP BeginFlush(void); + STDMETHODIMP EndFlush(void); + virtual HRESULT DeliverBeginFlush(void); + virtual HRESULT DeliverEndFlush(void); + + // deliver NewSegment to connected pin - you will need to + // override this if you queue any data in your output pin. + virtual HRESULT DeliverNewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate); + + //================================================================================ + // IQualityControl methods + //================================================================================ + + // All inherited from CBasePin and not overridden here. + // STDMETHODIMP Notify(IBaseFilter * pSender, Quality q); + // STDMETHODIMP SetSink(IQualityControl * piqc); +}; + + +//===================================================================== +//===================================================================== +// Defines CBaseInputPin +// +// derive your standard input pin from this. +// you need to supply GetMediaType and CheckConnect etc (see CBasePin), +// and you need to supply Receive to do something more useful. +// +//===================================================================== +//===================================================================== + +class AM_NOVTABLE CBaseInputPin : public CBasePin, + public IMemInputPin +{ + +protected: + + IMemAllocator *m_pAllocator; // Default memory allocator + + // allocator is read-only, so received samples + // cannot be modified (probably only relevant to in-place + // transforms + BYTE m_bReadOnly; + + // in flushing state (between BeginFlush and EndFlush) + // if TRUE, all Receives are returned with S_FALSE + BYTE m_bFlushing; + + // Sample properties - initalized in Receive + AM_SAMPLE2_PROPERTIES m_SampleProps; + +public: + + CBaseInputPin( + TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#ifdef UNICODE + CBaseInputPin( + CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#endif + virtual ~CBaseInputPin(); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // return the allocator interface that this input pin + // would like the output pin to use + STDMETHODIMP GetAllocator(IMemAllocator ** ppAllocator); + + // tell the input pin which allocator the output pin is actually + // going to use. + STDMETHODIMP NotifyAllocator( + IMemAllocator * pAllocator, + BOOL bReadOnly); + + // do something with this media sample + STDMETHODIMP Receive(IMediaSample *pSample); + + // do something with these media samples + STDMETHODIMP ReceiveMultiple ( + IMediaSample **pSamples, + long nSamples, + long *nSamplesProcessed); + + // See if Receive() blocks + STDMETHODIMP ReceiveCanBlock(); + + // Default handling for BeginFlush - call at the beginning + // of your implementation (makes sure that all Receive calls + // fail). After calling this, you need to free any queued data + // and then call downstream. + STDMETHODIMP BeginFlush(void); + + // default handling for EndFlush - call at end of your implementation + // - before calling this, ensure that there is no queued data and no thread + // pushing any more without a further receive, then call downstream, + // then call this method to clear the m_bFlushing flag and re-enable + // receives + STDMETHODIMP EndFlush(void); + + // this method is optional (can return E_NOTIMPL). + // default implementation returns E_NOTIMPL. Override if you have + // specific alignment or prefix needs, but could use an upstream + // allocator + STDMETHODIMP GetAllocatorRequirements(ALLOCATOR_PROPERTIES*pProps); + + // Release the pin's allocator. + HRESULT BreakConnect(); + + // helper method to check the read-only flag + BOOL IsReadOnly() { + return m_bReadOnly; + }; + + // helper method to see if we are flushing + BOOL IsFlushing() { + return m_bFlushing; + }; + + // Override this for checking whether it's OK to process samples + // Also call this from EndOfStream. + virtual HRESULT CheckStreaming(); + + // Pass a Quality notification on to the appropriate sink + HRESULT PassNotify(Quality& q); + + + //================================================================================ + // IQualityControl methods (from CBasePin) + //================================================================================ + + STDMETHODIMP Notify(IBaseFilter * pSender, Quality q); + + // no need to override: + // STDMETHODIMP SetSink(IQualityControl * piqc); + + + // switch the pin to inactive state - may already be inactive + virtual HRESULT Inactive(void); + + // Return sample properties pointer + AM_SAMPLE2_PROPERTIES * SampleProps() { + ASSERT(m_SampleProps.cbData != 0); + return &m_SampleProps; + } + +}; + +/////////////////////////////////////////////////////////////////////////// +// CDynamicOutputPin +// + +class CDynamicOutputPin : public CBaseOutputPin, + public IPinFlowControl +{ +public: +#ifdef UNICODE + CDynamicOutputPin( + CHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); +#endif + + CDynamicOutputPin( + TCHAR *pObjectName, + CBaseFilter *pFilter, + CCritSec *pLock, + HRESULT *phr, + LPCWSTR pName); + + ~CDynamicOutputPin(); + + // IUnknown Methods + DECLARE_IUNKNOWN + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // IPin Methods + STDMETHODIMP Disconnect(void); + + // IPinFlowControl Methods + STDMETHODIMP Block(DWORD dwBlockFlags, HANDLE hEvent); + + // Set graph config info + void SetConfigInfo(IGraphConfig *pGraphConfig, HANDLE hStopEvent); + + #ifdef DEBUG + virtual HRESULT Deliver(IMediaSample *pSample); + virtual HRESULT DeliverEndOfStream(void); + virtual HRESULT DeliverNewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate); + #endif // DEBUG + + HRESULT DeliverBeginFlush(void); + HRESULT DeliverEndFlush(void); + + HRESULT Inactive(void); + HRESULT Active(void); + virtual HRESULT CompleteConnect(IPin *pReceivePin); + + virtual HRESULT StartUsingOutputPin(void); + virtual void StopUsingOutputPin(void); + virtual bool StreamingThreadUsingOutputPin(void); + + HRESULT ChangeOutputFormat + ( + const AM_MEDIA_TYPE *pmt, + REFERENCE_TIME tSegmentStart, + REFERENCE_TIME tSegmentStop, + double dSegmentRate + ); + HRESULT ChangeMediaType(const CMediaType *pmt); + HRESULT DynamicReconnect(const CMediaType *pmt); + +protected: + HRESULT SynchronousBlockOutputPin(void); + HRESULT AsynchronousBlockOutputPin(HANDLE hNotifyCallerPinBlockedEvent); + HRESULT UnblockOutputPin(void); + + void BlockOutputPin(void); + void ResetBlockState(void); + + static HRESULT WaitEvent(HANDLE hEvent); + + enum BLOCK_STATE + { + NOT_BLOCKED, + PENDING, + BLOCKED + }; + + // This lock should be held when the following class members are + // being used: m_hNotifyCallerPinBlockedEvent, m_BlockState, + // m_dwBlockCallerThreadID and m_dwNumOutstandingOutputPinUsers. + CCritSec m_BlockStateLock; + + // This event should be signaled when the output pin is + // not blocked. This is a manual reset event. For more + // information on events, see the documentation for + // CreateEvent() in the Windows SDK. + HANDLE m_hUnblockOutputPinEvent; + + // This event will be signaled when block operation succeedes or + // when the user cancels the block operation. The block operation + // can be canceled by calling IPinFlowControl2::Block( 0, NULL ) + // while the block operation is pending. + HANDLE m_hNotifyCallerPinBlockedEvent; + + // The state of the current block operation. + BLOCK_STATE m_BlockState; + + // The ID of the thread which last called IPinFlowControl::Block(). + // For more information on thread IDs, see the documentation for + // GetCurrentThreadID() in the Windows SDK. + DWORD m_dwBlockCallerThreadID; + + // The number of times StartUsingOutputPin() has been sucessfully + // called and a corresponding call to StopUsingOutputPin() has not + // been made. When this variable is greater than 0, the streaming + // thread is calling IPin::NewSegment(), IPin::EndOfStream(), + // IMemInputPin::Receive() or IMemInputPin::ReceiveMultiple(). The + // streaming thread could also be calling: DynamicReconnect(), + // ChangeMediaType() or ChangeOutputFormat(). The output pin cannot + // be blocked while the output pin is being used. + DWORD m_dwNumOutstandingOutputPinUsers; + + // This event should be set when the IMediaFilter::Stop() is called. + // This is a manual reset event. It is also set when the output pin + // delivers a flush to the connected input pin. + HANDLE m_hStopEvent; + IGraphConfig* m_pGraphConfig; + + // TRUE if the output pin's allocator's samples are read only. + // Otherwise FALSE. For more information, see the documentation + // for IMemInputPin::NotifyAllocator(). + BOOL m_bPinUsesReadOnlyAllocator; + +private: + HRESULT Initialize(void); + HRESULT ChangeMediaTypeHelper(const CMediaType *pmt); + + #ifdef DEBUG + void AssertValid(void); + #endif // DEBUG +}; + +class CAutoUsingOutputPin +{ +public: + CAutoUsingOutputPin( CDynamicOutputPin* pOutputPin, HRESULT* phr ); + ~CAutoUsingOutputPin(); + +private: + CDynamicOutputPin* m_pOutputPin; +}; + +inline CAutoUsingOutputPin::CAutoUsingOutputPin( CDynamicOutputPin* pOutputPin, HRESULT* phr ) : + m_pOutputPin(NULL) +{ + // The caller should always pass in valid pointers. + ASSERT( NULL != pOutputPin ); + ASSERT( NULL != phr ); + + // Make sure the user initialized phr. + ASSERT( S_OK == *phr ); + + HRESULT hr = pOutputPin->StartUsingOutputPin(); + if( FAILED( hr ) ) + { + *phr = hr; + return; + } + + m_pOutputPin = pOutputPin; +} + +inline CAutoUsingOutputPin::~CAutoUsingOutputPin() +{ + if( NULL != m_pOutputPin ) + { + m_pOutputPin->StopUsingOutputPin(); + } +} + +#ifdef DEBUG + +inline HRESULT CDynamicOutputPin::Deliver(IMediaSample *pSample) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + return CBaseOutputPin::Deliver(pSample); +} + +inline HRESULT CDynamicOutputPin::DeliverEndOfStream(void) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT( StreamingThreadUsingOutputPin() ); + + return CBaseOutputPin::DeliverEndOfStream(); +} + +inline HRESULT CDynamicOutputPin::DeliverNewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate) +{ + // The caller should call StartUsingOutputPin() before calling this + // method. + ASSERT(StreamingThreadUsingOutputPin()); + + return CBaseOutputPin::DeliverNewSegment(tStart, tStop, dRate); +} + +#endif // DEBUG + +//===================================================================== +//===================================================================== +// Memory allocators +// +// the shared memory transport between pins requires the input pin +// to provide a memory allocator that can provide sample objects. A +// sample object supports the IMediaSample interface. +// +// CBaseAllocator handles the management of free and busy samples. It +// allocates CMediaSample objects. CBaseAllocator is an abstract class: +// in particular it has no method of initializing the list of free +// samples. CMemAllocator is derived from CBaseAllocator and initializes +// the list of samples using memory from the standard IMalloc interface. +// +// If you want your buffers to live in some special area of memory, +// derive your allocator object from CBaseAllocator. If you derive your +// IMemInputPin interface object from CBaseMemInputPin, you will get +// CMemAllocator-based allocation etc for free and will just need to +// supply the Receive handling, and media type / format negotiation. +//===================================================================== +//===================================================================== + + +//===================================================================== +//===================================================================== +// Defines CMediaSample +// +// an object of this class supports IMediaSample and represents a buffer +// for media data with some associated properties. Releasing it returns +// it to a freelist managed by a CBaseAllocator derived object. +//===================================================================== +//===================================================================== + +class CMediaSample : public IMediaSample2 // The interface we support +{ + +protected: + + friend class CBaseAllocator; + + /* Values for dwFlags - these are used for backward compatiblity + only now - use AM_SAMPLE_xxx + */ + enum { Sample_SyncPoint = 0x01, /* Is this a sync point */ + Sample_Preroll = 0x02, /* Is this a preroll sample */ + Sample_Discontinuity = 0x04, /* Set if start of new segment */ + Sample_TypeChanged = 0x08, /* Has the type changed */ + Sample_TimeValid = 0x10, /* Set if time is valid */ + Sample_MediaTimeValid = 0x20, /* Is the media time valid */ + Sample_TimeDiscontinuity = 0x40, /* Time discontinuity */ + Sample_StopValid = 0x100, /* Stop time valid */ + Sample_ValidFlags = 0x1FF + }; + + /* Properties, the media sample class can be a container for a format + change in which case we take a copy of a type through the SetMediaType + interface function and then return it when GetMediaType is called. As + we do no internal processing on it we leave it as a pointer */ + + DWORD m_dwFlags; /* Flags for this sample */ + /* Type specific flags are packed + into the top word + */ + DWORD m_dwTypeSpecificFlags; /* Media type specific flags */ + LPBYTE m_pBuffer; /* Pointer to the complete buffer */ + LONG m_lActual; /* Length of data in this sample */ + LONG m_cbBuffer; /* Size of the buffer */ + CBaseAllocator *m_pAllocator; /* The allocator who owns us */ + CMediaSample *m_pNext; /* Chaining in free list */ + REFERENCE_TIME m_Start; /* Start sample time */ + REFERENCE_TIME m_End; /* End sample time */ + LONGLONG m_MediaStart; /* Real media start position */ + LONG m_MediaEnd; /* A difference to get the end */ + AM_MEDIA_TYPE *m_pMediaType; /* Media type change data */ + DWORD m_dwStreamId; /* Stream id */ +public: + LONG m_cRef; /* Reference count */ + + +public: + + CMediaSample( + TCHAR *pName, + CBaseAllocator *pAllocator, + HRESULT *phr, + LPBYTE pBuffer = NULL, + LONG length = 0); +#ifdef UNICODE + CMediaSample( + CHAR *pName, + CBaseAllocator *pAllocator, + HRESULT *phr, + LPBYTE pBuffer = NULL, + LONG length = 0); +#endif + + virtual ~CMediaSample(); + + /* Note the media sample does not delegate to its owner */ + + STDMETHODIMP QueryInterface(REFIID riid, void **ppv); + STDMETHODIMP_(ULONG) AddRef(); + STDMETHODIMP_(ULONG) Release(); + + // set the buffer pointer and length. Used by allocators that + // want variable sized pointers or pointers into already-read data. + // This is only available through a CMediaSample* not an IMediaSample* + // and so cannot be changed by clients. + HRESULT SetPointer(BYTE * ptr, LONG cBytes); + + // Get me a read/write pointer to this buffer's memory. + STDMETHODIMP GetPointer(BYTE ** ppBuffer); + + STDMETHODIMP_(LONG) GetSize(void); + + // get the stream time at which this sample should start and finish. + STDMETHODIMP GetTime( + REFERENCE_TIME * pTimeStart, // put time here + REFERENCE_TIME * pTimeEnd + ); + + // Set the stream time at which this sample should start and finish. + STDMETHODIMP SetTime( + REFERENCE_TIME * pTimeStart, // put time here + REFERENCE_TIME * pTimeEnd + ); + STDMETHODIMP IsSyncPoint(void); + STDMETHODIMP SetSyncPoint(BOOL bIsSyncPoint); + STDMETHODIMP IsPreroll(void); + STDMETHODIMP SetPreroll(BOOL bIsPreroll); + + STDMETHODIMP_(LONG) GetActualDataLength(void); + STDMETHODIMP SetActualDataLength(LONG lActual); + + // these allow for limited format changes in band + + STDMETHODIMP GetMediaType(AM_MEDIA_TYPE **ppMediaType); + STDMETHODIMP SetMediaType(AM_MEDIA_TYPE *pMediaType); + + // returns S_OK if there is a discontinuity in the data (this same is + // not a continuation of the previous stream of data + // - there has been a seek). + STDMETHODIMP IsDiscontinuity(void); + // set the discontinuity property - TRUE if this sample is not a + // continuation, but a new sample after a seek. + STDMETHODIMP SetDiscontinuity(BOOL bDiscontinuity); + + // get the media times for this sample + STDMETHODIMP GetMediaTime( + LONGLONG * pTimeStart, + LONGLONG * pTimeEnd + ); + + // Set the media times for this sample + STDMETHODIMP SetMediaTime( + LONGLONG * pTimeStart, + LONGLONG * pTimeEnd + ); + + // Set and get properties (IMediaSample2) + STDMETHODIMP GetProperties( + DWORD cbProperties, + BYTE * pbProperties + ); + + STDMETHODIMP SetProperties( + DWORD cbProperties, + const BYTE * pbProperties + ); +}; + + +//===================================================================== +//===================================================================== +// Defines CBaseAllocator +// +// Abstract base class that manages a list of media samples +// +// This class provides support for getting buffers from the free list, +// including handling of commit and (asynchronous) decommit. +// +// Derive from this class and override the Alloc and Free functions to +// allocate your CMediaSample (or derived) objects and add them to the +// free list, preparing them as necessary. +//===================================================================== +//===================================================================== + +class AM_NOVTABLE CBaseAllocator : public CUnknown,// A non delegating IUnknown + public IMemAllocatorCallbackTemp, // The interface we support + public CCritSec // Provides object locking +{ + class CSampleList; + friend class CSampleList; + + /* Trick to get at protected member in CMediaSample */ + static CMediaSample * &NextSample(CMediaSample *pSample) + { + return pSample->m_pNext; + }; + + /* Mini list class for the free list */ + class CSampleList + { + public: + CSampleList() : m_List(NULL), m_nOnList(0) {}; +#ifdef DEBUG + ~CSampleList() + { + ASSERT(m_nOnList == 0); + }; +#endif + CMediaSample *Head() const { return m_List; }; + CMediaSample *Next(CMediaSample *pSample) const { return CBaseAllocator::NextSample(pSample); }; + int GetCount() const { return m_nOnList; }; + void Add(CMediaSample *pSample) + { + ASSERT(pSample != NULL); + CBaseAllocator::NextSample(pSample) = m_List; + m_List = pSample; + m_nOnList++; + }; + CMediaSample *RemoveHead() + { + CMediaSample *pSample = m_List; + if (pSample != NULL) { + m_List = CBaseAllocator::NextSample(m_List); + m_nOnList--; + } + return pSample; + }; + void Remove(CMediaSample *pSample); + + public: + CMediaSample *m_List; + int m_nOnList; + }; +protected: + + CSampleList m_lFree; // Free list + + /* Note to overriders of CBaseAllocator. + + We use a lazy signalling mechanism for waiting for samples. + This means we don't call the OS if no waits occur. + + In order to implement this: + + 1. When a new sample is added to m_lFree call NotifySample() which + calls ReleaseSemaphore on m_hSem with a count of m_lWaiting and + sets m_lWaiting to 0. + This must all be done holding the allocator's critical section. + + 2. When waiting for a sample call SetWaiting() which increments + m_lWaiting BEFORE leaving the allocator's critical section. + + 3. Actually wait by calling WaitForSingleObject(m_hSem, INFINITE) + having left the allocator's critical section. The effect of + this is to remove 1 from the semaphore's count. You MUST call + this once having incremented m_lWaiting. + + The following are then true when the critical section is not held : + (let nWaiting = number about to wait or waiting) + + (1) if (m_lFree.GetCount() != 0) then (m_lWaiting == 0) + (2) m_lWaiting + Semaphore count == nWaiting + + We would deadlock if + nWaiting != 0 && + m_lFree.GetCount() != 0 && + Semaphore count == 0 + + But from (1) if m_lFree.GetCount() != 0 then m_lWaiting == 0 so + from (2) Semaphore count == nWaiting (which is non-0) so the + deadlock can't happen. + */ + + HANDLE m_hSem; // For signalling + long m_lWaiting; // Waiting for a free element + long m_lCount; // how many buffers we have agreed to provide + long m_lAllocated; // how many buffers are currently allocated + long m_lSize; // agreed size of each buffer + long m_lAlignment; // agreed alignment + long m_lPrefix; // agreed prefix (preceeds GetPointer() value) + BOOL m_bChanged; // Have the buffer requirements changed + + // if true, we are decommitted and can't allocate memory + BOOL m_bCommitted; + // if true, the decommit has happened, but we haven't called Free yet + // as there are still outstanding buffers + BOOL m_bDecommitInProgress; + + // Notification interface + IMemAllocatorNotifyCallbackTemp *m_pNotify; + + BOOL m_fEnableReleaseCallback; + + // called to decommit the memory when the last buffer is freed + // pure virtual - need to override this + virtual void Free(void) PURE; + + // override to allocate the memory when commit called + virtual HRESULT Alloc(void); + +public: + + CBaseAllocator( + TCHAR *, LPUNKNOWN, HRESULT *, + BOOL bEvent = TRUE, BOOL fEnableReleaseCallback = FALSE); +#ifdef UNICODE + CBaseAllocator( + CHAR *, LPUNKNOWN, HRESULT *, + BOOL bEvent = TRUE, BOOL fEnableReleaseCallback = FALSE); +#endif + virtual ~CBaseAllocator(); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + STDMETHODIMP SetProperties( + ALLOCATOR_PROPERTIES* pRequest, + ALLOCATOR_PROPERTIES* pActual); + + // return the properties actually being used on this allocator + STDMETHODIMP GetProperties( + ALLOCATOR_PROPERTIES* pProps); + + // override Commit to allocate memory. We handle the GetBuffer + //state changes + STDMETHODIMP Commit(); + + // override this to handle the memory freeing. We handle any outstanding + // GetBuffer calls + STDMETHODIMP Decommit(); + + // get container for a sample. Blocking, synchronous call to get the + // next free buffer (as represented by an IMediaSample interface). + // on return, the time etc properties will be invalid, but the buffer + // pointer and size will be correct. The two time parameters are + // optional and either may be NULL, they may alternatively be set to + // the start and end times the sample will have attached to it + // bPrevFramesSkipped is not used (used only by the video renderer's + // allocator where it affects quality management in direct draw). + + STDMETHODIMP GetBuffer(IMediaSample **ppBuffer, + REFERENCE_TIME * pStartTime, + REFERENCE_TIME * pEndTime, + DWORD dwFlags); + + // final release of a CMediaSample will call this + STDMETHODIMP ReleaseBuffer(IMediaSample *pBuffer); + // obsolete:: virtual void PutOnFreeList(CMediaSample * pSample); + + STDMETHODIMP SetNotify(IMemAllocatorNotifyCallbackTemp *pNotify); + + STDMETHODIMP GetFreeCount(LONG *plBuffersFree); + + // Notify that a sample is available + void NotifySample(); + + // Notify that we're waiting for a sample + void SetWaiting() { m_lWaiting++; }; +}; + + +//===================================================================== +//===================================================================== +// Defines CMemAllocator +// +// this is an allocator based on CBaseAllocator that allocates sample +// buffers in main memory (from 'new'). You must call SetProperties +// before calling Commit. +// +// we don't free the memory when going into Decommit state. The simplest +// way to implement this without complicating CBaseAllocator is to +// have a Free() function, called to go into decommit state, that does +// nothing and a ReallyFree function called from our destructor that +// actually frees the memory. +//===================================================================== +//===================================================================== + +// Make me one from quartz.dll +STDAPI CreateMemoryAllocator(IMemAllocator **ppAllocator); + +class CMemAllocator : public CBaseAllocator +{ + +protected: + + LPBYTE m_pBuffer; // combined memory for all buffers + + // override to free the memory when decommit completes + // - we actually do nothing, and save the memory until deletion. + void Free(void); + + // called from the destructor (and from Alloc if changing size/count) to + // actually free up the memory + void ReallyFree(void); + + // overriden to allocate the memory when commit called + HRESULT Alloc(void); + +public: + /* This goes in the factory template table to create new instances */ + static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *); + + STDMETHODIMP SetProperties( + ALLOCATOR_PROPERTIES* pRequest, + ALLOCATOR_PROPERTIES* pActual); + + CMemAllocator(TCHAR *, LPUNKNOWN, HRESULT *); +#ifdef UNICODE + CMemAllocator(CHAR *, LPUNKNOWN, HRESULT *); +#endif + ~CMemAllocator(); +}; + +// helper used by IAMovieSetup implementation +STDAPI +AMovieSetupRegisterFilter( const AMOVIESETUP_FILTER * const psetupdata + , IFilterMapper * pIFM + , BOOL bRegister ); + + +/////////////////////////////////////////////////////////////////////////// +// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ +/////////////////////////////////////////////////////////////////////////// + +#endif /* __FILTER__ */ + + + diff --git a/plugins/GSdx/baseclasses/amvideo.cpp b/plugins/GSdx/baseclasses/amvideo.cpp new file mode 100644 index 0000000000..088a2e19e6 --- /dev/null +++ b/plugins/GSdx/baseclasses/amvideo.cpp @@ -0,0 +1,275 @@ +//------------------------------------------------------------------------------ +// File: AMVideo.cpp +// +// Desc: DirectShow base classes - implements helper functions for +// bitmap formats. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include + +// These are bit field masks for true colour devices + +const DWORD bits555[] = {0x007C00,0x0003E0,0x00001F}; +const DWORD bits565[] = {0x00F800,0x0007E0,0x00001F}; +const DWORD bits888[] = {0xFF0000,0x00FF00,0x0000FF}; + +// This maps bitmap subtypes into a bits per pixel value and also a +// name. unicode and ansi versions are stored because we have to +// return a pointer to a static string. +const struct { + const GUID *pSubtype; + WORD BitCount; + CHAR *pName; + WCHAR *wszName; +} BitCountMap[] = { &MEDIASUBTYPE_RGB1, 1, "RGB Monochrome", L"RGB Monochrome", + &MEDIASUBTYPE_RGB4, 4, "RGB VGA", L"RGB VGA", + &MEDIASUBTYPE_RGB8, 8, "RGB 8", L"RGB 8", + &MEDIASUBTYPE_RGB565, 16, "RGB 565 (16 bit)", L"RGB 565 (16 bit)", + &MEDIASUBTYPE_RGB555, 16, "RGB 555 (16 bit)", L"RGB 555 (16 bit)", + &MEDIASUBTYPE_RGB24, 24, "RGB 24", L"RGB 24", + &MEDIASUBTYPE_RGB32, 32, "RGB 32", L"RGB 32", + &MEDIASUBTYPE_ARGB32, 32, "ARGB 32", L"ARGB 32", + &MEDIASUBTYPE_Overlay, 0, "Overlay", L"Overlay", + &GUID_NULL, 0, "UNKNOWN", L"UNKNOWN" +}; + +// Return the size of the bitmap as defined by this header + +STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader) +{ + return DIBSIZE(*pHeader); +} + + +// This is called if the header has a 16 bit colour depth and needs to work +// out the detailed type from the bit fields (either RGB 565 or RGB 555) + +STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader) +{ + BITMAPINFO *pbmInfo = (BITMAPINFO *) pbmiHeader; + ASSERT(pbmiHeader->biBitCount == 16); + + // If its BI_RGB then it's RGB 555 by default + + if (pbmiHeader->biCompression == BI_RGB) { + return MEDIASUBTYPE_RGB555; + } + + // Compare the bit fields with RGB 555 + + DWORD *pMask = (DWORD *) pbmInfo->bmiColors; + if (pMask[0] == bits555[0]) { + if (pMask[1] == bits555[1]) { + if (pMask[2] == bits555[2]) { + return MEDIASUBTYPE_RGB555; + } + } + } + + // Compare the bit fields with RGB 565 + + pMask = (DWORD *) pbmInfo->bmiColors; + if (pMask[0] == bits565[0]) { + if (pMask[1] == bits565[1]) { + if (pMask[2] == bits565[2]) { + return MEDIASUBTYPE_RGB565; + } + } + } + return GUID_NULL; +} + + +// Given a BITMAPINFOHEADER structure this returns the GUID sub type that is +// used to describe it in format negotiations. For example a video codec fills +// in the format block with a VIDEOINFO structure, it also fills in the major +// type with MEDIATYPE_VIDEO and the subtype with a GUID that matches the bit +// count, for example if it is an eight bit image then MEDIASUBTYPE_RGB8 + +STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader) +{ + ASSERT(pbmiHeader); + + // If it's not RGB then create a GUID from the compression type + + if (pbmiHeader->biCompression != BI_RGB) { + if (pbmiHeader->biCompression != BI_BITFIELDS) { + FOURCCMap FourCCMap(pbmiHeader->biCompression); + return (const GUID) FourCCMap; + } + } + + // Map the RGB DIB bit depth to a image GUID + + switch(pbmiHeader->biBitCount) { + case 1 : return MEDIASUBTYPE_RGB1; + case 4 : return MEDIASUBTYPE_RGB4; + case 8 : return MEDIASUBTYPE_RGB8; + case 16 : return GetTrueColorType(pbmiHeader); + case 24 : return MEDIASUBTYPE_RGB24; + case 32 : return MEDIASUBTYPE_RGB32; + } + return GUID_NULL; +} + + +// Given a video bitmap subtype we return the number of bits per pixel it uses +// We return a WORD bit count as thats what the BITMAPINFOHEADER uses. If the +// GUID subtype is not found in the table we return an invalid USHRT_MAX + +STDAPI_(WORD) GetBitCount(const GUID *pSubtype) +{ + ASSERT(pSubtype); + const GUID *pMediaSubtype; + INT iPosition = 0; + + // Scan the mapping list seeing if the source GUID matches any known + // bitmap subtypes, the list is terminated by a GUID_NULL entry + + while (TRUE) { + pMediaSubtype = BitCountMap[iPosition].pSubtype; + if (IsEqualGUID(*pMediaSubtype,GUID_NULL)) { + return USHRT_MAX; + } + if (IsEqualGUID(*pMediaSubtype,*pSubtype)) { + return BitCountMap[iPosition].BitCount; + } + iPosition++; + } +} + + +// Given a bitmap subtype we return a description name that can be used for +// debug purposes. In a retail build this function still returns the names +// If the subtype isn't found in the lookup table we return string UNKNOWN + +int LocateSubtype(const GUID *pSubtype) +{ + ASSERT(pSubtype); + const GUID *pMediaSubtype; + INT iPosition = 0; + + // Scan the mapping list seeing if the source GUID matches any known + // bitmap subtypes, the list is terminated by a GUID_NULL entry + + while (TRUE) { + pMediaSubtype = BitCountMap[iPosition].pSubtype; + if (IsEqualGUID(*pMediaSubtype,*pSubtype) || + IsEqualGUID(*pMediaSubtype,GUID_NULL) + ) + { + break; + } + + iPosition++; + } + + return iPosition; +} + + + +STDAPI_(WCHAR *) GetSubtypeNameW(const GUID *pSubtype) +{ + return BitCountMap[LocateSubtype(pSubtype)].wszName; +} + +STDAPI_(CHAR *) GetSubtypeNameA(const GUID *pSubtype) +{ + return BitCountMap[LocateSubtype(pSubtype)].pName; +} + +#ifndef GetSubtypeName +#error wxutil.h should have defined GetSubtypeName +#endif +#undef GetSubtypeName + +// this is here for people that linked to it directly; most people +// would use the header file that picks the A or W version. +STDAPI_(CHAR *) GetSubtypeName(const GUID *pSubtype) +{ + return GetSubtypeNameA(pSubtype); +} + + +// The mechanism for describing a bitmap format is with the BITMAPINFOHEADER +// This is really messy to deal with because it invariably has fields that +// follow it holding bit fields, palettes and the rest. This function gives +// the number of bytes required to hold a VIDEOINFO that represents it. This +// count includes the prefix information (like the rcSource rectangle) the +// BITMAPINFOHEADER field, and any other colour information on the end. +// +// WARNING If you want to copy a BITMAPINFOHEADER into a VIDEOINFO always make +// sure that you use the HEADER macro because the BITMAPINFOHEADER field isn't +// right at the start of the VIDEOINFO (there are a number of other fields), +// +// CopyMemory(HEADER(pVideoInfo),pbmi,sizeof(BITMAPINFOHEADER)); +// + +STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader) +{ + // Everyone has this to start with this + LONG Size = SIZE_PREHEADER + pHeader->biSize; + + ASSERT(pHeader->biSize >= sizeof(BITMAPINFOHEADER)); + + // Does this format use a palette, if the number of colours actually used + // is zero then it is set to the maximum that are allowed for that colour + // depth (an example is 256 for eight bits). Truecolour formats may also + // pass a palette with them in which case the used count is non zero + + // This would scare me. + ASSERT(pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed == 0); + + if (pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed) { + LONG Entries = (DWORD) 1 << pHeader->biBitCount; + if (pHeader->biClrUsed) { + Entries = pHeader->biClrUsed; + } + Size += Entries * sizeof(RGBQUAD); + } + + // Truecolour formats may have a BI_BITFIELDS specifier for compression + // type which means that room for three DWORDs should be allocated that + // specify where in each pixel the RGB colour components may be found + + if (pHeader->biCompression == BI_BITFIELDS) { + Size += SIZE_MASKS; + } + + // A BITMAPINFO for a palettised image may also contain a palette map that + // provides the information to map from a source palette to a destination + // palette during a BitBlt for example, because this information is only + // ever processed during drawing you don't normally store the palette map + // nor have any way of knowing if it is present in the data structure + + return Size; +} + + +// Returns TRUE if the VIDEOINFO contains a palette + +STDAPI_(BOOL) ContainsPalette(const VIDEOINFOHEADER *pVideoInfo) +{ + if (PALETTISED(pVideoInfo) == FALSE) { + if (pVideoInfo->bmiHeader.biClrUsed == 0) { + return FALSE; + } + } + return TRUE; +} + + +// Return a pointer to the first entry in a palette + +STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFOHEADER *pVideoInfo) +{ + if (pVideoInfo->bmiHeader.biCompression == BI_BITFIELDS) { + return TRUECOLOR(pVideoInfo)->bmiColors; + } + return COLORS(pVideoInfo); +} diff --git a/plugins/GSdx/baseclasses/cache.h b/plugins/GSdx/baseclasses/cache.h new file mode 100644 index 0000000000..d98f262f9c --- /dev/null +++ b/plugins/GSdx/baseclasses/cache.h @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// File: Cache.h +// +// Desc: DirectShow base classes - efines a non-MFC generic cache class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +/* This class implements a simple cache. A cache object is instantiated + with the number of items it is to hold. An item is a pointer to an + object derived from CBaseObject (helps reduce memory leaks). The cache + can then have objects added to it and removed from it. The cache size + is fixed at construction time and may therefore run out or be flooded. + If it runs out it returns a NULL pointer, if it fills up it also returns + a NULL pointer instead of a pointer to the object just inserted */ + +/* Making these classes inherit from CBaseObject does nothing for their + functionality but it allows us to check there are no memory leaks */ + +/* WARNING Be very careful when using this class, what it lets you do is + store and retrieve objects so that you can minimise object creation + which in turns improves efficiency. However the object you store is + exactly the same as the object you get back which means that it short + circuits the constructor initialisation phase. This means any class + variables the object has (eg pointers) are highly likely to be invalid. + Therefore ensure you reinitialise the object before using it again */ + + +#ifndef __CACHE__ +#define __CACHE__ + + +class CCache : CBaseObject { + + /* Make copy constructor and assignment operator inaccessible */ + + CCache(const CCache &refCache); + CCache &operator=(const CCache &refCache); + +private: + + /* These are initialised in the constructor. The first variable points to + an array of pointers, each of which points to a CBaseObject derived + object. The m_iCacheSize is the static fixed size for the cache and the + m_iUsed defines the number of places filled with objects at any time. + We fill the array of pointers from the start (ie m_ppObjects[0] first) + and then only add and remove objects from the end position, so in this + respect the array of object pointers should be treated as a stack */ + + CBaseObject **m_ppObjects; + const INT m_iCacheSize; + INT m_iUsed; + +public: + + CCache(TCHAR *pName,INT iItems); + virtual ~CCache(); + + /* Add an item to the cache */ + CBaseObject *AddToCache(CBaseObject *pObject); + + /* Remove an item from the cache */ + CBaseObject *RemoveFromCache(); + + /* Delete all the objects held in the cache */ + void RemoveAll(void); + + /* Return the cache size which is set during construction */ + INT GetCacheSize(void) const {return m_iCacheSize;}; +}; + +#endif /* __CACHE__ */ + diff --git a/plugins/GSdx/baseclasses/combase.cpp b/plugins/GSdx/baseclasses/combase.cpp new file mode 100644 index 0000000000..70a33ecae0 --- /dev/null +++ b/plugins/GSdx/baseclasses/combase.cpp @@ -0,0 +1,256 @@ +//------------------------------------------------------------------------------ +// File: ComBase.cpp +// +// Desc: DirectShow base classes - implements class hierarchy for creating +// COM objects. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#pragma warning( disable : 4514 ) // Disable warnings re unused inline functions + + +/* Define the static member variable */ + +LONG CBaseObject::m_cObjects = 0; + + +/* Constructor */ + +CBaseObject::CBaseObject(const TCHAR *pName) +{ + /* Increment the number of active objects */ + InterlockedIncrement(&m_cObjects); + +#ifdef DEBUG + +#ifdef UNICODE + m_dwCookie = DbgRegisterObjectCreation(0, pName); +#else + m_dwCookie = DbgRegisterObjectCreation(pName, 0); +#endif + +#endif +} + +#ifdef UNICODE +CBaseObject::CBaseObject(const char *pName) +{ + /* Increment the number of active objects */ + InterlockedIncrement(&m_cObjects); + +#ifdef DEBUG + m_dwCookie = DbgRegisterObjectCreation(pName, 0); +#endif +} +#endif + +HINSTANCE hlibOLEAut32; + +/* Destructor */ + +CBaseObject::~CBaseObject() +{ + /* Decrement the number of objects active */ + if (InterlockedDecrement(&m_cObjects) == 0) { + if (hlibOLEAut32) { + FreeLibrary(hlibOLEAut32); + + hlibOLEAut32 = 0; + } + }; + + +#ifdef DEBUG + DbgRegisterObjectDestruction(m_dwCookie); +#endif +} + +static const TCHAR szOle32Aut[] = TEXT("OleAut32.dll"); + +HINSTANCE LoadOLEAut32() +{ + if (hlibOLEAut32 == 0) { + + hlibOLEAut32 = LoadLibrary(szOle32Aut); + } + + return hlibOLEAut32; +} + + +/* Constructor */ + +// We know we use "this" in the initialization list, we also know we don't modify *phr. +#pragma warning( disable : 4355 4100 ) +CUnknown::CUnknown(const TCHAR *pName, LPUNKNOWN pUnk) +: CBaseObject(pName) +/* Start the object with a reference count of zero - when the */ +/* object is queried for it's first interface this may be */ +/* incremented depending on whether or not this object is */ +/* currently being aggregated upon */ +, m_cRef(0) +/* Set our pointer to our IUnknown interface. */ +/* If we have an outer, use its, otherwise use ours. */ +/* This pointer effectivly points to the owner of */ +/* this object and can be accessed by the GetOwner() method. */ +, m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast( static_cast(this) ) ) + /* Why the double cast? Well, the inner cast is a type-safe cast */ + /* to pointer to a type from which we inherit. The second is */ + /* type-unsafe but works because INonDelegatingUnknown "behaves */ + /* like" IUnknown. (Only the names on the methods change.) */ +{ + // Everything we need to do has been done in the initializer list +} + +// This does the same as above except it has a useless HRESULT argument +// use the previous constructor, this is just left for compatibility... +CUnknown::CUnknown(TCHAR *pName, LPUNKNOWN pUnk,HRESULT *phr) : + CBaseObject(pName), + m_cRef(0), + m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast( static_cast(this) ) ) +{ +} + +#ifdef UNICODE +CUnknown::CUnknown(const CHAR *pName, LPUNKNOWN pUnk) +: CBaseObject(pName), m_cRef(0), + m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast( static_cast(this) ) ) +{ } + +CUnknown::CUnknown(CHAR *pName, LPUNKNOWN pUnk,HRESULT *phr) : + CBaseObject(pName), m_cRef(0), + m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast( static_cast(this) ) ) +{ } + +#endif + +#pragma warning( default : 4355 4100 ) + + +/* QueryInterface */ + +STDMETHODIMP CUnknown::NonDelegatingQueryInterface(REFIID riid, void ** ppv) +{ + CheckPointer(ppv,E_POINTER); + ValidateReadWritePtr(ppv,sizeof(PVOID)); + + /* We know only about IUnknown */ + + if (riid == IID_IUnknown) { + GetInterface((LPUNKNOWN) (PNDUNKNOWN) this, ppv); + return NOERROR; + } else { + *ppv = NULL; + return E_NOINTERFACE; + } +} + +/* We have to ensure that we DON'T use a max macro, since these will typically */ +/* lead to one of the parameters being evaluated twice. Since we are worried */ +/* about concurrency, we can't afford to access the m_cRef twice since we can't */ +/* afford to run the risk that its value having changed between accesses. */ + +template inline static T ourmax( const T & a, const T & b ) +{ + return a > b ? a : b; +} + +/* AddRef */ + +STDMETHODIMP_(ULONG) CUnknown::NonDelegatingAddRef() +{ + LONG lRef = InterlockedIncrement( &m_cRef ); + ASSERT(lRef > 0); + DbgLog((LOG_MEMORY,3,TEXT(" Obj %d ref++ = %d"), + m_dwCookie, m_cRef)); + return ourmax(ULONG(m_cRef), 1ul); +} + + +/* Release */ + +STDMETHODIMP_(ULONG) CUnknown::NonDelegatingRelease() +{ + /* If the reference count drops to zero delete ourselves */ + + LONG lRef = InterlockedDecrement( &m_cRef ); + ASSERT(lRef >= 0); + + DbgLog((LOG_MEMORY,3,TEXT(" Object %d ref-- = %d"), + m_dwCookie, m_cRef)); + if (lRef == 0) { + + // COM rules say we must protect against re-entrancy. + // If we are an aggregator and we hold our own interfaces + // on the aggregatee, the QI for these interfaces will + // addref ourselves. So after doing the QI we must release + // a ref count on ourselves. Then, before releasing the + // private interface, we must addref ourselves. When we do + // this from the destructor here it will result in the ref + // count going to 1 and then back to 0 causing us to + // re-enter the destructor. Hence we add an extra refcount here + // once we know we will delete the object. + // for an example aggregator see filgraph\distrib.cpp. + + m_cRef++; + + delete this; + return ULONG(0); + } else { + return ourmax(ULONG(m_cRef), 1ul); + } +} + + +/* Return an interface pointer to a requesting client + performing a thread safe AddRef as necessary */ + +STDAPI GetInterface(LPUNKNOWN pUnk, void **ppv) +{ + CheckPointer(ppv, E_POINTER); + *ppv = pUnk; + pUnk->AddRef(); + return NOERROR; +} + + +/* Compares two interfaces and returns TRUE if they are on the same object */ + +BOOL WINAPI IsEqualObject(IUnknown *pFirst, IUnknown *pSecond) +{ + /* Different objects can't have the same interface pointer for + any interface + */ + if (pFirst == pSecond) { + return TRUE; + } + /* OK - do it the hard way - check if they have the same + IUnknown pointers - a single object can only have one of these + */ + LPUNKNOWN pUnknown1; // Retrieve the IUnknown interface + LPUNKNOWN pUnknown2; // Retrieve the other IUnknown interface + HRESULT hr; // General OLE return code + + ASSERT(pFirst); + ASSERT(pSecond); + + /* See if the IUnknown pointers match */ + + hr = pFirst->QueryInterface(IID_IUnknown,(void **) &pUnknown1); + ASSERT(SUCCEEDED(hr)); + ASSERT(pUnknown1); + + hr = pSecond->QueryInterface(IID_IUnknown,(void **) &pUnknown2); + ASSERT(SUCCEEDED(hr)); + ASSERT(pUnknown2); + + /* Release the extra interfaces we hold */ + + pUnknown1->Release(); + pUnknown2->Release(); + return (pUnknown1 == pUnknown2); +} + diff --git a/plugins/GSdx/baseclasses/combase.h b/plugins/GSdx/baseclasses/combase.h new file mode 100644 index 0000000000..34d5ca65cd --- /dev/null +++ b/plugins/GSdx/baseclasses/combase.h @@ -0,0 +1,319 @@ +//------------------------------------------------------------------------------ +// File: ComBase.h +// +// Desc: DirectShow base classes - defines a class hierarchy for creating +// COM objects. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +/* + +a. Derive your COM object from CUnknown + +b. Make a static CreateInstance function that takes an LPUNKNOWN, an HRESULT * + and a TCHAR *. The LPUNKNOWN defines the object to delegate IUnknown calls + to. The HRESULT * allows error codes to be passed around constructors and + the TCHAR * is a descriptive name that can be printed on the debugger. + + It is important that constructors only change the HRESULT * if they have + to set an ERROR code, if it was successful then leave it alone or you may + overwrite an error code from an object previously created. + + When you call a constructor the descriptive name should be in static store + as we do not copy the string. To stop large amounts of memory being used + in retail builds by all these static strings use the NAME macro, + + CMyFilter = new CImplFilter(NAME("My filter"),pUnknown,phr); + if (FAILED(hr)) { + return hr; + } + + In retail builds NAME(_x_) compiles to NULL, the base CBaseObject class + knows not to do anything with objects that don't have a name. + +c. Have a constructor for your object that passes the LPUNKNOWN, HRESULT * and + TCHAR * to the CUnknown constructor. You can set the HRESULT if you have an + error, or just simply pass it through to the constructor. + + The object creation will fail in the class factory if the HRESULT indicates + an error (ie FAILED(HRESULT) == TRUE) + +d. Create a FactoryTemplate with your object's class id and CreateInstance + function. + +Then (for each interface) either + +Multiple inheritance + +1. Also derive it from ISomeInterface +2. Include DECLARE_IUNKNOWN in your class definition to declare + implementations of QueryInterface, AddRef and Release that + call the outer unknown +3. Override NonDelegatingQueryInterface to expose ISomeInterface by + code something like + + if (riid == IID_ISomeInterface) { + return GetInterface((ISomeInterface *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } + +4. Declare and implement the member functions of ISomeInterface. + +or: Nested interfaces + +1. Declare a class derived from CUnknown +2. Include DECLARE_IUNKNOWN in your class definition +3. Override NonDelegatingQueryInterface to expose ISomeInterface by + code something like + + if (riid == IID_ISomeInterface) { + return GetInterface((ISomeInterface *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } + +4. Implement the member functions of ISomeInterface. Use GetOwner() to + access the COM object class. + +And in your COM object class: + +5. Make the nested class a friend of the COM object class, and declare + an instance of the nested class as a member of the COM object class. + + NOTE that because you must always pass the outer unknown and an hResult + to the CUnknown constructor you cannot use a default constructor, in + other words you will have to make the member variable a pointer to the + class and make a NEW call in your constructor to actually create it. + +6. override the NonDelegatingQueryInterface with code like this: + + if (riid == IID_ISomeInterface) { + return m_pImplFilter-> + NonDelegatingQueryInterface(IID_ISomeInterface, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } + +You can have mixed classes which support some interfaces via multiple +inheritance and some via nested classes + +*/ + +#ifndef __COMBASE__ +#define __COMBASE__ + +// Filter Setup data structures no defined in axextend.idl + +typedef REGPINTYPES +AMOVIESETUP_MEDIATYPE, * PAMOVIESETUP_MEDIATYPE, * FAR LPAMOVIESETUP_MEDIATYPE; + +typedef REGFILTERPINS +AMOVIESETUP_PIN, * PAMOVIESETUP_PIN, * FAR LPAMOVIESETUP_PIN; + +typedef struct _AMOVIESETUP_FILTER +{ + const CLSID * clsID; + const WCHAR * strName; + DWORD dwMerit; + UINT nPins; + const AMOVIESETUP_PIN * lpPin; +} +AMOVIESETUP_FILTER, * PAMOVIESETUP_FILTER, * FAR LPAMOVIESETUP_FILTER; + +/* The DLLENTRY module initialises the module handle on loading */ + +extern HINSTANCE g_hInst; + +/* On DLL load remember which platform we are running on */ + +extern DWORD g_amPlatform; +extern OSVERSIONINFO g_osInfo; // Filled in by GetVersionEx + +/* Version of IUnknown that is renamed to allow a class to support both + non delegating and delegating IUnknowns in the same COM object */ + +#ifndef INONDELEGATINGUNKNOWN_DEFINED +DECLARE_INTERFACE(INonDelegatingUnknown) +{ + STDMETHOD(NonDelegatingQueryInterface) (THIS_ REFIID, LPVOID *) PURE; + STDMETHOD_(ULONG, NonDelegatingAddRef)(THIS) PURE; + STDMETHOD_(ULONG, NonDelegatingRelease)(THIS) PURE; +}; +#define INONDELEGATINGUNKNOWN_DEFINED +#endif + +typedef INonDelegatingUnknown *PNDUNKNOWN; + + +/* This is the base object class that supports active object counting. As + part of the debug facilities we trace every time a C++ object is created + or destroyed. The name of the object has to be passed up through the class + derivation list during construction as you cannot call virtual functions + in the constructor. The downside of all this is that every single object + constructor has to take an object name parameter that describes it */ + +class CBaseObject +{ + +private: + + // Disable the copy constructor and assignment by default so you will get + // compiler errors instead of unexpected behaviour if you pass objects + // by value or assign objects. + CBaseObject(const CBaseObject& objectSrc); // no implementation + void operator=(const CBaseObject& objectSrc); // no implementation + +private: + static LONG m_cObjects; /* Total number of objects active */ + +protected: +#ifdef DEBUG + DWORD m_dwCookie; /* Cookie identifying this object */ +#endif + + +public: + + /* These increment and decrement the number of active objects */ + + CBaseObject(const TCHAR *pName); +#ifdef UNICODE + CBaseObject(const char *pName); +#endif + ~CBaseObject(); + + /* Call this to find if there are any CUnknown derived objects active */ + + static LONG ObjectsActive() { + return m_cObjects; + }; +}; + + +/* An object that supports one or more COM interfaces will be based on + this class. It supports counting of total objects for DLLCanUnloadNow + support, and an implementation of the core non delegating IUnknown */ + +class AM_NOVTABLE CUnknown : public INonDelegatingUnknown, + public CBaseObject +{ +private: + const LPUNKNOWN m_pUnknown; /* Owner of this object */ + +protected: /* So we can override NonDelegatingRelease() */ + volatile LONG m_cRef; /* Number of reference counts */ + +public: + + CUnknown(const TCHAR *pName, LPUNKNOWN pUnk); + virtual ~CUnknown() {}; + + // This is redundant, just use the other constructor + // as we never touch the HRESULT in this anyway + CUnknown(TCHAR *pName, LPUNKNOWN pUnk,HRESULT *phr); +#ifdef UNICODE + CUnknown(const char *pName, LPUNKNOWN pUnk); + CUnknown(char *pName, LPUNKNOWN pUnk,HRESULT *phr); +#endif + + /* Return the owner of this object */ + + LPUNKNOWN GetOwner() const { + return m_pUnknown; + }; + + /* Called from the class factory to create a new instance, it is + pure virtual so it must be overriden in your derived class */ + + /* static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *) */ + + /* Non delegating unknown implementation */ + + STDMETHODIMP NonDelegatingQueryInterface(REFIID, void **); + STDMETHODIMP_(ULONG) NonDelegatingAddRef(); + STDMETHODIMP_(ULONG) NonDelegatingRelease(); +}; + +#if (_MSC_VER <= 1200) +#pragma warning(disable:4211) + +/* The standard InterlockedXXX functions won't take volatiles */ +static inline LONG WINAPI InterlockedIncrement( volatile LONG * plong ) +{ return InterlockedIncrement( const_cast( plong ) ); } + +static inline LONG WINAPI InterlockedDecrement( volatile LONG * plong ) +{ return InterlockedDecrement( const_cast( plong ) ); } + +#pragma warning(default:4211) +#endif + + +/* Return an interface pointer to a requesting client + performing a thread safe AddRef as necessary */ + +STDAPI GetInterface(LPUNKNOWN pUnk, void **ppv); + +/* A function that can create a new COM object */ + +typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(LPUNKNOWN pUnkOuter, HRESULT *phr); + +/* A function (can be NULL) which is called from the DLL entrypoint + routine for each factory template: + + bLoading - TRUE on DLL load, FALSE on DLL unload + rclsid - the m_ClsID of the entry +*/ +typedef void (CALLBACK *LPFNInitRoutine)(BOOL bLoading, const CLSID *rclsid); + +/* Create one of these per object class in an array so that + the default class factory code can create new instances */ + +class CFactoryTemplate { + +public: + + const WCHAR * m_Name; + const CLSID * m_ClsID; + LPFNNewCOMObject m_lpfnNew; + LPFNInitRoutine m_lpfnInit; + const AMOVIESETUP_FILTER * m_pAMovieSetup_Filter; + + BOOL IsClassID(REFCLSID rclsid) const { + return (IsEqualCLSID(*m_ClsID,rclsid)); + }; + + CUnknown *CreateInstance(LPUNKNOWN pUnk, HRESULT *phr) const { + CheckPointer(phr,NULL); + return m_lpfnNew(pUnk, phr); + }; +}; + + +/* You must override the (pure virtual) NonDelegatingQueryInterface to return + interface pointers (using GetInterface) to the interfaces your derived + class supports (the default implementation only supports IUnknown) */ + +#define DECLARE_IUNKNOWN \ + STDMETHODIMP QueryInterface(REFIID riid, void **ppv) { \ + return GetOwner()->QueryInterface(riid,ppv); \ + }; \ + STDMETHODIMP_(ULONG) AddRef() { \ + return GetOwner()->AddRef(); \ + }; \ + STDMETHODIMP_(ULONG) Release() { \ + return GetOwner()->Release(); \ + }; + + + +HINSTANCE LoadOLEAut32(); + + +#endif /* __COMBASE__ */ + + + + diff --git a/plugins/GSdx/baseclasses/ctlutil.cpp b/plugins/GSdx/baseclasses/ctlutil.cpp new file mode 100644 index 0000000000..1a623cf943 --- /dev/null +++ b/plugins/GSdx/baseclasses/ctlutil.cpp @@ -0,0 +1,2531 @@ +//------------------------------------------------------------------------------ +// File: CtlUtil.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// Base classes implementing IDispatch parsing for the basic control dual +// interfaces. Derive from these and implement just the custom method and +// property methods. We also implement CPosPassThru that can be used by +// renderers and transforms to pass by IMediaPosition and IMediaSeeking + + +#include "streams.h" +#include +#include "seekpt.h" + +// 'bool' non standard reserved word +#pragma warning(disable:4237) + + +// --- CBaseDispatch implementation ---------- +CBaseDispatch::~CBaseDispatch() +{ + if (m_pti) { + m_pti->Release(); + } +} + + +// return 1 if we support GetTypeInfo + +STDMETHODIMP +CBaseDispatch::GetTypeInfoCount(UINT * pctinfo) +{ + CheckPointer(pctinfo,E_POINTER); + ValidateReadWritePtr(pctinfo,sizeof(UINT *)); + *pctinfo = 1; + return S_OK; +} + + +typedef HRESULT (STDAPICALLTYPE *LPLOADTYPELIB)( + const OLECHAR FAR *szFile, + ITypeLib FAR* FAR* pptlib); + +typedef HRESULT (STDAPICALLTYPE *LPLOADREGTYPELIB)(REFGUID rguid, + WORD wVerMajor, + WORD wVerMinor, + LCID lcid, + ITypeLib FAR* FAR* pptlib); + +// attempt to find our type library + +STDMETHODIMP +CBaseDispatch::GetTypeInfo( + REFIID riid, + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + CheckPointer(pptinfo,E_POINTER); + ValidateReadWritePtr(pptinfo,sizeof(ITypeInfo *)); + HRESULT hr; + + *pptinfo = NULL; + + // we only support one type element + if (0 != itinfo) { + return TYPE_E_ELEMENTNOTFOUND; + } + + if (NULL == pptinfo) { + return E_POINTER; + } + + // always look for neutral + if (NULL == m_pti) { + + LPLOADTYPELIB lpfnLoadTypeLib; + LPLOADREGTYPELIB lpfnLoadRegTypeLib; + ITypeLib *ptlib; + HINSTANCE hInst; + + static const char szTypeLib[] = "LoadTypeLib"; + static const char szRegTypeLib[] = "LoadRegTypeLib"; + static const WCHAR szControl[] = L"control.tlb"; + + // + // Try to get the Ole32Aut.dll module handle. + // + + hInst = LoadOLEAut32(); + if (hInst == NULL) { + DWORD dwError = GetLastError(); + return AmHresultFromWin32(dwError); + } + lpfnLoadRegTypeLib = (LPLOADREGTYPELIB)GetProcAddress(hInst, + szRegTypeLib); + if (lpfnLoadRegTypeLib == NULL) { + DWORD dwError = GetLastError(); + return AmHresultFromWin32(dwError); + } + + hr = (*lpfnLoadRegTypeLib)(LIBID_QuartzTypeLib, 1, 0, // version 1.0 + lcid, &ptlib); + + if (FAILED(hr)) { + + // attempt to load directly - this will fill the + // registry in if it finds it + + lpfnLoadTypeLib = (LPLOADTYPELIB)GetProcAddress(hInst, szTypeLib); + if (lpfnLoadTypeLib == NULL) { + DWORD dwError = GetLastError(); + return AmHresultFromWin32(dwError); + } + + hr = (*lpfnLoadTypeLib)(szControl, &ptlib); + if (FAILED(hr)) { + return hr; + } + } + + hr = ptlib->GetTypeInfoOfGuid( + riid, + &m_pti); + + ptlib->Release(); + + if (FAILED(hr)) { + return hr; + } + } + + *pptinfo = m_pti; + m_pti->AddRef(); + return S_OK; +} + + +STDMETHODIMP +CBaseDispatch::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + // although the IDispatch riid is dead, we use this to pass from + // the interface implementation class to us the iid we are talking about. + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(riid, 0, lcid, &pti); + + if (SUCCEEDED(hr)) { + hr = pti->GetIDsOfNames(rgszNames, cNames, rgdispid); + + pti->Release(); + } + return hr; +} + + +// --- CMediaControl implementation --------- + +CMediaControl::CMediaControl(const TCHAR * name,LPUNKNOWN pUnk) : + CUnknown(name, pUnk) +{ +} + +// expose our interfaces IMediaControl and IUnknown + +STDMETHODIMP +CMediaControl::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IMediaControl) { + return GetInterface( (IMediaControl *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +// return 1 if we support GetTypeInfo + +STDMETHODIMP +CMediaControl::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +// attempt to find our type library + +STDMETHODIMP +CMediaControl::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IMediaControl, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CMediaControl::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IMediaControl, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CMediaControl::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IMediaControl *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- CMediaEvent implementation ---------- + + +CMediaEvent::CMediaEvent(const TCHAR * name,LPUNKNOWN pUnk) : + CUnknown(name, pUnk) +{ +} + + +// expose our interfaces IMediaEvent and IUnknown + +STDMETHODIMP +CMediaEvent::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IMediaEvent || riid == IID_IMediaEventEx) { + return GetInterface( (IMediaEventEx *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +// return 1 if we support GetTypeInfo + +STDMETHODIMP +CMediaEvent::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +// attempt to find our type library + +STDMETHODIMP +CMediaEvent::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IMediaEvent, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CMediaEvent::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IMediaEvent, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CMediaEvent::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IMediaEvent *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- CMediaPosition implementation ---------- + + +CMediaPosition::CMediaPosition(const TCHAR * name,LPUNKNOWN pUnk) : + CUnknown(name, pUnk) +{ +} + +CMediaPosition::CMediaPosition(const TCHAR * name, + LPUNKNOWN pUnk, + HRESULT * phr) : + CUnknown(name, pUnk) +{ + UNREFERENCED_PARAMETER(phr); +} + + +// expose our interfaces IMediaPosition and IUnknown + +STDMETHODIMP +CMediaPosition::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IMediaPosition) { + return GetInterface( (IMediaPosition *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +// return 1 if we support GetTypeInfo + +STDMETHODIMP +CMediaPosition::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +// attempt to find our type library + +STDMETHODIMP +CMediaPosition::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IMediaPosition, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CMediaPosition::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IMediaPosition, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CMediaPosition::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IMediaPosition *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- IMediaPosition and IMediaSeeking pass through class ---------- + + +CPosPassThru::CPosPassThru(const TCHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr, + IPin *pPin) : + CMediaPosition(pName,pUnk), + m_pPin(pPin) +{ + if (pPin == NULL) { + *phr = E_POINTER; + return; + } +} + + +// Expose our IMediaSeeking and IMediaPosition interfaces + +STDMETHODIMP +CPosPassThru::NonDelegatingQueryInterface(REFIID riid,void **ppv) +{ + CheckPointer(ppv,E_POINTER); + *ppv = NULL; + + if (riid == IID_IMediaSeeking) { + return GetInterface( static_cast(this), ppv); + } + return CMediaPosition::NonDelegatingQueryInterface(riid,ppv); +} + + +// Return the IMediaPosition interface from our peer + +HRESULT +CPosPassThru::GetPeer(IMediaPosition ** ppMP) +{ + *ppMP = NULL; + + IPin *pConnected; + HRESULT hr = m_pPin->ConnectedTo(&pConnected); + if (FAILED(hr)) { + return E_NOTIMPL; + } + IMediaPosition * pMP; + hr = pConnected->QueryInterface(IID_IMediaPosition, (void **) &pMP); + pConnected->Release(); + if (FAILED(hr)) { + return E_NOTIMPL; + } + + *ppMP = pMP; + return S_OK; +} + + +// Return the IMediaSeeking interface from our peer + +HRESULT +CPosPassThru::GetPeerSeeking(IMediaSeeking ** ppMS) +{ + *ppMS = NULL; + + IPin *pConnected; + HRESULT hr = m_pPin->ConnectedTo(&pConnected); + if (FAILED(hr)) { + return E_NOTIMPL; + } + IMediaSeeking * pMS; + hr = pConnected->QueryInterface(IID_IMediaSeeking, (void **) &pMS); + pConnected->Release(); + if (FAILED(hr)) { + return E_NOTIMPL; + } + + *ppMS = pMS; + return S_OK; +} + + +// --- IMediaSeeking methods ---------- + + +STDMETHODIMP +CPosPassThru::GetCapabilities(DWORD * pCaps) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->GetCapabilities(pCaps); + pMS->Release(); + return hr; +} + +STDMETHODIMP +CPosPassThru::CheckCapabilities(DWORD * pCaps) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->CheckCapabilities(pCaps); + pMS->Release(); + return hr; +} + +STDMETHODIMP +CPosPassThru::IsFormatSupported(const GUID * pFormat) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->IsFormatSupported(pFormat); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::QueryPreferredFormat(GUID *pFormat) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->QueryPreferredFormat(pFormat); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::SetTimeFormat(const GUID * pFormat) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->SetTimeFormat(pFormat); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::GetTimeFormat(GUID *pFormat) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->GetTimeFormat(pFormat); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::IsUsingTimeFormat(const GUID * pFormat) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->IsUsingTimeFormat(pFormat); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::ConvertTimeFormat(LONGLONG * pTarget, const GUID * pTargetFormat, + LONGLONG Source, const GUID * pSourceFormat ) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->ConvertTimeFormat(pTarget, pTargetFormat, Source, pSourceFormat ); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::SetPositions( LONGLONG * pCurrent, DWORD CurrentFlags + , LONGLONG * pStop, DWORD StopFlags ) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->SetPositions(pCurrent, CurrentFlags, pStop, StopFlags ); + pMS->Release(); + return hr; +} + +STDMETHODIMP +CPosPassThru::GetPositions(LONGLONG *pCurrent, LONGLONG * pStop) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->GetPositions(pCurrent,pStop); + pMS->Release(); + return hr; +} + +HRESULT +CPosPassThru::GetSeekingLongLong +( HRESULT (__stdcall IMediaSeeking::*pMethod)( LONGLONG * ) +, LONGLONG * pll +) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (SUCCEEDED(hr)) + { + hr = (pMS->*pMethod)(pll); + pMS->Release(); + } + return hr; +} + +// If we don't have a current position then ask upstream + +STDMETHODIMP +CPosPassThru::GetCurrentPosition(LONGLONG *pCurrent) +{ + // Can we report the current position + HRESULT hr = GetMediaTime(pCurrent,NULL); + if (SUCCEEDED(hr)) hr = NOERROR; + else hr = GetSeekingLongLong( &IMediaSeeking::GetCurrentPosition, pCurrent ); + return hr; +} + + +STDMETHODIMP +CPosPassThru::GetStopPosition(LONGLONG *pStop) +{ + return GetSeekingLongLong( &IMediaSeeking::GetStopPosition, pStop );; +} + +STDMETHODIMP +CPosPassThru::GetDuration(LONGLONG *pDuration) +{ + return GetSeekingLongLong( &IMediaSeeking::GetDuration, pDuration );; +} + + +STDMETHODIMP +CPosPassThru::GetPreroll(LONGLONG *pllPreroll) +{ + return GetSeekingLongLong( &IMediaSeeking::GetPreroll, pllPreroll );; +} + + +STDMETHODIMP +CPosPassThru::GetAvailable( LONGLONG *pEarliest, LONGLONG *pLatest ) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + + hr = pMS->GetAvailable( pEarliest, pLatest ); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::GetRate(double * pdRate) +{ + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + hr = pMS->GetRate(pdRate); + pMS->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::SetRate(double dRate) +{ + if (0.0 == dRate) { + return E_INVALIDARG; + } + + IMediaSeeking* pMS; + HRESULT hr = GetPeerSeeking(&pMS); + if (FAILED(hr)) { + return hr; + } + hr = pMS->SetRate(dRate); + pMS->Release(); + return hr; +} + + + + +// --- IMediaPosition methods ---------- + + +STDMETHODIMP +CPosPassThru::get_Duration(REFTIME * plength) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + + hr = pMP->get_Duration(plength); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::get_CurrentPosition(REFTIME * pllTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->get_CurrentPosition(pllTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::put_CurrentPosition(REFTIME llTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->put_CurrentPosition(llTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::get_StopTime(REFTIME * pllTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->get_StopTime(pllTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::put_StopTime(REFTIME llTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->put_StopTime(llTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::get_PrerollTime(REFTIME * pllTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->get_PrerollTime(pllTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::put_PrerollTime(REFTIME llTime) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->put_PrerollTime(llTime); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::get_Rate(double * pdRate) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->get_Rate(pdRate); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::put_Rate(double dRate) +{ + if (0.0 == dRate) { + return E_INVALIDARG; + } + + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->put_Rate(dRate); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::CanSeekForward(LONG *pCanSeekForward) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->CanSeekForward(pCanSeekForward); + pMP->Release(); + return hr; +} + + +STDMETHODIMP +CPosPassThru::CanSeekBackward(LONG *pCanSeekBackward) +{ + IMediaPosition* pMP; + HRESULT hr = GetPeer(&pMP); + if (FAILED(hr)) { + return hr; + } + hr = pMP->CanSeekBackward(pCanSeekBackward); + pMP->Release(); + return hr; +} + + +// --- Implements the CRendererPosPassThru class ---------- + + +// Media times (eg current frame, field, sample etc) are passed through the +// filtergraph in media samples. When a renderer gets a sample with media +// times in it, it will call one of the RegisterMediaTime methods we expose +// (one takes an IMediaSample, the other takes the media times direct). We +// store the media times internally and return them in GetCurrentPosition. + +CRendererPosPassThru::CRendererPosPassThru(const TCHAR *pName, + LPUNKNOWN pUnk, + HRESULT *phr, + IPin *pPin) : + CPosPassThru(pName,pUnk,phr,pPin), + m_StartMedia(0), + m_EndMedia(0), + m_bReset(TRUE) +{ +} + + +// Sets the media times the object should report + +HRESULT +CRendererPosPassThru::RegisterMediaTime(IMediaSample *pMediaSample) +{ + ASSERT(pMediaSample); + LONGLONG StartMedia; + LONGLONG EndMedia; + + CAutoLock cAutoLock(&m_PositionLock); + + // Get the media times from the sample + + HRESULT hr = pMediaSample->GetTime(&StartMedia,&EndMedia); + if (FAILED(hr)) + { + ASSERT(hr == VFW_E_SAMPLE_TIME_NOT_SET); + return hr; + } + + m_StartMedia = StartMedia; + m_EndMedia = EndMedia; + m_bReset = FALSE; + return NOERROR; +} + + +// Sets the media times the object should report + +HRESULT +CRendererPosPassThru::RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime) +{ + CAutoLock cAutoLock(&m_PositionLock); + m_StartMedia = StartTime; + m_EndMedia = EndTime; + m_bReset = FALSE; + return NOERROR; +} + + +// Return the current media times registered in the object + +HRESULT +CRendererPosPassThru::GetMediaTime(LONGLONG *pStartTime,LONGLONG *pEndTime) +{ + ASSERT(pStartTime); + + CAutoLock cAutoLock(&m_PositionLock); + if (m_bReset == TRUE) { + return E_FAIL; + } + + // We don't have to return the end time + + HRESULT hr = ConvertTimeFormat( pStartTime, 0, m_StartMedia, &TIME_FORMAT_MEDIA_TIME ); + if (pEndTime && SUCCEEDED(hr)) { + hr = ConvertTimeFormat( pEndTime, 0, m_EndMedia, &TIME_FORMAT_MEDIA_TIME ); + } + return hr; +} + + +// Resets the media times we hold + +HRESULT +CRendererPosPassThru::ResetMediaTime() +{ + CAutoLock cAutoLock(&m_PositionLock); + m_StartMedia = 0; + m_EndMedia = 0; + m_bReset = TRUE; + return NOERROR; +} + +// Intended to be called by the owing filter during EOS processing so +// that the media times can be adjusted to the stop time. This ensures +// that the GetCurrentPosition will actully get to the stop position. +HRESULT +CRendererPosPassThru::EOS() +{ + HRESULT hr; + + if ( m_bReset == TRUE ) hr = E_FAIL; + else + { + LONGLONG llStop; + if SUCCEEDED(hr=GetStopPosition(&llStop)) + { + CAutoLock cAutoLock(&m_PositionLock); + m_StartMedia = + m_EndMedia = llStop; + } + } + return hr; +} + +// -- CSourceSeeking implementation ------------ + +CSourceSeeking::CSourceSeeking( + const TCHAR * pName, + LPUNKNOWN pUnk, + HRESULT* phr, + CCritSec * pLock) : + CUnknown(pName, pUnk), + m_pLock(pLock), + m_rtStart((long)0) +{ + m_rtStop = _I64_MAX / 2; + m_rtDuration = m_rtStop; + m_dRateSeeking = 1.0; + + m_dwSeekingCaps = AM_SEEKING_CanSeekForwards + | AM_SEEKING_CanSeekBackwards + | AM_SEEKING_CanSeekAbsolute + | AM_SEEKING_CanGetStopPos + | AM_SEEKING_CanGetDuration; +} + +HRESULT CSourceSeeking::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + if(riid == IID_IMediaSeeking) { + CheckPointer(ppv, E_POINTER); + return GetInterface(static_cast(this), ppv); + } + else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +HRESULT CSourceSeeking::IsFormatSupported(const GUID * pFormat) +{ + CheckPointer(pFormat, E_POINTER); + // only seeking in time (REFERENCE_TIME units) is supported + return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE; +} + +HRESULT CSourceSeeking::QueryPreferredFormat(GUID *pFormat) +{ + CheckPointer(pFormat, E_POINTER); + *pFormat = TIME_FORMAT_MEDIA_TIME; + return S_OK; +} + +HRESULT CSourceSeeking::SetTimeFormat(const GUID * pFormat) +{ + CheckPointer(pFormat, E_POINTER); + + // nothing to set; just check that it's TIME_FORMAT_TIME + return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : E_INVALIDARG; +} + +HRESULT CSourceSeeking::IsUsingTimeFormat(const GUID * pFormat) +{ + CheckPointer(pFormat, E_POINTER); + return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE; +} + +HRESULT CSourceSeeking::GetTimeFormat(GUID *pFormat) +{ + CheckPointer(pFormat, E_POINTER); + *pFormat = TIME_FORMAT_MEDIA_TIME; + return S_OK; +} + +HRESULT CSourceSeeking::GetDuration(LONGLONG *pDuration) +{ + CheckPointer(pDuration, E_POINTER); + CAutoLock lock(m_pLock); + *pDuration = m_rtDuration; + return S_OK; +} + +HRESULT CSourceSeeking::GetStopPosition(LONGLONG *pStop) +{ + CheckPointer(pStop, E_POINTER); + CAutoLock lock(m_pLock); + *pStop = m_rtStop; + return S_OK; +} + +HRESULT CSourceSeeking::GetCurrentPosition(LONGLONG *pCurrent) +{ + // GetCurrentPosition is typically supported only in renderers and + // not in source filters. + return E_NOTIMPL; +} + +HRESULT CSourceSeeking::GetCapabilities( DWORD * pCapabilities ) +{ + CheckPointer(pCapabilities, E_POINTER); + *pCapabilities = m_dwSeekingCaps; + return S_OK; +} + +HRESULT CSourceSeeking::CheckCapabilities( DWORD * pCapabilities ) +{ + CheckPointer(pCapabilities, E_POINTER); + + // make sure all requested capabilities are in our mask + return (~m_dwSeekingCaps & *pCapabilities) ? S_FALSE : S_OK; +} + +HRESULT CSourceSeeking::ConvertTimeFormat( LONGLONG * pTarget, const GUID * pTargetFormat, + LONGLONG Source, const GUID * pSourceFormat ) +{ + CheckPointer(pTarget, E_POINTER); + // format guids can be null to indicate current format + + // since we only support TIME_FORMAT_MEDIA_TIME, we don't really + // offer any conversions. + if(pTargetFormat == 0 || *pTargetFormat == TIME_FORMAT_MEDIA_TIME) + { + if(pSourceFormat == 0 || *pSourceFormat == TIME_FORMAT_MEDIA_TIME) + { + *pTarget = Source; + return S_OK; + } + } + + return E_INVALIDARG; +} + + +HRESULT CSourceSeeking::SetPositions( LONGLONG * pCurrent, DWORD CurrentFlags + , LONGLONG * pStop, DWORD StopFlags ) +{ + DWORD StopPosBits = StopFlags & AM_SEEKING_PositioningBitsMask; + DWORD StartPosBits = CurrentFlags & AM_SEEKING_PositioningBitsMask; + + if(StopFlags) { + CheckPointer(pStop, E_POINTER); + + // accept only relative, incremental, or absolute positioning + if(StopPosBits != StopFlags) { + return E_INVALIDARG; + } + } + + if(CurrentFlags) { + CheckPointer(pCurrent, E_POINTER); + if(StartPosBits != AM_SEEKING_AbsolutePositioning && + StartPosBits != AM_SEEKING_RelativePositioning) { + return E_INVALIDARG; + } + } + + + // scope for autolock + { + CAutoLock lock(m_pLock); + + // set start position + if(StartPosBits == AM_SEEKING_AbsolutePositioning) + { + m_rtStart = *pCurrent; + } + else if(StartPosBits == AM_SEEKING_RelativePositioning) + { + m_rtStart += *pCurrent; + } + + // set stop position + if(StopPosBits == AM_SEEKING_AbsolutePositioning) + { + m_rtStop = *pStop; + } + else if(StopPosBits == AM_SEEKING_IncrementalPositioning) + { + m_rtStop = m_rtStart + *pStop; + } + else if(StopPosBits == AM_SEEKING_RelativePositioning) + { + m_rtStop = m_rtStop + *pStop; + } + } + + + HRESULT hr = S_OK; + if(SUCCEEDED(hr) && StopPosBits) { + hr = ChangeStop(); + } + if(StartPosBits) { + hr = ChangeStart(); + } + + return hr; +} + + +HRESULT CSourceSeeking::GetPositions( LONGLONG * pCurrent, LONGLONG * pStop ) +{ + if(pCurrent) { + *pCurrent = m_rtStart; + } + if(pStop) { + *pStop = m_rtStop; + } + + return S_OK;; +} + + +HRESULT CSourceSeeking::GetAvailable( LONGLONG * pEarliest, LONGLONG * pLatest ) +{ + if(pEarliest) { + *pEarliest = 0; + } + if(pLatest) { + CAutoLock lock(m_pLock); + *pLatest = m_rtDuration; + } + return S_OK; +} + +HRESULT CSourceSeeking::SetRate( double dRate) +{ + { + CAutoLock lock(m_pLock); + m_dRateSeeking = dRate; + } + return ChangeRate(); +} + +HRESULT CSourceSeeking::GetRate( double * pdRate) +{ + CheckPointer(pdRate, E_POINTER); + CAutoLock lock(m_pLock); + *pdRate = m_dRateSeeking; + return S_OK; +} + +HRESULT CSourceSeeking::GetPreroll(LONGLONG *pPreroll) +{ + CheckPointer(pPreroll, E_POINTER); + *pPreroll = 0; + return S_OK; +} + + + + + +// --- CSourcePosition implementation ---------- + + +CSourcePosition::CSourcePosition(const TCHAR * pName, + LPUNKNOWN pUnk, + HRESULT* phr, + CCritSec * pLock) : + CMediaPosition(pName, pUnk), + m_pLock(pLock), + m_Start(CRefTime((LONGLONG)0)) +{ + m_Stop = _I64_MAX; + m_Rate = 1.0; +} + + +STDMETHODIMP +CSourcePosition::get_Duration(REFTIME * plength) +{ + CheckPointer(plength,E_POINTER); + ValidateReadWritePtr(plength,sizeof(REFTIME)); + CAutoLock lock(m_pLock); + + *plength = m_Duration; + return S_OK; +} + + +STDMETHODIMP +CSourcePosition::put_CurrentPosition(REFTIME llTime) +{ + m_pLock->Lock(); + m_Start = llTime; + m_pLock->Unlock(); + + return ChangeStart(); +} + + +STDMETHODIMP +CSourcePosition::get_StopTime(REFTIME * pllTime) +{ + CheckPointer(pllTime,E_POINTER); + ValidateReadWritePtr(pllTime,sizeof(REFTIME)); + CAutoLock lock(m_pLock); + + *pllTime = m_Stop; + return S_OK; +} + + +STDMETHODIMP +CSourcePosition::put_StopTime(REFTIME llTime) +{ + m_pLock->Lock(); + m_Stop = llTime; + m_pLock->Unlock(); + + return ChangeStop(); +} + + +STDMETHODIMP +CSourcePosition::get_PrerollTime(REFTIME * pllTime) +{ + CheckPointer(pllTime,E_POINTER); + ValidateReadWritePtr(pllTime,sizeof(REFTIME)); + return E_NOTIMPL; +} + + +STDMETHODIMP +CSourcePosition::put_PrerollTime(REFTIME llTime) +{ + return E_NOTIMPL; +} + + +STDMETHODIMP +CSourcePosition::get_Rate(double * pdRate) +{ + CheckPointer(pdRate,E_POINTER); + ValidateReadWritePtr(pdRate,sizeof(double)); + CAutoLock lock(m_pLock); + + *pdRate = m_Rate; + return S_OK; +} + + +STDMETHODIMP +CSourcePosition::put_Rate(double dRate) +{ + m_pLock->Lock(); + m_Rate = dRate; + m_pLock->Unlock(); + + return ChangeRate(); +} + + +// By default we can seek forwards + +STDMETHODIMP +CSourcePosition::CanSeekForward(LONG *pCanSeekForward) +{ + CheckPointer(pCanSeekForward,E_POINTER); + *pCanSeekForward = OATRUE; + return S_OK; +} + + +// By default we can seek backwards + +STDMETHODIMP +CSourcePosition::CanSeekBackward(LONG *pCanSeekBackward) +{ + CheckPointer(pCanSeekBackward,E_POINTER); + *pCanSeekBackward = OATRUE; + return S_OK; +} + + +// --- Implementation of CBasicAudio class ---------- + + +CBasicAudio::CBasicAudio(const TCHAR * pName,LPUNKNOWN punk) : + CUnknown(pName, punk) +{ +} + +// overriden to publicise our interfaces + +STDMETHODIMP +CBasicAudio::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IBasicAudio) { + return GetInterface( (IBasicAudio *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +STDMETHODIMP +CBasicAudio::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +STDMETHODIMP +CBasicAudio::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IBasicAudio, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CBasicAudio::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IBasicAudio, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CBasicAudio::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IBasicAudio *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- IVideoWindow implementation ---------- + +CBaseVideoWindow::CBaseVideoWindow(const TCHAR * pName,LPUNKNOWN punk) : + CUnknown(pName, punk) +{ +} + + +// overriden to publicise our interfaces + +STDMETHODIMP +CBaseVideoWindow::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IVideoWindow) { + return GetInterface( (IVideoWindow *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +STDMETHODIMP +CBaseVideoWindow::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +STDMETHODIMP +CBaseVideoWindow::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IVideoWindow, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CBaseVideoWindow::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IVideoWindow, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CBaseVideoWindow::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IVideoWindow *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- IBasicVideo implementation ---------- + + +CBaseBasicVideo::CBaseBasicVideo(const TCHAR * pName,LPUNKNOWN punk) : + CUnknown(pName, punk) +{ +} + + +// overriden to publicise our interfaces + +STDMETHODIMP +CBaseBasicVideo::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IBasicVideo || riid == IID_IBasicVideo2) { + return GetInterface( static_cast(this), ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +STDMETHODIMP +CBaseBasicVideo::GetTypeInfoCount(UINT * pctinfo) +{ + return m_basedisp.GetTypeInfoCount(pctinfo); +} + + +STDMETHODIMP +CBaseBasicVideo::GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo) +{ + return m_basedisp.GetTypeInfo( + IID_IBasicVideo, + itinfo, + lcid, + pptinfo); +} + + +STDMETHODIMP +CBaseBasicVideo::GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid) +{ + return m_basedisp.GetIDsOfNames( + IID_IBasicVideo, + rgszNames, + cNames, + lcid, + rgdispid); +} + + +STDMETHODIMP +CBaseBasicVideo::Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr) +{ + // this parameter is a dead leftover from an earlier interface + if (IID_NULL != riid) { + return DISP_E_UNKNOWNINTERFACE; + } + + ITypeInfo * pti; + HRESULT hr = GetTypeInfo(0, lcid, &pti); + + if (FAILED(hr)) { + return hr; + } + + hr = pti->Invoke( + (IBasicVideo *)this, + dispidMember, + wFlags, + pdispparams, + pvarResult, + pexcepinfo, + puArgErr); + + pti->Release(); + return hr; +} + + +// --- Implementation of Deferred Commands ---------- + + +CDispParams::CDispParams(UINT nArgs, VARIANT* pArgs, HRESULT *phr) +{ + cNamedArgs = 0; + rgdispidNamedArgs = NULL; + cArgs = nArgs; + + if (cArgs) { + rgvarg = new VARIANT[cArgs]; + if (NULL == rgvarg) { + cArgs = 0; + if (phr) { + *phr = E_OUTOFMEMORY; + } + return; + } + + for (UINT i = 0; i < cArgs; i++) { + + VARIANT * pDest = &rgvarg[i]; + VARIANT * pSrc = &pArgs[i]; + + pDest->vt = pSrc->vt; + switch(pDest->vt) { + + case VT_I4: + pDest->lVal = pSrc->lVal; + break; + + case VT_UI1: + pDest->bVal = pSrc->bVal; + break; + + case VT_I2: + pDest->iVal = pSrc->iVal; + break; + + case VT_R4: + pDest->fltVal = pSrc->fltVal; + break; + + case VT_R8: + pDest->dblVal = pSrc->dblVal; + break; + + case VT_BOOL: + pDest->boolVal = pSrc->boolVal; + break; + + case VT_ERROR: + pDest->scode = pSrc->scode; + break; + + case VT_CY: + pDest->cyVal = pSrc->cyVal; + break; + + case VT_DATE: + pDest->date = pSrc->date; + break; + + case VT_BSTR: + if (pSrc->bstrVal == NULL) { + pDest->bstrVal = NULL; + } else { + + // a BSTR is a WORD followed by a UNICODE string. + // the pointer points just after the WORD + + WORD len = * (WORD*) (pSrc->bstrVal - (sizeof(WORD) / sizeof(OLECHAR))); + OLECHAR* pch = new OLECHAR[len + (sizeof(WORD)/sizeof(OLECHAR))]; + if (pch) { + WORD *pui = (WORD*)pch; + *pui = len; + pDest->bstrVal = pch + (sizeof(WORD)/sizeof(OLECHAR)); + CopyMemory(pDest->bstrVal, pSrc->bstrVal, len*sizeof(OLECHAR)); + } else { + cArgs = i; + if (phr) { + *phr = E_OUTOFMEMORY; + } + } + } + pDest->bstrVal = pSrc->bstrVal; + break; + + case VT_UNKNOWN: + pDest->punkVal = pSrc->punkVal; + pDest->punkVal->AddRef(); + break; + + case VT_DISPATCH: + pDest->pdispVal = pSrc->pdispVal; + pDest->pdispVal->AddRef(); + break; + + default: + // a type we haven't got round to adding yet! + ASSERT(0); + break; + } + } + + } else { + rgvarg = NULL; + } + +} + + +CDispParams::~CDispParams() +{ + for (UINT i = 0; i < cArgs; i++) { + switch(rgvarg[i].vt) { + case VT_BSTR: + if (rgvarg[i].bstrVal != NULL) { + OLECHAR * pch = rgvarg[i].bstrVal - (sizeof(WORD)/sizeof(OLECHAR)); + delete pch; + } + break; + + case VT_UNKNOWN: + rgvarg[i].punkVal->Release(); + break; + + case VT_DISPATCH: + rgvarg[i].pdispVal->Release(); + break; + } + } + delete[] rgvarg; +} + + +// lifetime is controlled by refcounts (see defer.h) + +CDeferredCommand::CDeferredCommand( + CCmdQueue * pQ, + LPUNKNOWN pUnk, + HRESULT * phr, + LPUNKNOWN pUnkExecutor, + REFTIME time, + GUID* iid, + long dispidMethod, + short wFlags, + long nArgs, + VARIANT* pDispParams, + VARIANT* pvarResult, + short* puArgErr, + BOOL bStream + ) : + CUnknown(NAME("DeferredCommand"), pUnk), + m_pQueue(pQ), + m_pUnk(pUnkExecutor), + m_iid(iid), + m_dispidMethod(dispidMethod), + m_wFlags(wFlags), + m_DispParams(nArgs, pDispParams, phr), + m_pvarResult(pvarResult), + m_bStream(bStream), + m_hrResult(E_ABORT) + +{ + // convert REFTIME to REFERENCE_TIME + COARefTime convertor(time); + m_time = convertor; + + // no check of time validity - it's ok to queue a command that's + // already late + + // check iid is supportable on pUnk by QueryInterface for it + IUnknown * pInterface; + HRESULT hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface); + if (FAILED(hr)) { + *phr = hr; + return; + } + pInterface->Release(); + + + // !!! check dispidMethod and param/return types using typelib + ITypeInfo *pti; + hr = m_Dispatch.GetTypeInfo(*iid, 0, 0, &pti); + if (FAILED(hr)) { + *phr = hr; + return; + } + // !!! some sort of ITypeInfo validity check here + pti->Release(); + + + // Fix up the dispid for put and get + if (wFlags == DISPATCH_PROPERTYPUT) { + m_DispParams.cNamedArgs = 1; + m_DispId = DISPID_PROPERTYPUT; + m_DispParams.rgdispidNamedArgs = &m_DispId; + } + + // all checks ok - add to queue + hr = pQ->Insert(this); + if (FAILED(hr)) { + *phr = hr; + } +} + + +// refcounts are held by caller of InvokeAt... and by list. So if +// we get here, we can't be on the list + +#if 0 +CDeferredCommand::~CDeferredCommand() +{ + // this assert is invalid since if the queue is deleted while we are + // still on the queue, we will have been removed by the queue and this + // m_pQueue will not have been modified. + // ASSERT(m_pQueue == NULL); + + // we don't hold a ref count on pUnk, which is the object that should + // execute the command. + // This is because there would otherwise be a circular refcount problem + // since pUnk probably owns the CmdQueue object that has a refcount + // on us. + // The lifetime of pUnk is guaranteed by it being part of, or lifetime + // controlled by, our parent object. As long as we are on the list, pUnk + // must be valid. Once we are off the list, we do not use pUnk. + +} +#endif + + +// overriden to publicise our interfaces + +STDMETHODIMP +CDeferredCommand::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + ValidateReadWritePtr(ppv,sizeof(PVOID)); + if (riid == IID_IDeferredCommand) { + return GetInterface( (IDeferredCommand *) this, ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} + + +// remove from q. this will reduce the refcount by one (since the q +// holds a count) but can't make us go away since he must have a +// refcount in order to call this method. + +STDMETHODIMP +CDeferredCommand::Cancel() +{ + if (m_pQueue == NULL) { + return VFW_E_ALREADY_CANCELLED; + } + + HRESULT hr = m_pQueue->Remove(this); + if (FAILED(hr)) { + return hr; + } + + m_pQueue = NULL; + return S_OK; +} + + +STDMETHODIMP +CDeferredCommand::Confidence(LONG* pConfidence) +{ + return E_NOTIMPL; +} + + +STDMETHODIMP +CDeferredCommand::GetHResult(HRESULT * phrResult) +{ + CheckPointer(phrResult,E_POINTER); + ValidateReadWritePtr(phrResult,sizeof(HRESULT)); + + if (m_pQueue != NULL) { + return E_ABORT; + } + *phrResult = m_hrResult; + return S_OK; +} + + +// set the time to be a new time (checking that it is valid) and +// then requeue + +STDMETHODIMP +CDeferredCommand::Postpone(REFTIME newtime) +{ + + // check that this time is not past + // convert REFTIME to REFERENCE_TIME + COARefTime convertor(newtime); + + // check that the time has not passed + if (m_pQueue->CheckTime(convertor, IsStreamTime())) { + return VFW_E_TIME_ALREADY_PASSED; + } + + // extract from list + HRESULT hr = m_pQueue->Remove(this); + if (FAILED(hr)) { + return hr; + } + + // change time + m_time = convertor; + + // requeue + hr = m_pQueue->Insert(this); + + return hr; +} + + +HRESULT +CDeferredCommand::Invoke() +{ + // check that we are still outstanding + if (m_pQueue == NULL) { + return VFW_E_ALREADY_CANCELLED; + } + + // get the type info + ITypeInfo* pti; + HRESULT hr = m_Dispatch.GetTypeInfo(GetIID(), 0, 0, &pti); + if (FAILED(hr)) { + return hr; + } + + // qi for the expected interface and then invoke it. Note that we have to + // treat the returned interface as IUnknown since we don't know its type. + IUnknown* pInterface; + + hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface); + if (FAILED(hr)) { + pti->Release(); + return hr; + } + + EXCEPINFO expinfo; + UINT uArgErr; + m_hrResult = pti->Invoke( + pInterface, + GetMethod(), + GetFlags(), + GetParams(), + GetResult(), + &expinfo, + &uArgErr); + + // release the interface we QI'd for + pInterface->Release(); + pti->Release(); + + + // remove from list whether or not successful + // or we loop indefinitely + hr = m_pQueue->Remove(this); + m_pQueue = NULL; + return hr; +} + + + +// --- CCmdQueue methods ---------- + + +CCmdQueue::CCmdQueue() : + m_listPresentation(NAME("Presentation time command list")), + m_listStream(NAME("Stream time command list")), + m_evDue(TRUE), // manual reset + m_dwAdvise(0), + m_pClock(NULL), + m_bRunning(FALSE) +{ +} + + +CCmdQueue::~CCmdQueue() +{ + // empty all our lists + + // we hold a refcount on each, so traverse and Release each + // entry then RemoveAll to empty the list + WXLIST_POSITION pos = m_listPresentation.GetHeadPosition(); + + while(pos) { + CDeferredCommand* pCmd = m_listPresentation.GetNext(pos); + pCmd->Release(); + } + m_listPresentation.RemoveAll(); + + pos = m_listStream.GetHeadPosition(); + + while(pos) { + CDeferredCommand* pCmd = m_listStream.GetNext(pos); + pCmd->Release(); + } + m_listStream.RemoveAll(); + + if (m_pClock) { + if (m_dwAdvise) { + m_pClock->Unadvise(m_dwAdvise); + m_dwAdvise = 0; + } + m_pClock->Release(); + } +} + + +// returns a new CDeferredCommand object that will be initialised with +// the parameters and will be added to the queue during construction. +// returns S_OK if successfully created otherwise an error and +// no object has been queued. + +HRESULT +CCmdQueue::New( + CDeferredCommand **ppCmd, + LPUNKNOWN pUnk, // this object will execute command + REFTIME time, + GUID* iid, + long dispidMethod, + short wFlags, + long cArgs, + VARIANT* pDispParams, + VARIANT* pvarResult, + short* puArgErr, + BOOL bStream +) +{ + CAutoLock lock(&m_Lock); + + HRESULT hr = S_OK; + *ppCmd = NULL; + + CDeferredCommand* pCmd; + pCmd = new CDeferredCommand( + this, + NULL, // not aggregated + &hr, + pUnk, // this guy will execute + time, + iid, + dispidMethod, + wFlags, + cArgs, + pDispParams, + pvarResult, + puArgErr, + bStream); + + if (pCmd == NULL) { + hr = E_OUTOFMEMORY; + } else { + *ppCmd = pCmd; + } + return hr; +} + + +HRESULT +CCmdQueue::Insert(CDeferredCommand* pCmd) +{ + CAutoLock lock(&m_Lock); + + // addref the item + pCmd->AddRef(); + + CGenericList * pList; + if (pCmd->IsStreamTime()) { + pList = &m_listStream; + } else { + pList = &m_listPresentation; + } + WXLIST_POSITION pos = pList->GetHeadPosition(); + + // seek past all items that are before us + while (pos && + (pList->Get(pos)->GetTime() <= pCmd->GetTime())) { + + pList->GetNext(pos); + } + + // now at end of list or in front of items that come later + if (!pos) { + pList->AddTail(pCmd); + } else { + pList->AddBefore(pos, pCmd); + } + + SetTimeAdvise(); + return S_OK; +} + + +HRESULT +CCmdQueue::Remove(CDeferredCommand* pCmd) +{ + CAutoLock lock(&m_Lock); + HRESULT hr = S_OK; + + CGenericList * pList; + if (pCmd->IsStreamTime()) { + pList = &m_listStream; + } else { + pList = &m_listPresentation; + } + WXLIST_POSITION pos = pList->GetHeadPosition(); + + // traverse the list + while (pos && (pList->Get(pos) != pCmd)) { + pList->GetNext(pos); + } + + // did we drop off the end? + if (!pos) { + hr = VFW_E_NOT_FOUND; + } else { + + // found it - now take off list + pList->Remove(pos); + + // Insert did an AddRef, so release it + pCmd->Release(); + + // check that timer request is still for earliest time + SetTimeAdvise(); + } + return hr; +} + + +// set the clock used for timing + +HRESULT +CCmdQueue::SetSyncSource(IReferenceClock* pClock) +{ + CAutoLock lock(&m_Lock); + + // addref the new clock first in case they are the same + if (pClock) { + pClock->AddRef(); + } + + // kill any advise on the old clock + if (m_pClock) { + if (m_dwAdvise) { + m_pClock->Unadvise(m_dwAdvise); + m_dwAdvise = 0; + } + m_pClock->Release(); + } + m_pClock = pClock; + + // set up a new advise + SetTimeAdvise(); + return S_OK; +} + + +// set up a timer event with the reference clock + +void +CCmdQueue::SetTimeAdvise(void) +{ + // make sure we have a clock to use + if (!m_pClock) { + return; + } + + // reset the event whenever we are requesting a new signal + m_evDue.Reset(); + + // time 0 is earliest + CRefTime current; + + // find the earliest presentation time + if (m_listPresentation.GetCount() > 0) { + + WXLIST_POSITION pos = m_listPresentation.GetHeadPosition(); + current = m_listPresentation.Get(pos)->GetTime(); + } + + // if we're running, check the stream times too + if (m_bRunning) { + + CRefTime t; + + if (m_listStream.GetCount() > 0) { + + WXLIST_POSITION pos = m_listStream.GetHeadPosition(); + t = m_listStream.Get(pos)->GetTime(); + + // add on stream time offset to get presentation time + t += m_StreamTimeOffset; + + // is this earlier? + if ((current == TimeZero) || (t < current)) { + current = t; + } + } + } + + // need to change? + if ((current > TimeZero) && (current != m_tCurrentAdvise)) { + if (m_dwAdvise) { + m_pClock->Unadvise(m_dwAdvise); + // reset the event whenever we are requesting a new signal + m_evDue.Reset(); + } + + // ask for time advice - the first two params are either + // stream time offset and stream time or + // presentation time and 0. we always use the latter + HRESULT hr = m_pClock->AdviseTime( + (REFERENCE_TIME)current, + TimeZero, + (HEVENT) HANDLE(m_evDue), + &m_dwAdvise); + + ASSERT(SUCCEEDED(hr)); + m_tCurrentAdvise = current; + } +} + + +// switch to run mode. Streamtime to Presentation time mapping known. + +HRESULT +CCmdQueue::Run(REFERENCE_TIME tStreamTimeOffset) +{ + CAutoLock lock(&m_Lock); + + m_StreamTimeOffset = tStreamTimeOffset; + m_bRunning = TRUE; + + // ensure advise is accurate + SetTimeAdvise(); + return S_OK; +} + + +// switch to Stopped or Paused mode. Time mapping not known. + +HRESULT +CCmdQueue::EndRun() +{ + CAutoLock lock(&m_Lock); + + m_bRunning = FALSE; + + // check timer setting - stream times + SetTimeAdvise(); + return S_OK; +} + + +// return a pointer to the next due command. Blocks for msTimeout +// milliseconds until there is a due command. +// Stream-time commands will only become due between Run and Endrun calls. +// The command remains queued until invoked or cancelled. +// Returns E_ABORT if timeout occurs, otherwise S_OK (or other error). +// +// returns an AddRef'd object + +HRESULT +CCmdQueue::GetDueCommand(CDeferredCommand ** ppCmd, long msTimeout) +{ + // loop until we timeout or find a due command + for (;;) { + + { + CAutoLock lock(&m_Lock); + + + // find the earliest command + CDeferredCommand * pCmd = NULL; + + // check the presentation time and the + // stream time list to find the earliest + + if (m_listPresentation.GetCount() > 0) { + WXLIST_POSITION pos = m_listPresentation.GetHeadPosition(); + pCmd = m_listPresentation.Get(pos); + } + + if (m_bRunning && (m_listStream.GetCount() > 0)) { + WXLIST_POSITION pos = m_listStream.GetHeadPosition(); + CDeferredCommand* pStrm = m_listStream.Get(pos); + + CRefTime t = pStrm->GetTime() + m_StreamTimeOffset; + if (!pCmd || (t < pCmd->GetTime())) { + pCmd = pStrm; + } + } + + // if we have found one, is it due? + if (pCmd) { + if (CheckTime(pCmd->GetTime(), pCmd->IsStreamTime())) { + + // yes it's due - addref it + pCmd->AddRef(); + *ppCmd = pCmd; + return S_OK; + } + } + } + + // block until the advise is signalled + if (WaitForSingleObject(m_evDue, msTimeout) != WAIT_OBJECT_0) { + return E_ABORT; + } + } +} + + +// return a pointer to a command that will be due for a given time. +// Pass in a stream time here. The stream time offset will be passed +// in via the Run method. +// Commands remain queued until invoked or cancelled. +// This method will not block. It will report E_ABORT if there are no +// commands due yet. +// +// returns an AddRef'd object + +HRESULT +CCmdQueue::GetCommandDueFor(REFERENCE_TIME rtStream, CDeferredCommand**ppCmd) +{ + CAutoLock lock(&m_Lock); + + CRefTime tStream(rtStream); + + // find the earliest stream and presentation time commands + CDeferredCommand* pStream = NULL; + if (m_listStream.GetCount() > 0) { + WXLIST_POSITION pos = m_listStream.GetHeadPosition(); + pStream = m_listStream.Get(pos); + } + CDeferredCommand* pPresent = NULL; + if (m_listPresentation.GetCount() > 0) { + WXLIST_POSITION pos = m_listPresentation.GetHeadPosition(); + pPresent = m_listPresentation.Get(pos); + } + + // is there a presentation time that has passed already + if (pPresent && CheckTime(pPresent->GetTime(), FALSE)) { + pPresent->AddRef(); + *ppCmd = pPresent; + return S_OK; + } + + // is there a stream time command due before this stream time + if (pStream && (pStream->GetTime() <= tStream)) { + pPresent->AddRef(); + *ppCmd = pStream; + return S_OK; + } + + // if we are running, we can map presentation times to + // stream time. In this case, is there a presentation time command + // that will be due before this stream time is presented? + if (m_bRunning && pPresent) { + + // this stream time will appear at... + tStream += m_StreamTimeOffset; + + // due before that? + if (pPresent->GetTime() <= tStream) { + *ppCmd = pPresent; + return S_OK; + } + } + + // no commands due yet + return VFW_E_NOT_FOUND; +} + diff --git a/plugins/GSdx/baseclasses/ctlutil.h b/plugins/GSdx/baseclasses/ctlutil.h new file mode 100644 index 0000000000..5df80c0d00 --- /dev/null +++ b/plugins/GSdx/baseclasses/ctlutil.h @@ -0,0 +1,919 @@ +//------------------------------------------------------------------------------ +// File: CtlUtil.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// Base classes implementing IDispatch parsing for the basic control dual +// interfaces. Derive from these and implement just the custom method and +// property methods. We also implement CPosPassThru that can be used by +// renderers and transforms to pass by IMediaPosition and IMediaSeeking + +#ifndef __CTLUTIL__ +#define __CTLUTIL__ + +// OLE Automation has different ideas of TRUE and FALSE + +#define OATRUE (-1) +#define OAFALSE (0) + + +// It's possible that we could replace this class with CreateStdDispatch + +class CBaseDispatch +{ + ITypeInfo * m_pti; + +public: + + CBaseDispatch() : m_pti(NULL) {} + ~CBaseDispatch(); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + REFIID riid, + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); +}; + + +class AM_NOVTABLE CMediaControl : + public IMediaControl, + public CUnknown +{ + CBaseDispatch m_basedisp; + +public: + + CMediaControl(const TCHAR *, LPUNKNOWN); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); +}; + + +class AM_NOVTABLE CMediaEvent : + public IMediaEventEx, + public CUnknown +{ + CBaseDispatch m_basedisp; + +public: + + CMediaEvent(const TCHAR *, LPUNKNOWN); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); +}; + + +class AM_NOVTABLE CMediaPosition : + public IMediaPosition, + public CUnknown +{ + CBaseDispatch m_basedisp; + + +public: + + CMediaPosition(const TCHAR *, LPUNKNOWN); + CMediaPosition(const TCHAR *, LPUNKNOWN, HRESULT *phr); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); + +}; + + +// OA-compatibility means that we must use double as the RefTime value, +// and REFERENCE_TIME (essentially a LONGLONG) within filters. +// this class converts between the two + +class COARefTime : public CRefTime { +public: + + COARefTime() { + }; + + COARefTime(CRefTime t) + : CRefTime(t) + { + }; + + COARefTime(REFERENCE_TIME t) + : CRefTime(t) + { + }; + + COARefTime(double d) { + m_time = (LONGLONG) (d * 10000000); + }; + + operator double() { + return double(m_time) / 10000000; + }; + + operator REFERENCE_TIME() { + return m_time; + }; + + COARefTime& operator=(const double& rd) { + m_time = (LONGLONG) (rd * 10000000); + return *this; + } + + COARefTime& operator=(const REFERENCE_TIME& rt) { + m_time = rt; + return *this; + } + + inline BOOL operator==(const COARefTime& rt) + { + return m_time == rt.m_time; + }; + + inline BOOL operator!=(const COARefTime& rt) + { + return m_time != rt.m_time; + }; + + inline BOOL operator < (const COARefTime& rt) + { + return m_time < rt.m_time; + }; + + inline BOOL operator > (const COARefTime& rt) + { + return m_time > rt.m_time; + }; + + inline BOOL operator >= (const COARefTime& rt) + { + return m_time >= rt.m_time; + }; + + inline BOOL operator <= (const COARefTime& rt) + { + return m_time <= rt.m_time; + }; + + inline COARefTime operator+(const COARefTime& rt) + { + return COARefTime(m_time + rt.m_time); + }; + + inline COARefTime operator-(const COARefTime& rt) + { + return COARefTime(m_time - rt.m_time); + }; + + inline COARefTime operator*(LONG l) + { + return COARefTime(m_time * l); + }; + + inline COARefTime operator/(LONG l) + { + return COARefTime(m_time / l); + }; + +private: + // Prevent bugs from constructing from LONG (which gets + // converted to double and then multiplied by 10000000 + COARefTime(LONG); + int operator=(LONG); +}; + + +// A utility class that handles IMediaPosition and IMediaSeeking on behalf +// of single-input pin renderers, or transform filters. +// +// Renderers will expose this from the filter; transform filters will +// expose it from the output pin and not the renderer. +// +// Create one of these, giving it your IPin* for your input pin, and delegate +// all IMediaPosition methods to it. It will query the input pin for +// IMediaPosition and respond appropriately. +// +// Call ForceRefresh if the pin connection changes. +// +// This class no longer caches the upstream IMediaPosition or IMediaSeeking +// it acquires it on each method call. This means ForceRefresh is not needed. +// The method is kept for source compatibility and to minimise the changes +// if we need to put it back later for performance reasons. + +class CPosPassThru : public IMediaSeeking, public CMediaPosition +{ + IPin *m_pPin; + + HRESULT GetPeer(IMediaPosition **ppMP); + HRESULT GetPeerSeeking(IMediaSeeking **ppMS); + +public: + + CPosPassThru(const TCHAR *, LPUNKNOWN, HRESULT*, IPin *); + DECLARE_IUNKNOWN + + HRESULT ForceRefresh() { + return S_OK; + }; + + // override to return an accurate current position + virtual HRESULT GetMediaTime(LONGLONG *pStartTime,LONGLONG *pEndTime) { + return E_FAIL; + } + + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,void **ppv); + + // IMediaSeeking methods + STDMETHODIMP GetCapabilities( DWORD * pCapabilities ); + STDMETHODIMP CheckCapabilities( DWORD * pCapabilities ); + STDMETHODIMP SetTimeFormat(const GUID * pFormat); + STDMETHODIMP GetTimeFormat(GUID *pFormat); + STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat); + STDMETHODIMP IsFormatSupported( const GUID * pFormat); + STDMETHODIMP QueryPreferredFormat( GUID *pFormat); + STDMETHODIMP ConvertTimeFormat(LONGLONG * pTarget, const GUID * pTargetFormat, + LONGLONG Source, const GUID * pSourceFormat ); + STDMETHODIMP SetPositions( LONGLONG * pCurrent, DWORD CurrentFlags + , LONGLONG * pStop, DWORD StopFlags ); + + STDMETHODIMP GetPositions( LONGLONG * pCurrent, LONGLONG * pStop ); + STDMETHODIMP GetCurrentPosition( LONGLONG * pCurrent ); + STDMETHODIMP GetStopPosition( LONGLONG * pStop ); + STDMETHODIMP SetRate( double dRate); + STDMETHODIMP GetRate( double * pdRate); + STDMETHODIMP GetDuration( LONGLONG *pDuration); + STDMETHODIMP GetAvailable( LONGLONG *pEarliest, LONGLONG *pLatest ); + STDMETHODIMP GetPreroll( LONGLONG *pllPreroll ); + + // IMediaPosition properties + STDMETHODIMP get_Duration(REFTIME * plength); + STDMETHODIMP put_CurrentPosition(REFTIME llTime); + STDMETHODIMP get_StopTime(REFTIME * pllTime); + STDMETHODIMP put_StopTime(REFTIME llTime); + STDMETHODIMP get_PrerollTime(REFTIME * pllTime); + STDMETHODIMP put_PrerollTime(REFTIME llTime); + STDMETHODIMP get_Rate(double * pdRate); + STDMETHODIMP put_Rate(double dRate); + STDMETHODIMP get_CurrentPosition(REFTIME * pllTime); + STDMETHODIMP CanSeekForward(LONG *pCanSeekForward); + STDMETHODIMP CanSeekBackward(LONG *pCanSeekBackward); + +private: + HRESULT GetSeekingLongLong( HRESULT (__stdcall IMediaSeeking::*pMethod)( LONGLONG * ), + LONGLONG * pll ); +}; + + +// Adds the ability to return a current position + +class CRendererPosPassThru : public CPosPassThru +{ + CCritSec m_PositionLock; // Locks access to our position + LONGLONG m_StartMedia; // Start media time last seen + LONGLONG m_EndMedia; // And likewise the end media + BOOL m_bReset; // Have media times been set + +public: + + // Used to help with passing media times through graph + + CRendererPosPassThru(const TCHAR *, LPUNKNOWN, HRESULT*, IPin *); + HRESULT RegisterMediaTime(IMediaSample *pMediaSample); + HRESULT RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime); + HRESULT GetMediaTime(LONGLONG *pStartTime,LONGLONG *pEndTime); + HRESULT ResetMediaTime(); + HRESULT EOS(); +}; + +STDAPI CreatePosPassThru( + LPUNKNOWN pAgg, + BOOL bRenderer, + IPin *pPin, + IUnknown **ppPassThru +); + +// A class that handles the IDispatch part of IBasicAudio and leaves the +// properties and methods themselves pure virtual. + +class AM_NOVTABLE CBasicAudio : public IBasicAudio, public CUnknown +{ + CBaseDispatch m_basedisp; + +public: + + CBasicAudio(const TCHAR *, LPUNKNOWN); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); +}; + + +// A class that handles the IDispatch part of IBasicVideo and leaves the +// properties and methods themselves pure virtual. + +class AM_NOVTABLE CBaseBasicVideo : public IBasicVideo2, public CUnknown +{ + CBaseDispatch m_basedisp; + +public: + + CBaseBasicVideo(const TCHAR *, LPUNKNOWN); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); + + STDMETHODIMP GetPreferredAspectRatio( + long *plAspectX, + long *plAspectY) + { + return E_NOTIMPL; + } +}; + + +// A class that handles the IDispatch part of IVideoWindow and leaves the +// properties and methods themselves pure virtual. + +class AM_NOVTABLE CBaseVideoWindow : public IVideoWindow, public CUnknown +{ + CBaseDispatch m_basedisp; + +public: + + CBaseVideoWindow(const TCHAR *, LPUNKNOWN); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + /* IDispatch methods */ + STDMETHODIMP GetTypeInfoCount(UINT * pctinfo); + + STDMETHODIMP GetTypeInfo( + UINT itinfo, + LCID lcid, + ITypeInfo ** pptinfo); + + STDMETHODIMP GetIDsOfNames( + REFIID riid, + OLECHAR ** rgszNames, + UINT cNames, + LCID lcid, + DISPID * rgdispid); + + STDMETHODIMP Invoke( + DISPID dispidMember, + REFIID riid, + LCID lcid, + WORD wFlags, + DISPPARAMS * pdispparams, + VARIANT * pvarResult, + EXCEPINFO * pexcepinfo, + UINT * puArgErr); +}; + + +// abstract class to help source filters with their implementation +// of IMediaPosition. Derive from this and set the duration (and stop +// position). Also override NotifyChange to do something when the properties +// change. + +class AM_NOVTABLE CSourcePosition : public CMediaPosition +{ + +public: + CSourcePosition(const TCHAR *, LPUNKNOWN, HRESULT*, CCritSec *); + + // IMediaPosition methods + STDMETHODIMP get_Duration(REFTIME * plength); + STDMETHODIMP put_CurrentPosition(REFTIME llTime); + STDMETHODIMP get_StopTime(REFTIME * pllTime); + STDMETHODIMP put_StopTime(REFTIME llTime); + STDMETHODIMP get_PrerollTime(REFTIME * pllTime); + STDMETHODIMP put_PrerollTime(REFTIME llTime); + STDMETHODIMP get_Rate(double * pdRate); + STDMETHODIMP put_Rate(double dRate); + STDMETHODIMP CanSeekForward(LONG *pCanSeekForward); + STDMETHODIMP CanSeekBackward(LONG *pCanSeekBackward); + + // override if you can return the data you are actually working on + STDMETHODIMP get_CurrentPosition(REFTIME * pllTime) { + return E_NOTIMPL; + }; + +protected: + + // we call this to notify changes. Override to handle them + virtual HRESULT ChangeStart() PURE; + virtual HRESULT ChangeStop() PURE; + virtual HRESULT ChangeRate() PURE; + + COARefTime m_Duration; + COARefTime m_Start; + COARefTime m_Stop; + double m_Rate; + + CCritSec * m_pLock; +}; + +class AM_NOVTABLE CSourceSeeking : + public IMediaSeeking, + public CUnknown +{ + +public: + + DECLARE_IUNKNOWN; + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // IMediaSeeking methods + + STDMETHODIMP IsFormatSupported(const GUID * pFormat); + STDMETHODIMP QueryPreferredFormat(GUID *pFormat); + STDMETHODIMP SetTimeFormat(const GUID * pFormat); + STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat); + STDMETHODIMP GetTimeFormat(GUID *pFormat); + STDMETHODIMP GetDuration(LONGLONG *pDuration); + STDMETHODIMP GetStopPosition(LONGLONG *pStop); + STDMETHODIMP GetCurrentPosition(LONGLONG *pCurrent); + STDMETHODIMP GetCapabilities( DWORD * pCapabilities ); + STDMETHODIMP CheckCapabilities( DWORD * pCapabilities ); + STDMETHODIMP ConvertTimeFormat( LONGLONG * pTarget, const GUID * pTargetFormat, + LONGLONG Source, const GUID * pSourceFormat ); + + STDMETHODIMP SetPositions( LONGLONG * pCurrent, DWORD CurrentFlags + , LONGLONG * pStop, DWORD StopFlags ); + + STDMETHODIMP GetPositions( LONGLONG * pCurrent, LONGLONG * pStop ); + + STDMETHODIMP GetAvailable( LONGLONG * pEarliest, LONGLONG * pLatest ); + STDMETHODIMP SetRate( double dRate); + STDMETHODIMP GetRate( double * pdRate); + STDMETHODIMP GetPreroll(LONGLONG *pPreroll); + + +protected: + + // ctor + CSourceSeeking(const TCHAR *, LPUNKNOWN, HRESULT*, CCritSec *); + + // we call this to notify changes. Override to handle them + virtual HRESULT ChangeStart() PURE; + virtual HRESULT ChangeStop() PURE; + virtual HRESULT ChangeRate() PURE; + + CRefTime m_rtDuration; // length of stream + CRefTime m_rtStart; // source will start here + CRefTime m_rtStop; // source will stop here + double m_dRateSeeking; + + // seeking capabilities + DWORD m_dwSeekingCaps; + + CCritSec * m_pLock; +}; + + +// Base classes supporting Deferred commands. + +// Deferred commands are queued by calls to methods on the IQueueCommand +// interface, exposed by the filtergraph and by some filters. A successful +// call to one of these methods will return an IDeferredCommand interface +// representing the queued command. +// +// A CDeferredCommand object represents a single deferred command, and exposes +// the IDeferredCommand interface as well as other methods permitting time +// checks and actual execution. It contains a reference to the CCommandQueue +// object on which it is queued. +// +// CCommandQueue is a base class providing a queue of CDeferredCommand +// objects, and methods to add, remove, check status and invoke the queued +// commands. A CCommandQueue object would be part of an object that +// implemented IQueueCommand. + +class CCmdQueue; + +// take a copy of the params and store them. Release any allocated +// memory in destructor + +class CDispParams : public DISPPARAMS +{ +public: + CDispParams(UINT nArgs, VARIANT* pArgs, HRESULT *phr = NULL); + ~CDispParams(); +}; + + +// CDeferredCommand lifetime is controlled by refcounts. Caller of +// InvokeAt.. gets a refcounted interface pointer, and the CCmdQueue +// object also holds a refcount on us. Calling Cancel or Invoke takes +// us off the CCmdQueue and thus reduces the refcount by 1. Once taken +// off the queue we cannot be put back on the queue. + +class CDeferredCommand + : public CUnknown, + public IDeferredCommand +{ +public: + + CDeferredCommand( + CCmdQueue * pQ, + LPUNKNOWN pUnk, // aggregation outer unk + HRESULT * phr, + LPUNKNOWN pUnkExecutor, // object that will execute this cmd + REFTIME time, + GUID* iid, + long dispidMethod, + short wFlags, + long cArgs, + VARIANT* pDispParams, + VARIANT* pvarResult, + short* puArgErr, + BOOL bStream + ); + + DECLARE_IUNKNOWN + + // override this to publicise our interfaces + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // IDeferredCommand methods + STDMETHODIMP Cancel(); + STDMETHODIMP Confidence( + LONG* pConfidence); + STDMETHODIMP Postpone( + REFTIME newtime); + STDMETHODIMP GetHResult( + HRESULT* phrResult); + + // other public methods + + HRESULT Invoke(); + + // access methods + + // returns TRUE if streamtime, FALSE if presentation time + BOOL IsStreamTime() { + return m_bStream; + }; + + CRefTime GetTime() { + return m_time; + }; + + REFIID GetIID() { + return *m_iid; + }; + + long GetMethod() { + return m_dispidMethod; + }; + + short GetFlags() { + return m_wFlags; + }; + + DISPPARAMS* GetParams() { + return &m_DispParams; + }; + + VARIANT* GetResult() { + return m_pvarResult; + }; + +protected: + + CCmdQueue* m_pQueue; + + // pUnk for the interface that we will execute the command on + LPUNKNOWN m_pUnk; + + // stored command data + REFERENCE_TIME m_time; + GUID* m_iid; + long m_dispidMethod; + short m_wFlags; + VARIANT* m_pvarResult; + BOOL m_bStream; + CDispParams m_DispParams; + DISPID m_DispId; // For get and put + + // we use this for ITypeInfo access + CBaseDispatch m_Dispatch; + + // save retval here + HRESULT m_hrResult; +}; + + +// a list of CDeferredCommand objects. this is a base class providing +// the basics of access to the list. If you want to use CDeferredCommand +// objects then your queue needs to be derived from this class. + +class AM_NOVTABLE CCmdQueue +{ +public: + CCmdQueue(); + virtual ~CCmdQueue(); + + // returns a new CDeferredCommand object that will be initialised with + // the parameters and will be added to the queue during construction. + // returns S_OK if successfully created otherwise an error and + // no object has been queued. + virtual HRESULT New( + CDeferredCommand **ppCmd, + LPUNKNOWN pUnk, + REFTIME time, + GUID* iid, + long dispidMethod, + short wFlags, + long cArgs, + VARIANT* pDispParams, + VARIANT* pvarResult, + short* puArgErr, + BOOL bStream + ); + + // called by the CDeferredCommand object to add and remove itself + // from the queue + virtual HRESULT Insert(CDeferredCommand* pCmd); + virtual HRESULT Remove(CDeferredCommand* pCmd); + + // Command-Due Checking + // + // There are two schemes of synchronisation: coarse and accurate. In + // coarse mode, you wait till the time arrives and then execute the cmd. + // In accurate mode, you wait until you are processing the sample that + // will appear at the time, and then execute the command. It's up to the + // filter which one it will implement. The filtergraph will always + // implement coarse mode for commands queued at the filtergraph. + // + // If you want coarse sync, you probably want to wait until there is a + // command due, and then execute it. You can do this by calling + // GetDueCommand. If you have several things to wait for, get the + // event handle from GetDueHandle() and when this is signalled then call + // GetDueCommand. Stream time will only advance between calls to Run and + // EndRun. Note that to avoid an extra thread there is no guarantee that + // if the handle is set there will be a command ready. Each time the + // event is signalled, call GetDueCommand (probably with a 0 timeout); + // This may return E_ABORT. + // + // If you want accurate sync, you must call GetCommandDueFor, passing + // as a parameter the stream time of the samples you are about to process. + // This will return: + // -- a stream-time command due at or before that stream time + // -- a presentation-time command due at or before the + // time that stream time will be presented (only between Run + // and EndRun calls, since outside of this, the mapping from + // stream time to presentation time is not known. + // -- any presentation-time command due now. + // This means that if you want accurate synchronisation on samples that + // might be processed during Paused mode, you need to use + // stream-time commands. + // + // In all cases, commands remain queued until Invoked or Cancelled. The + // setting and resetting of the event handle is managed entirely by this + // queue object. + + // set the clock used for timing + virtual HRESULT SetSyncSource(IReferenceClock*); + + // switch to run mode. Streamtime to Presentation time mapping known. + virtual HRESULT Run(REFERENCE_TIME tStreamTimeOffset); + + // switch to Stopped or Paused mode. Time mapping not known. + virtual HRESULT EndRun(); + + // return a pointer to the next due command. Blocks for msTimeout + // milliseconds until there is a due command. + // Stream-time commands will only become due between Run and Endrun calls. + // The command remains queued until invoked or cancelled. + // Returns E_ABORT if timeout occurs, otherwise S_OK (or other error). + // Returns an AddRef-ed object + virtual HRESULT GetDueCommand(CDeferredCommand ** ppCmd, long msTimeout); + + // return the event handle that will be signalled whenever + // there are deferred commands due for execution (when GetDueCommand + // will not block). + HANDLE GetDueHandle() { + return HANDLE(m_evDue); + }; + + // return a pointer to a command that will be due for a given time. + // Pass in a stream time here. The stream time offset will be passed + // in via the Run method. + // Commands remain queued until invoked or cancelled. + // This method will not block. It will report VFW_E_NOT_FOUND if there + // are no commands due yet. + // Returns an AddRef-ed object + virtual HRESULT GetCommandDueFor(REFERENCE_TIME tStream, CDeferredCommand**ppCmd); + + // check if a given time is due (TRUE if it is due yet) + BOOL CheckTime(CRefTime time, BOOL bStream) { + + // if no clock, nothing is due! + if (!m_pClock) { + return FALSE; + } + + // stream time + if (bStream) { + + // not valid if not running + if (!m_bRunning) { + return FALSE; + } + // add on known stream time offset to get presentation time + time += m_StreamTimeOffset; + } + + CRefTime Now; + m_pClock->GetTime((REFERENCE_TIME*)&Now); + return (time <= Now); + }; + +protected: + + // protect access to lists etc + CCritSec m_Lock; + + // commands queued in presentation time are stored here + CGenericList m_listPresentation; + + // commands queued in stream time are stored here + CGenericList m_listStream; + + // set when any commands are due + CAMEvent m_evDue; + + // creates an advise for the earliest time required, if any + void SetTimeAdvise(void); + + // advise id from reference clock (0 if no outstanding advise) + DWORD_PTR m_dwAdvise; + + // advise time is for this presentation time + CRefTime m_tCurrentAdvise; + + // the reference clock we are using (addrefed) + IReferenceClock* m_pClock; + + // true when running + BOOL m_bRunning; + + // contains stream time offset when m_bRunning is true + CRefTime m_StreamTimeOffset; +}; + +#endif // __CTLUTIL__ diff --git a/plugins/GSdx/baseclasses/ddmm.cpp b/plugins/GSdx/baseclasses/ddmm.cpp new file mode 100644 index 0000000000..079a35e474 --- /dev/null +++ b/plugins/GSdx/baseclasses/ddmm.cpp @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// File: DDMM.cpp +// +// Desc: DirectShow base classes - implements routines for using DirectDraw +// on a multimonitor system. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include +#include "ddmm.h" + +/* + * FindDeviceCallback + */ +typedef struct { + LPSTR szDevice; + GUID* lpGUID; + GUID GUID; + BOOL fFound; +} FindDeviceData; + +BOOL CALLBACK FindDeviceCallback(GUID* lpGUID, LPSTR szName, LPSTR szDevice, LPVOID lParam) +{ + FindDeviceData *p = (FindDeviceData*)lParam; + + if (lstrcmpiA(p->szDevice, szDevice) == 0) { + if (lpGUID) { + p->GUID = *lpGUID; + p->lpGUID = &p->GUID; + } else { + p->lpGUID = NULL; + } + p->fFound = TRUE; + return FALSE; + } + return TRUE; +} + + +BOOL CALLBACK FindDeviceCallbackEx(GUID* lpGUID, LPSTR szName, LPSTR szDevice, LPVOID lParam, HMONITOR hMonitor) +{ + FindDeviceData *p = (FindDeviceData*)lParam; + + if (lstrcmpiA(p->szDevice, szDevice) == 0) { + if (lpGUID) { + p->GUID = *lpGUID; + p->lpGUID = &p->GUID; + } else { + p->lpGUID = NULL; + } + p->fFound = TRUE; + return FALSE; + } + return TRUE; +} + + +/* + * DirectDrawCreateFromDevice + * + * create a DirectDraw object for a particular device + */ +IDirectDraw * DirectDrawCreateFromDevice(LPSTR szDevice, PDRAWCREATE DirectDrawCreateP, PDRAWENUM DirectDrawEnumerateP) +{ + IDirectDraw* pdd = NULL; + FindDeviceData find; + + if (szDevice == NULL) { + DirectDrawCreateP(NULL, &pdd, NULL); + return pdd; + } + + find.szDevice = szDevice; + find.fFound = FALSE; + DirectDrawEnumerateP(FindDeviceCallback, (LPVOID)&find); + + if (find.fFound) + { + // + // In 4bpp mode the following DDraw call causes a message box to be popped + // up by DDraw (!?!). It's DDraw's fault, but we don't like it. So we + // make sure it doesn't happen. + // + UINT ErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS); + DirectDrawCreateP(find.lpGUID, &pdd, NULL); + SetErrorMode(ErrorMode); + } + + return pdd; +} + + +/* + * DirectDrawCreateFromDeviceEx + * + * create a DirectDraw object for a particular device + */ +IDirectDraw * DirectDrawCreateFromDeviceEx(LPSTR szDevice, PDRAWCREATE DirectDrawCreateP, LPDIRECTDRAWENUMERATEEXA DirectDrawEnumerateExP) +{ + IDirectDraw* pdd = NULL; + FindDeviceData find; + + if (szDevice == NULL) { + DirectDrawCreateP(NULL, &pdd, NULL); + return pdd; + } + + find.szDevice = szDevice; + find.fFound = FALSE; + DirectDrawEnumerateExP(FindDeviceCallbackEx, (LPVOID)&find, + DDENUM_ATTACHEDSECONDARYDEVICES); + + if (find.fFound) + { + // + // In 4bpp mode the following DDraw call causes a message box to be popped + // up by DDraw (!?!). It's DDraw's fault, but we don't like it. So we + // make sure it doesn't happen. + // + UINT ErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS); + DirectDrawCreateP(find.lpGUID, &pdd, NULL); + SetErrorMode(ErrorMode); + } + + return pdd; +} diff --git a/plugins/GSdx/baseclasses/ddmm.h b/plugins/GSdx/baseclasses/ddmm.h new file mode 100644 index 0000000000..678bec3f66 --- /dev/null +++ b/plugins/GSdx/baseclasses/ddmm.h @@ -0,0 +1,28 @@ +//------------------------------------------------------------------------------ +// File: DDMM.h +// +// Desc: DirectShow base classes - efines routines for using DirectDraw +// on a multimonitor system. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifdef __cplusplus +extern "C" { /* Assume C declarations for C++ */ +#endif /* __cplusplus */ + +// DDRAW.H might not include these +#ifndef DDENUM_ATTACHEDSECONDARYDEVICES +#define DDENUM_ATTACHEDSECONDARYDEVICES 0x00000001L +#endif + +typedef HRESULT (*PDRAWCREATE)(IID *,LPDIRECTDRAW *,LPUNKNOWN); +typedef HRESULT (*PDRAWENUM)(LPDDENUMCALLBACKA, LPVOID); + +IDirectDraw * DirectDrawCreateFromDevice(LPSTR, PDRAWCREATE, PDRAWENUM); +IDirectDraw * DirectDrawCreateFromDeviceEx(LPSTR, PDRAWCREATE, LPDIRECTDRAWENUMERATEEXA); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ diff --git a/plugins/GSdx/baseclasses/dsschedule.h b/plugins/GSdx/baseclasses/dsschedule.h new file mode 100644 index 0000000000..cc21b1c3ce --- /dev/null +++ b/plugins/GSdx/baseclasses/dsschedule.h @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// File: DSSchedule.h (replaces DirectX 8's schedule.h) +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __CAMSchedule__ +#define __CAMSchedule__ + +class CAMSchedule : private CBaseObject +{ +public: + virtual ~CAMSchedule(); + // ev is the event we should fire if the advise time needs re-evaluating + CAMSchedule( HANDLE ev ); + + DWORD GetAdviseCount(); + REFERENCE_TIME GetNextAdviseTime(); + + // We need a method for derived classes to add advise packets, we return the cookie + DWORD_PTR AddAdvisePacket( const REFERENCE_TIME & time1, const REFERENCE_TIME & time2, HANDLE h, BOOL periodic ); + // And a way to cancel + HRESULT Unadvise(DWORD_PTR dwAdviseCookie); + + // Tell us the time please, and we'll dispatch the expired events. We return the time of the next event. + // NB: The time returned will be "useless" if you start adding extra Advises. But that's the problem of + // whoever is using this helper class (typically a clock). + REFERENCE_TIME Advise( const REFERENCE_TIME & rtTime ); + + // Get the event handle which will be set if advise time requires re-evaluation. + HANDLE GetEvent() const { return m_ev; } + +private: + // We define the nodes that will be used in our singly linked list + // of advise packets. The list is ordered by time, with the + // elements that will expire first at the front. + class CAdvisePacket + { + public: + CAdvisePacket() + {} + + CAdvisePacket * m_next; + DWORD_PTR m_dwAdviseCookie; + REFERENCE_TIME m_rtEventTime; // Time at which event should be set + REFERENCE_TIME m_rtPeriod; // Periodic time + HANDLE m_hNotify; // Handle to event or semephore + BOOL m_bPeriodic; // TRUE => Periodic event + + CAdvisePacket( CAdvisePacket * next, LONGLONG time ) : m_next(next), m_rtEventTime(time) + {} + + void InsertAfter( CAdvisePacket * p ) + { + p->m_next = m_next; + m_next = p; + } + + int IsZ() const // That is, is it the node that represents the end of the list + { return m_next == 0; } + + CAdvisePacket * RemoveNext() + { + CAdvisePacket *const next = m_next; + CAdvisePacket *const new_next = next->m_next; + m_next = new_next; + return next; + } + + void DeleteNext() + { + delete RemoveNext(); + } + + CAdvisePacket * Next() const + { + CAdvisePacket * result = m_next; + if (result->IsZ()) result = 0; + return result; + } + + DWORD_PTR Cookie() const + { return m_dwAdviseCookie; } + }; + + // Structure is: + // head -> elmt1 -> elmt2 -> z -> null + // So an empty list is: head -> z -> null + // Having head & z as links makes insertaion, + // deletion and shunting much easier. + CAdvisePacket head, z; // z is both a tail and a sentry + + volatile DWORD_PTR m_dwNextCookie; // Strictly increasing + volatile DWORD m_dwAdviseCount; // Number of elements on list + + CCritSec m_Serialize; + + // AddAdvisePacket: adds the packet, returns the cookie (0 if failed) + DWORD_PTR AddAdvisePacket( CAdvisePacket * pPacket ); + // Event that we should set if the packed added above will be the next to fire. + const HANDLE m_ev; + + // A Shunt is where we have changed the first element in the + // list and want it re-evaluating (i.e. repositioned) in + // the list. + void ShuntHead(); + + // Rather than delete advise packets, we cache them for future use + CAdvisePacket * m_pAdviseCache; + DWORD m_dwCacheCount; + enum { dwCacheMax = 5 }; // Don't bother caching more than five + + void Delete( CAdvisePacket * pLink );// This "Delete" will cache the Link + +// Attributes and methods for debugging +public: +#ifdef DEBUG + void DumpLinkedList(); +#else + void DumpLinkedList() {} +#endif + +}; + +#endif // __CAMSchedule__ diff --git a/plugins/GSdx/baseclasses/fourcc.h b/plugins/GSdx/baseclasses/fourcc.h new file mode 100644 index 0000000000..dea8171a6b --- /dev/null +++ b/plugins/GSdx/baseclasses/fourcc.h @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// File: FourCC.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// FOURCCMap +// +// provides a mapping between old-style multimedia format DWORDs +// and new-style GUIDs. +// +// A range of 4 billion GUIDs has been allocated to ensure that this +// mapping can be done straightforwardly one-to-one in both directions. +// +// January 95 + + +#ifndef __FOURCC__ +#define __FOURCC__ + + +// Multimedia format types are marked with DWORDs built from four 8-bit +// chars and known as FOURCCs. New multimedia AM_MEDIA_TYPE definitions include +// a subtype GUID. In order to simplify the mapping, GUIDs in the range: +// XXXXXXXX-0000-0010-8000-00AA00389B71 +// are reserved for FOURCCs. + +class FOURCCMap : public GUID +{ + +public: + FOURCCMap(); + FOURCCMap(DWORD Fourcc); + FOURCCMap(const GUID *); + + + DWORD GetFOURCC(void); + void SetFOURCC(DWORD fourcc); + void SetFOURCC(const GUID *); + +private: + void InitGUID(); +}; + +#define GUID_Data2 0 +#define GUID_Data3 0x10 +#define GUID_Data4_1 0xaa000080 +#define GUID_Data4_2 0x719b3800 + +inline void +FOURCCMap::InitGUID() { + Data2 = GUID_Data2; + Data3 = GUID_Data3; + ((DWORD *)Data4)[0] = GUID_Data4_1; + ((DWORD *)Data4)[1] = GUID_Data4_2; +} + +inline +FOURCCMap::FOURCCMap() { + InitGUID(); + SetFOURCC( DWORD(0)); +} + +inline +FOURCCMap::FOURCCMap(DWORD fourcc) +{ + InitGUID(); + SetFOURCC(fourcc); +} + +inline +FOURCCMap::FOURCCMap(const GUID * pGuid) +{ + InitGUID(); + SetFOURCC(pGuid); +} + +inline void +FOURCCMap::SetFOURCC(const GUID * pGuid) +{ + FOURCCMap * p = (FOURCCMap*) pGuid; + SetFOURCC(p->GetFOURCC()); +} + +inline void +FOURCCMap::SetFOURCC(DWORD fourcc) +{ + Data1 = fourcc; +} + +inline DWORD +FOURCCMap::GetFOURCC(void) +{ + return Data1; +} + +#endif /* __FOURCC__ */ + diff --git a/plugins/GSdx/baseclasses/measure.h b/plugins/GSdx/baseclasses/measure.h new file mode 100644 index 0000000000..75365e3a66 --- /dev/null +++ b/plugins/GSdx/baseclasses/measure.h @@ -0,0 +1,222 @@ +//------------------------------------------------------------------------------ +// File: Measure.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +/* + The idea is to pepper the source code with interesting measurements and + have the last few thousand of these recorded in a circular buffer that + can be post-processed to give interesting numbers. + + WHAT THE LOG LOOKS LIKE: + + Time (sec) Type Delta Incident_Name + 0.055,41 NOTE -. Incident Nine - Another note + 0.055,42 NOTE 0.000,01 Incident Nine - Another note + 0.055,44 NOTE 0.000,02 Incident Nine - Another note + 0.055,45 STOP -. Incident Eight - Also random + 0.055,47 START -. Incident Seven - Random + 0.055,49 NOTE 0.000,05 Incident Nine - Another note + ------- ---------------- + 0.125,60 STOP 0.000,03 Msr_Stop + 0.125,62 START -. Msr_Start + 0.125,63 START -. Incident Two - Start/Stop + 0.125,65 STOP 0.000,03 Msr_Start + 0.125,66 START -. Msr_Stop + 0.125,68 STOP 0.000,05 Incident Two - Start/Stop + 0.125,70 STOP 0.000,04 Msr_Stop + 0.125,72 START -. Msr_Start + 0.125,73 START -. Incident Two - Start/Stop + 0.125,75 STOP 0.000,03 Msr_Start + 0.125,77 START -. Msr_Stop + 0.125,78 STOP 0.000,05 Incident Two - Start/Stop + 0.125,80 STOP 0.000,03 Msr_Stop + 0.125,81 NOTE -. Incident Three - single Note + 0.125,83 START -. Incident Four - Start, no stop + 0.125,85 START -. Incident Five - Single Start/Stop + 0.125,87 STOP 0.000,02 Incident Five - Single Start/Stop + +Number Average StdDev Smallest Largest Incident_Name + 10 0.000,58 0.000,10 0.000,55 0.000,85 Incident One - Note + 50 0.000,05 0.000,00 0.000,05 0.000,05 Incident Two - Start/Stop + 1 -. -. -. -. Incident Three - single Note + 0 -. -. -. -. Incident Four - Start, no stop + 1 0.000,02 -. 0.000,02 0.000,02 Incident Five - Single Start/Stop + 0 -. -. -. -. Incident Six - zero occurrences + 100 0.000,25 0.000,12 0.000,02 0.000,62 Incident Seven - Random + 100 0.000,79 0.000,48 0.000,02 0.001,92 Incident Eight - Also random + 5895 0.000,01 0.000,01 0.000,01 0.000,56 Incident Nine - Another note + 10 0.000,03 0.000,00 0.000,03 0.000,04 Msr_Note + 50 0.000,03 0.000,00 0.000,03 0.000,04 Msr_Start + 50 0.000,04 0.000,03 0.000,03 0.000,31 Msr_Stop + + WHAT IT MEANS: + The log shows what happened and when. Each line shows the time at which + something happened (see WHAT YOU CODE below) what it was that happened + and (if approporate) the time since the corresponding previous event + (that's the delta column). + + The statistics show how many times each event occurred, what the average + delta time was, also the standard deviation, largest and smalles delta. + + WHAT YOU CODE: + + Before anything else executes: - register your ids + + int id1 = Msr_Register("Incident One - Note"); + int id2 = Msr_Register("Incident Two - Start/Stop"); + int id3 = Msr_Register("Incident Three - single Note"); + etc. + + At interesting moments: + + // To measure a repetitive event - e.g. end of bitblt to screen + Msr_Note(Id9); // e.g. "video frame hiting the screen NOW!" + + or + + // To measure an elapsed time e.g. time taken to decode an MPEG B-frame + Msr_Start(Id2); // e.g. "Starting to decode MPEG B-frame" + . . . + MsrStop(Id2); // "Finished MPEG decode" + + At the end: + + HANDLE hFile; + hFile = CreateFile("Perf.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); + Msr_Dump(hFile); // This writes the log out to the file + CloseHandle(hFile); + + or + + Msr_Dump(NULL); // This writes it to DbgLog((LOG_TRACE,0, ... )); + // but if you are writing it out to the debugger + // then the times are probably all garbage because + // the debugger can make things run awfully slow. + + A given id should be used either for start / stop or Note calls. If Notes + are mixed in with Starts and Stops their statistics will be gibberish. + + If you code the calls in upper case i.e. MSR_START(idMunge); then you get + macros which will turn into nothing unless PERF is defined. + + You can reset the statistical counts for a given id by calling Reset(Id). + They are reset by default at the start. + It logs Reset as a special incident, so you can see it in the log. + + The log is a circular buffer in storage (to try to minimise disk I/O). + It overwrites the oldest entries once full. The statistics include ALL + incidents since the last Reset, whether still visible in the log or not. +*/ + +#ifndef __MEASURE__ +#define __MEASURE__ + +#ifdef PERF +#define MSR_INIT() Msr_Init() +#define MSR_TERMINATE() Msr_Terminate() +#define MSR_REGISTER(a) Msr_Register(a) +#define MSR_RESET(a) Msr_Reset(a) +#define MSR_CONTROL(a) Msr_Control(a) +#define MSR_START(a) Msr_Start(a) +#define MSR_STOP(a) Msr_Stop(a) +#define MSR_NOTE(a) Msr_Note(a) +#define MSR_INTEGER(a,b) Msr_Integer(a,b) +#define MSR_DUMP(a) Msr_Dump(a) +#define MSR_DUMPSTATS(a) Msr_DumpStats(a) +#else +#define MSR_INIT() ((void)0) +#define MSR_TERMINATE() ((void)0) +#define MSR_REGISTER(a) 0 +#define MSR_RESET(a) ((void)0) +#define MSR_CONTROL(a) ((void)0) +#define MSR_START(a) ((void)0) +#define MSR_STOP(a) ((void)0) +#define MSR_NOTE(a) ((void)0) +#define MSR_INTEGER(a,b) ((void)0) +#define MSR_DUMP(a) ((void)0) +#define MSR_DUMPSTATS(a) ((void)0) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// This must be called first - (called by the DllEntry) + +void WINAPI Msr_Init(void); + + +// Call this last to clean up (or just let it fall off the end - who cares?) + +void WINAPI Msr_Terminate(void); + + +// Call this to get an Id for an "incident" that you can pass to Start, Stop or Note +// everything that's logged is called an "incident". + +int WINAPI Msr_Register(LPTSTR Incident); + + +// Reset the statistical counts for an incident + +void WINAPI Msr_Reset(int Id); + + +// Reset all the counts for all incidents +#define MSR_RESET_ALL 0 +#define MSR_PAUSE 1 +#define MSR_RUN 2 + +void WINAPI Msr_Control(int iAction); + + +// log the start of an operation + +void WINAPI Msr_Start(int Id); + + +// log the end of an operation + +void WINAPI Msr_Stop(int Id); + + +// log a one-off or repetitive operation + +void WINAPI Msr_Note(int Id); + + +// log an integer (on which we can see statistics later) +void WINAPI Msr_Integer(int Id, int n); + + +// print out all the vaialable log (it may have wrapped) and then the statistics. +// When the log wraps you lose log but the statistics are still complete. +// hFIle==NULL => use DbgLog +// otherwise hFile must have come from CreateFile or OpenFile. + +void WINAPI Msr_Dump(HANDLE hFile); + + +// just dump the statistics - never mind the log + +void WINAPI Msr_DumpStats(HANDLE hFile); + +// Type definitions in case you want to declare a pointer to the dump functions +// (makes it a trifle easier to do dynamic linking +// i.e. LoadModule, GetProcAddress and call that) + +// Typedefs so can declare MSR_DUMPPROC *MsrDumpStats; or whatever +typedef void WINAPI MSR_DUMPPROC(HANDLE hFile); +typedef void WINAPI MSR_CONTROLPROC(int iAction); + + +#ifdef __cplusplus +} +#endif + +#endif // __MEASURE__ diff --git a/plugins/GSdx/baseclasses/msgthrd.h b/plugins/GSdx/baseclasses/msgthrd.h new file mode 100644 index 0000000000..f07d5162ad --- /dev/null +++ b/plugins/GSdx/baseclasses/msgthrd.h @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// File: MsgThrd.h +// +// Desc: DirectShow base classes - provides support for a worker thread +// class to which one can asynchronously post messages. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// Message class - really just a structure. +// +class CMsg { +public: + UINT uMsg; + DWORD dwFlags; + LPVOID lpParam; + CAMEvent *pEvent; + + CMsg(UINT u, DWORD dw, LPVOID lp, CAMEvent *pEvnt) + : uMsg(u), dwFlags(dw), lpParam(lp), pEvent(pEvnt) {} + + CMsg() + : uMsg(0), dwFlags(0L), lpParam(NULL), pEvent(NULL) {} +}; + +// This is the actual thread class. It exports all the usual thread control +// functions. The created thread is different from a normal WIN32 thread in +// that it is prompted to perform particaular tasks by responding to messages +// posted to its message queue. +// +class AM_NOVTABLE CMsgThread { +private: + static DWORD WINAPI DefaultThreadProc(LPVOID lpParam); + DWORD m_ThreadId; + HANDLE m_hThread; + +protected: + + // if you want to override GetThreadMsg to block on other things + // as well as this queue, you need access to this + CGenericList m_ThreadQueue; + CCritSec m_Lock; + HANDLE m_hSem; + LONG m_lWaiting; + +public: + CMsgThread() + : m_ThreadId(0), + m_hThread(NULL), + m_lWaiting(0), + m_hSem(NULL), + // make a list with a cache of 5 items + m_ThreadQueue(NAME("MsgThread list"), 5) + { + } + + ~CMsgThread(); + // override this if you want to block on other things as well + // as the message loop + void virtual GetThreadMsg(CMsg *msg); + + // override this if you want to do something on thread startup + virtual void OnThreadInit() { + }; + + BOOL CreateThread(); + + BOOL WaitForThreadExit(LPDWORD lpdwExitCode) { + if (m_hThread != NULL) { + WaitForSingleObject(m_hThread, INFINITE); + return GetExitCodeThread(m_hThread, lpdwExitCode); + } + return FALSE; + } + + DWORD ResumeThread() { + return ::ResumeThread(m_hThread); + } + + DWORD SuspendThread() { + return ::SuspendThread(m_hThread); + } + + int GetThreadPriority() { + return ::GetThreadPriority(m_hThread); + } + + BOOL SetThreadPriority(int nPriority) { + return ::SetThreadPriority(m_hThread, nPriority); + } + + HANDLE GetThreadHandle() { + return m_hThread; + } + + DWORD GetThreadId() { + return m_ThreadId; + } + + + void PutThreadMsg(UINT uMsg, DWORD dwMsgFlags, + LPVOID lpMsgParam, CAMEvent *pEvent = NULL) { + CAutoLock lck(&m_Lock); + CMsg* pMsg = new CMsg(uMsg, dwMsgFlags, lpMsgParam, pEvent); + m_ThreadQueue.AddTail(pMsg); + if (m_lWaiting != 0) { + ReleaseSemaphore(m_hSem, m_lWaiting, 0); + m_lWaiting = 0; + } + } + + // This is the function prototype of the function that the client + // supplies. It is always called on the created thread, never on + // the creator thread. + // + virtual LRESULT ThreadMessageProc( + UINT uMsg, DWORD dwFlags, LPVOID lpParam, CAMEvent *pEvent) = 0; +}; + diff --git a/plugins/GSdx/baseclasses/mtype.cpp b/plugins/GSdx/baseclasses/mtype.cpp new file mode 100644 index 0000000000..d56c15a5da --- /dev/null +++ b/plugins/GSdx/baseclasses/mtype.cpp @@ -0,0 +1,477 @@ +//------------------------------------------------------------------------------ +// File: MType.cpp +// +// Desc: DirectShow base classes - implements a class that holds and +// manages media type information. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// helper class that derived pin objects can use to compare media +// types etc. Has same data members as the struct AM_MEDIA_TYPE defined +// in the streams IDL file, but also has (non-virtual) functions + +#include "streams.h" +#include + +CMediaType::~CMediaType(){ + FreeMediaType(*this); +} + + +CMediaType::CMediaType() +{ + InitMediaType(); +} + + +CMediaType::CMediaType(const GUID * type) +{ + InitMediaType(); + majortype = *type; +} + + +// copy constructor does a deep copy of the format block + +CMediaType::CMediaType(const AM_MEDIA_TYPE& rt, HRESULT* phr) +{ + HRESULT hr = CopyMediaType(this, &rt); + if (FAILED(hr) && (NULL != phr)) { + *phr = hr; + } +} + + +CMediaType::CMediaType(const CMediaType& rt, HRESULT* phr) +{ + HRESULT hr = CopyMediaType(this, &rt); + if (FAILED(hr) && (NULL != phr)) { + *phr = hr; + } +} + + +// this class inherits publicly from AM_MEDIA_TYPE so the compiler could generate +// the following assignment operator itself, however it could introduce some +// memory conflicts and leaks in the process because the structure contains +// a dynamically allocated block (pbFormat) which it will not copy correctly + +CMediaType& +CMediaType::operator=(const AM_MEDIA_TYPE& rt) +{ + Set(rt); + return *this; +} + +CMediaType& +CMediaType::operator=(const CMediaType& rt) +{ + *this = (AM_MEDIA_TYPE &) rt; + return *this; +} + +BOOL +CMediaType::operator == (const CMediaType& rt) const +{ + // I don't believe we need to check sample size or + // temporal compression flags, since I think these must + // be represented in the type, subtype and format somehow. They + // are pulled out as separate flags so that people who don't understand + // the particular format representation can still see them, but + // they should duplicate information in the format block. + + return ((IsEqualGUID(majortype,rt.majortype) == TRUE) && + (IsEqualGUID(subtype,rt.subtype) == TRUE) && + (IsEqualGUID(formattype,rt.formattype) == TRUE) && + (cbFormat == rt.cbFormat) && + ( (cbFormat == 0) || + (memcmp(pbFormat, rt.pbFormat, cbFormat) == 0))); +} + + +BOOL +CMediaType::operator != (const CMediaType& rt) const +{ + /* Check to see if they are equal */ + + if (*this == rt) { + return FALSE; + } + return TRUE; +} + + +HRESULT +CMediaType::Set(const CMediaType& rt) +{ + return Set((AM_MEDIA_TYPE &) rt); +} + + +HRESULT +CMediaType::Set(const AM_MEDIA_TYPE& rt) +{ + if (&rt != this) { + FreeMediaType(*this); + HRESULT hr = CopyMediaType(this, &rt); + if (FAILED(hr)) { + return E_OUTOFMEMORY; + } + } + + return S_OK; +} + + +BOOL +CMediaType::IsValid() const +{ + return (!IsEqualGUID(majortype,GUID_NULL)); +} + + +void +CMediaType::SetType(const GUID* ptype) +{ + majortype = *ptype; +} + + +void +CMediaType::SetSubtype(const GUID* ptype) +{ + subtype = *ptype; +} + + +ULONG +CMediaType::GetSampleSize() const { + if (IsFixedSize()) { + return lSampleSize; + } else { + return 0; + } +} + + +void +CMediaType::SetSampleSize(ULONG sz) { + if (sz == 0) { + SetVariableSize(); + } else { + bFixedSizeSamples = TRUE; + lSampleSize = sz; + } +} + + +void +CMediaType::SetVariableSize() { + bFixedSizeSamples = FALSE; +} + + +void +CMediaType::SetTemporalCompression(BOOL bCompressed) { + bTemporalCompression = bCompressed; +} + +BOOL +CMediaType::SetFormat(BYTE * pformat, ULONG cb) +{ + if (NULL == AllocFormatBuffer(cb)) + return(FALSE); + + ASSERT(pbFormat); + memcpy(pbFormat, pformat, cb); + return(TRUE); +} + + +// set the type of the media type format block, this type defines what you +// will actually find in the format pointer. For example FORMAT_VideoInfo or +// FORMAT_WaveFormatEx. In the future this may be an interface pointer to a +// property set. Before sending out media types this should be filled in. + +void +CMediaType::SetFormatType(const GUID *pformattype) +{ + formattype = *pformattype; +} + + +// reset the format buffer + +void CMediaType::ResetFormatBuffer() +{ + if (cbFormat) { + CoTaskMemFree((PVOID)pbFormat); + } + cbFormat = 0; + pbFormat = NULL; +} + + +// allocate length bytes for the format and return a read/write pointer +// If we cannot allocate the new block of memory we return NULL leaving +// the original block of memory untouched (as does ReallocFormatBuffer) + +BYTE* +CMediaType::AllocFormatBuffer(ULONG length) +{ + ASSERT(length); + + // do the types have the same buffer size + + if (cbFormat == length) { + return pbFormat; + } + + // allocate the new format buffer + + BYTE *pNewFormat = (PBYTE)CoTaskMemAlloc(length); + if (pNewFormat == NULL) { + if (length <= cbFormat) return pbFormat; //reuse the old block anyway. + return NULL; + } + + // delete the old format + + if (cbFormat != 0) { + ASSERT(pbFormat); + CoTaskMemFree((PVOID)pbFormat); + } + + cbFormat = length; + pbFormat = pNewFormat; + return pbFormat; +} + + +// reallocate length bytes for the format and return a read/write pointer +// to it. We keep as much information as we can given the new buffer size +// if this fails the original format buffer is left untouched. The caller +// is responsible for ensuring the size of memory required is non zero + +BYTE* +CMediaType::ReallocFormatBuffer(ULONG length) +{ + ASSERT(length); + + // do the types have the same buffer size + + if (cbFormat == length) { + return pbFormat; + } + + // allocate the new format buffer + + BYTE *pNewFormat = (PBYTE)CoTaskMemAlloc(length); + if (pNewFormat == NULL) { + if (length <= cbFormat) return pbFormat; //reuse the old block anyway. + return NULL; + } + + // copy any previous format (or part of if new is smaller) + // delete the old format and replace with the new one + + if (cbFormat != 0) { + ASSERT(pbFormat); + memcpy(pNewFormat,pbFormat,min(length,cbFormat)); + CoTaskMemFree((PVOID)pbFormat); + } + + cbFormat = length; + pbFormat = pNewFormat; + return pNewFormat; +} + +// initialise a media type structure + +void CMediaType::InitMediaType() +{ + ZeroMemory((PVOID)this, sizeof(*this)); + lSampleSize = 1; + bFixedSizeSamples = TRUE; +} + + +// a partially specified media type can be passed to IPin::Connect +// as a constraint on the media type used in the connection. +// the type, subtype or format type can be null. +BOOL +CMediaType::IsPartiallySpecified(void) const +{ + if ((majortype == GUID_NULL) || + (formattype == GUID_NULL)) { + return TRUE; + } else { + return FALSE; + } +} + +BOOL +CMediaType::MatchesPartial(const CMediaType* ppartial) const +{ + if ((ppartial->majortype != GUID_NULL) && + (majortype != ppartial->majortype)) { + return FALSE; + } + if ((ppartial->subtype != GUID_NULL) && + (subtype != ppartial->subtype)) { + return FALSE; + } + + if (ppartial->formattype != GUID_NULL) { + // if the format block is specified then it must match exactly + if (formattype != ppartial->formattype) { + return FALSE; + } + if (cbFormat != ppartial->cbFormat) { + return FALSE; + } + if ((cbFormat != 0) && + (memcmp(pbFormat, ppartial->pbFormat, cbFormat) != 0)) { + return FALSE; + } + } + + return TRUE; + +} + + + +// general purpose function to delete a heap allocated AM_MEDIA_TYPE structure +// which is useful when calling IEnumMediaTypes::Next as the interface +// implementation allocates the structures which you must later delete +// the format block may also be a pointer to an interface to release + +void WINAPI DeleteMediaType(AM_MEDIA_TYPE *pmt) +{ + // allow NULL pointers for coding simplicity + + if (pmt == NULL) { + return; + } + + FreeMediaType(*pmt); + CoTaskMemFree((PVOID)pmt); +} + + +// this also comes in useful when using the IEnumMediaTypes interface so +// that you can copy a media type, you can do nearly the same by creating +// a CMediaType object but as soon as it goes out of scope the destructor +// will delete the memory it allocated (this takes a copy of the memory) + +AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const *pSrc) +{ + ASSERT(pSrc); + + // Allocate a block of memory for the media type + + AM_MEDIA_TYPE *pMediaType = + (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE)); + + if (pMediaType == NULL) { + return NULL; + } + // Copy the variable length format block + + HRESULT hr = CopyMediaType(pMediaType,pSrc); + if (FAILED(hr)) { + CoTaskMemFree((PVOID)pMediaType); + return NULL; + } + + return pMediaType; +} + + +// Copy 1 media type to another + +HRESULT WINAPI CopyMediaType(AM_MEDIA_TYPE *pmtTarget, const AM_MEDIA_TYPE *pmtSource) +{ + // We'll leak if we copy onto one that already exists - there's one + // case we can check like that - copying to itself. + ASSERT(pmtSource != pmtTarget); + *pmtTarget = *pmtSource; + if (pmtSource->cbFormat != 0) { + ASSERT(pmtSource->pbFormat != NULL); + pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc(pmtSource->cbFormat); + if (pmtTarget->pbFormat == NULL) { + pmtTarget->cbFormat = 0; + return E_OUTOFMEMORY; + } else { + CopyMemory((PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat, + pmtTarget->cbFormat); + } + } + if (pmtTarget->pUnk != NULL) { + pmtTarget->pUnk->AddRef(); + } + + return S_OK; +} + +// Free an existing media type (ie free resources it holds) + +void WINAPI FreeMediaType(AM_MEDIA_TYPE& mt) +{ + if (mt.cbFormat != 0) { + CoTaskMemFree((PVOID)mt.pbFormat); + + // Strictly unnecessary but tidier + mt.cbFormat = 0; + mt.pbFormat = NULL; + } + if (mt.pUnk != NULL) { + mt.pUnk->Release(); + mt.pUnk = NULL; + } +} + +// Initialize a media type from a WAVEFORMATEX + +STDAPI CreateAudioMediaType( + const WAVEFORMATEX *pwfx, + AM_MEDIA_TYPE *pmt, + BOOL bSetFormat +) +{ + pmt->majortype = MEDIATYPE_Audio; + if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + pmt->subtype = ((PWAVEFORMATEXTENSIBLE)pwfx)->SubFormat; + } else { + pmt->subtype = FOURCCMap(pwfx->wFormatTag); + } + pmt->formattype = FORMAT_WaveFormatEx; + pmt->bFixedSizeSamples = TRUE; + pmt->bTemporalCompression = FALSE; + pmt->lSampleSize = pwfx->nBlockAlign; + pmt->pUnk = NULL; + if (bSetFormat) { + if (pwfx->wFormatTag == WAVE_FORMAT_PCM) { + pmt->cbFormat = sizeof(WAVEFORMATEX); + } else { + pmt->cbFormat = sizeof(WAVEFORMATEX) + pwfx->cbSize; + } + pmt->pbFormat = (PBYTE)CoTaskMemAlloc(pmt->cbFormat); + if (pmt->pbFormat == NULL) { + return E_OUTOFMEMORY; + } + if (pwfx->wFormatTag == WAVE_FORMAT_PCM) { + CopyMemory(pmt->pbFormat, pwfx, sizeof(PCMWAVEFORMAT)); + ((WAVEFORMATEX *)pmt->pbFormat)->cbSize = 0; + } else { + CopyMemory(pmt->pbFormat, pwfx, pmt->cbFormat); + } + } + return S_OK; +} + +// eliminate very many spurious warnings from MS compiler +#pragma warning(disable:4514) diff --git a/plugins/GSdx/baseclasses/mtype.h b/plugins/GSdx/baseclasses/mtype.h new file mode 100644 index 0000000000..7a8a2395b3 --- /dev/null +++ b/plugins/GSdx/baseclasses/mtype.h @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// File: MtType.h +// +// Desc: DirectShow base classes - defines a class that holds and manages +// media type information. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __MTYPE__ +#define __MTYPE__ + +/* Helper class that derived pin objects can use to compare media + types etc. Has same data members as the struct AM_MEDIA_TYPE defined + in the streams IDL file, but also has (non-virtual) functions */ + +class CMediaType : public _AMMediaType { + +public: + + ~CMediaType(); + CMediaType(); + CMediaType(const GUID * majortype); + CMediaType(const AM_MEDIA_TYPE&, HRESULT* phr = NULL); + CMediaType(const CMediaType&, HRESULT* phr = NULL); + + CMediaType& operator=(const CMediaType&); + CMediaType& operator=(const AM_MEDIA_TYPE&); + + BOOL operator == (const CMediaType&) const; + BOOL operator != (const CMediaType&) const; + + HRESULT Set(const CMediaType& rt); + HRESULT Set(const AM_MEDIA_TYPE& rt); + + BOOL IsValid() const; + + const GUID *Type() const { return &majortype;} ; + void SetType(const GUID *); + const GUID *Subtype() const { return &subtype;} ; + void SetSubtype(const GUID *); + + BOOL IsFixedSize() const {return bFixedSizeSamples; }; + BOOL IsTemporalCompressed() const {return bTemporalCompression; }; + ULONG GetSampleSize() const; + + void SetSampleSize(ULONG sz); + void SetVariableSize(); + void SetTemporalCompression(BOOL bCompressed); + + // read/write pointer to format - can't change length without + // calling SetFormat, AllocFormatBuffer or ReallocFormatBuffer + + BYTE* Format() const {return pbFormat; }; + ULONG FormatLength() const { return cbFormat; }; + + void SetFormatType(const GUID *); + const GUID *FormatType() const {return &formattype; }; + BOOL SetFormat(BYTE *pFormat, ULONG length); + void ResetFormatBuffer(); + BYTE* AllocFormatBuffer(ULONG length); + BYTE* ReallocFormatBuffer(ULONG length); + + void InitMediaType(); + + BOOL MatchesPartial(const CMediaType* ppartial) const; + BOOL IsPartiallySpecified(void) const; +}; + + +/* General purpose functions to copy and delete a task allocated AM_MEDIA_TYPE + structure which is useful when using the IEnumMediaFormats interface as + the implementation allocates the structures which you must later delete */ + +void WINAPI DeleteMediaType(AM_MEDIA_TYPE *pmt); +AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const *pSrc); +HRESULT WINAPI CopyMediaType(AM_MEDIA_TYPE *pmtTarget, const AM_MEDIA_TYPE *pmtSource); +void WINAPI FreeMediaType(AM_MEDIA_TYPE& mt); + +// Initialize a media type from a WAVEFORMATEX + +STDAPI CreateAudioMediaType( + const WAVEFORMATEX *pwfx, + AM_MEDIA_TYPE *pmt, + BOOL bSetFormat); + +#endif /* __MTYPE__ */ + diff --git a/plugins/GSdx/baseclasses/outputq.cpp b/plugins/GSdx/baseclasses/outputq.cpp new file mode 100644 index 0000000000..da96a10765 --- /dev/null +++ b/plugins/GSdx/baseclasses/outputq.cpp @@ -0,0 +1,794 @@ +//------------------------------------------------------------------------------ +// File: OutputQ.cpp +// +// Desc: DirectShow base classes - implements COutputQueue class used by an +// output pin which may sometimes want to queue output samples on a +// separate thread and sometimes call Receive() directly on the input +// pin. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" + + +// +// COutputQueue Constructor : +// +// Determines if a thread is to be created and creates resources +// +// pInputPin - the downstream input pin we're queueing samples to +// +// phr - changed to a failure code if this function fails +// (otherwise unchanges) +// +// bAuto - Ask pInputPin if it can block in Receive by calling +// its ReceiveCanBlock method and create a thread if +// it can block, otherwise not. +// +// bQueue - if bAuto == FALSE then we create a thread if and only +// if bQueue == TRUE +// +// lBatchSize - work in batches of lBatchSize +// +// bBatchEact - Use exact batch sizes so don't send until the +// batch is full or SendAnyway() is called +// +// lListSize - If we create a thread make the list of samples queued +// to the thread have this size cache +// +// dwPriority - If we create a thread set its priority to this +// +COutputQueue::COutputQueue( + IPin *pInputPin, // Pin to send stuff to + HRESULT *phr, // 'Return code' + BOOL bAuto, // Ask pin if queue or not + BOOL bQueue, // Send through queue + LONG lBatchSize, // Batch + BOOL bBatchExact, // Batch exactly to BatchSize + LONG lListSize, + DWORD dwPriority, + bool bFlushingOpt // flushing optimization + ) : m_lBatchSize(lBatchSize), + m_bBatchExact(bBatchExact && (lBatchSize > 1)), + m_hThread(NULL), + m_hSem(NULL), + m_List(NULL), + m_pPin(pInputPin), + m_ppSamples(NULL), + m_lWaiting(0), + m_pInputPin(NULL), + m_bSendAnyway(FALSE), + m_nBatched(0), + m_bFlushing(FALSE), + m_bFlushed(TRUE), + m_bFlushingOpt(bFlushingOpt), + m_bTerminate(FALSE), + m_hEventPop(NULL), + m_hr(S_OK) +{ + ASSERT(m_lBatchSize > 0); + + + if (FAILED(*phr)) { + return; + } + + // Check the input pin is OK and cache its IMemInputPin interface + + *phr = pInputPin->QueryInterface(IID_IMemInputPin, (void **)&m_pInputPin); + if (FAILED(*phr)) { + return; + } + + // See if we should ask the downstream pin + + if (bAuto) { + HRESULT hr = m_pInputPin->ReceiveCanBlock(); + if (SUCCEEDED(hr)) { + bQueue = hr == S_OK; + } + } + + // Create our sample batch + + m_ppSamples = new PMEDIASAMPLE[m_lBatchSize]; + if (m_ppSamples == NULL) { + *phr = E_OUTOFMEMORY; + return; + } + + // If we're queueing allocate resources + + if (bQueue) { + DbgLog((LOG_TRACE, 2, TEXT("Creating thread for output pin"))); + m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL); + if (m_hSem == NULL) { + DWORD dwError = GetLastError(); + *phr = AmHresultFromWin32(dwError); + return; + } + m_List = new CSampleList(NAME("Sample Queue List"), + lListSize, + FALSE // No lock + ); + if (m_List == NULL) { + *phr = E_OUTOFMEMORY; + return; + } + + + DWORD dwThreadId; + m_hThread = CreateThread(NULL, + 0, + InitialThreadProc, + (LPVOID)this, + 0, + &dwThreadId); + if (m_hThread == NULL) { + DWORD dwError = GetLastError(); + *phr = AmHresultFromWin32(dwError); + return; + } + SetThreadPriority(m_hThread, dwPriority); + } else { + DbgLog((LOG_TRACE, 2, TEXT("Calling input pin directly - no thread"))); + } +} + +// +// COutputQueuee Destructor : +// +// Free all resources - +// +// Thread, +// Batched samples +// +COutputQueue::~COutputQueue() +{ + DbgLog((LOG_TRACE, 3, TEXT("COutputQueue::~COutputQueue"))); + /* Free our pointer */ + if (m_pInputPin != NULL) { + m_pInputPin->Release(); + } + if (m_hThread != NULL) { + { + CAutoLock lck(this); + m_bTerminate = TRUE; + m_hr = S_FALSE; + NotifyThread(); + } + DbgWaitForSingleObject(m_hThread); + EXECUTE_ASSERT(CloseHandle(m_hThread)); + + // The thread frees the samples when asked to terminate + + ASSERT(m_List->GetCount() == 0); + delete m_List; + } else { + FreeSamples(); + } + if (m_hSem != NULL) { + EXECUTE_ASSERT(CloseHandle(m_hSem)); + } + delete [] m_ppSamples; +} + +// +// Call the real thread proc as a member function +// +DWORD WINAPI COutputQueue::InitialThreadProc(LPVOID pv) +{ + HRESULT hrCoInit = CAMThread::CoInitializeHelper(); + + COutputQueue *pSampleQueue = (COutputQueue *)pv; + DWORD dwReturn = pSampleQueue->ThreadProc(); + + if(hrCoInit == S_OK) { + CoUninitialize(); + } + + return dwReturn; +} + +// +// Thread sending the samples downstream : +// +// When there is nothing to do the thread sets m_lWaiting (while +// holding the critical section) and then waits for m_hSem to be +// set (not holding the critical section) +// +DWORD COutputQueue::ThreadProc() +{ + while (TRUE) { + BOOL bWait = FALSE; + IMediaSample *pSample; + LONG lNumberToSend; // Local copy + NewSegmentPacket* ppacket; + + // + // Get a batch of samples and send it if possible + // In any case exit the loop if there is a control action + // requested + // + { + CAutoLock lck(this); + while (TRUE) { + + if (m_bTerminate) { + FreeSamples(); + return 0; + } + if (m_bFlushing) { + FreeSamples(); + SetEvent(m_evFlushComplete); + } + + // Get a sample off the list + + pSample = m_List->RemoveHead(); + // inform derived class we took something off the queue + if (m_hEventPop) { + //DbgLog((LOG_TRACE,3,TEXT("Queue: Delivered SET EVENT"))); + SetEvent(m_hEventPop); + } + + if (pSample != NULL && + !IsSpecialSample(pSample)) { + + // If its just a regular sample just add it to the batch + // and exit the loop if the batch is full + + m_ppSamples[m_nBatched++] = pSample; + if (m_nBatched == m_lBatchSize) { + break; + } + } else { + + // If there was nothing in the queue and there's nothing + // to send (either because there's nothing or the batch + // isn't full) then prepare to wait + + if (pSample == NULL && + (m_bBatchExact || m_nBatched == 0)) { + + // Tell other thread to set the event when there's + // something do to + + ASSERT(m_lWaiting == 0); + m_lWaiting++; + bWait = TRUE; + } else { + + // We break out of the loop on SEND_PACKET unless + // there's nothing to send + + if (pSample == SEND_PACKET && m_nBatched == 0) { + continue; + } + + if (pSample == NEW_SEGMENT) { + // now we need the parameters - we are + // guaranteed that the next packet contains them + ppacket = (NewSegmentPacket *) m_List->RemoveHead(); + // we took something off the queue + if (m_hEventPop) { + //DbgLog((LOG_TRACE,3,TEXT("Queue: Delivered SET EVENT"))); + SetEvent(m_hEventPop); + } + + ASSERT(ppacket); + } + // EOS_PACKET falls through here and we exit the loop + // In this way it acts like SEND_PACKET + } + break; + } + } + if (!bWait) { + // We look at m_nBatched from the client side so keep + // it up to date inside the critical section + lNumberToSend = m_nBatched; // Local copy + m_nBatched = 0; + } + } + + // Wait for some more data + + if (bWait) { + DbgWaitForSingleObject(m_hSem); + continue; + } + + + + // OK - send it if there's anything to send + // We DON'T check m_bBatchExact here because either we've got + // a full batch or we dropped through because we got + // SEND_PACKET or EOS_PACKET - both of which imply we should + // flush our batch + + if (lNumberToSend != 0) { + long nProcessed; + if (m_hr == S_OK) { + ASSERT(!m_bFlushed); + HRESULT hr = m_pInputPin->ReceiveMultiple(m_ppSamples, + lNumberToSend, + &nProcessed); + /* Don't overwrite a flushing state HRESULT */ + CAutoLock lck(this); + if (m_hr == S_OK) { + m_hr = hr; + } + ASSERT(!m_bFlushed); + } + while (lNumberToSend != 0) { + m_ppSamples[--lNumberToSend]->Release(); + } + if (m_hr != S_OK) { + + // In any case wait for more data - S_OK just + // means there wasn't an error + + DbgLog((LOG_ERROR, 2, TEXT("ReceiveMultiple returned %8.8X"), + m_hr)); + } + } + + // Check for end of stream + + if (pSample == EOS_PACKET) { + + // We don't send even end of stream on if we've previously + // returned something other than S_OK + // This is because in that case the pin which returned + // something other than S_OK should have either sent + // EndOfStream() or notified the filter graph + + if (m_hr == S_OK) { + DbgLog((LOG_TRACE, 2, TEXT("COutputQueue sending EndOfStream()"))); + HRESULT hr = m_pPin->EndOfStream(); + if (FAILED(hr)) { + DbgLog((LOG_ERROR, 2, TEXT("COutputQueue got code 0x%8.8X from EndOfStream()"))); + } + } + } + + // Data from a new source + + if (pSample == RESET_PACKET) { + m_hr = S_OK; + SetEvent(m_evFlushComplete); + } + + if (pSample == NEW_SEGMENT) { + m_pPin->NewSegment(ppacket->tStart, ppacket->tStop, ppacket->dRate); + delete ppacket; + } + } +} + +// Send batched stuff anyway +void COutputQueue::SendAnyway() +{ + if (!IsQueued()) { + + // m_bSendAnyway is a private parameter checked in ReceiveMultiple + + m_bSendAnyway = TRUE; + LONG nProcessed; + ReceiveMultiple(NULL, 0, &nProcessed); + m_bSendAnyway = FALSE; + + } else { + CAutoLock lck(this); + QueueSample(SEND_PACKET); + NotifyThread(); + } +} + +void +COutputQueue::NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate) +{ + if (!IsQueued()) { + if (S_OK == m_hr) { + if (m_bBatchExact) { + SendAnyway(); + } + m_pPin->NewSegment(tStart, tStop, dRate); + } + } else { + if (m_hr == S_OK) { + // + // we need to queue the new segment to appear in order in the + // data, but we need to pass parameters to it. Rather than + // take the hit of wrapping every single sample so we can tell + // special ones apart, we queue special pointers to indicate + // special packets, and we guarantee (by holding the + // critical section) that the packet immediately following a + // NEW_SEGMENT value is a NewSegmentPacket containing the + // parameters. + NewSegmentPacket * ppack = new NewSegmentPacket; + if (ppack == NULL) { + return; + } + ppack->tStart = tStart; + ppack->tStop = tStop; + ppack->dRate = dRate; + + CAutoLock lck(this); + QueueSample(NEW_SEGMENT); + QueueSample( (IMediaSample*) ppack); + NotifyThread(); + } + } +} + + +// +// End of Stream is queued to output device +// +void COutputQueue::EOS() +{ + CAutoLock lck(this); + if (!IsQueued()) { + if (m_bBatchExact) { + SendAnyway(); + } + if (m_hr == S_OK) { + DbgLog((LOG_TRACE, 2, TEXT("COutputQueue sending EndOfStream()"))); + m_bFlushed = FALSE; + HRESULT hr = m_pPin->EndOfStream(); + if (FAILED(hr)) { + DbgLog((LOG_ERROR, 2, TEXT("COutputQueue got code 0x%8.8X from EndOfStream()"))); + } + } + } else { + if (m_hr == S_OK) { + m_bFlushed = FALSE; + QueueSample(EOS_PACKET); + NotifyThread(); + } + } +} + +// +// Flush all the samples in the queue +// +void COutputQueue::BeginFlush() +{ + if (IsQueued()) { + { + CAutoLock lck(this); + + // block receives -- we assume this is done by the + // filter in which we are a component + + // discard all queued data + + m_bFlushing = TRUE; + + // Make sure we discard all samples from now on + + if (m_hr == S_OK) { + m_hr = S_FALSE; + } + + // Optimize so we don't keep calling downstream all the time + + if (m_bFlushed && m_bFlushingOpt) { + return; + } + + // Make sure we really wait for the flush to complete + m_evFlushComplete.Reset(); + + NotifyThread(); + } + + // pass this downstream + + m_pPin->BeginFlush(); + } else { + // pass downstream first to avoid deadlocks + m_pPin->BeginFlush(); + CAutoLock lck(this); + // discard all queued data + + m_bFlushing = TRUE; + + // Make sure we discard all samples from now on + + if (m_hr == S_OK) { + m_hr = S_FALSE; + } + } + +} + +// +// leave flush mode - pass this downstream +void COutputQueue::EndFlush() +{ + { + CAutoLock lck(this); + ASSERT(m_bFlushing); + if (m_bFlushingOpt && m_bFlushed && IsQueued()) { + m_bFlushing = FALSE; + m_hr = S_OK; + return; + } + } + + // sync with pushing thread -- done in BeginFlush + // ensure no more data to go downstream -- done in BeginFlush + // + // Because we are synching here there is no need to hold the critical + // section (in fact we'd deadlock if we did!) + + if (IsQueued()) { + m_evFlushComplete.Wait(); + } else { + FreeSamples(); + } + + // Be daring - the caller has guaranteed no samples will arrive + // before EndFlush() returns + + m_bFlushing = FALSE; + m_bFlushed = TRUE; + + // call EndFlush on downstream pins + + m_pPin->EndFlush(); + + m_hr = S_OK; +} + +// COutputQueue::QueueSample +// +// private method to Send a sample to the output queue +// The critical section MUST be held when this is called + +void COutputQueue::QueueSample(IMediaSample *pSample) +{ + if (NULL == m_List->AddTail(pSample)) { + if (!IsSpecialSample(pSample)) { + pSample->Release(); + } + } +} + +// +// COutputQueue::Receive() +// +// Send a single sample by the multiple sample route +// (NOTE - this could be optimized if necessary) +// +// On return the sample will have been Release()'d +// + +HRESULT COutputQueue::Receive(IMediaSample *pSample) +{ + LONG nProcessed; + return ReceiveMultiple(&pSample, 1, &nProcessed); +} + +// +// COutputQueue::ReceiveMultiple() +// +// Send a set of samples to the downstream pin +// +// ppSamples - array of samples +// nSamples - how many +// nSamplesProcessed - How many were processed +// +// On return all samples will have been Release()'d +// + +HRESULT COutputQueue::ReceiveMultiple ( + IMediaSample **ppSamples, + long nSamples, + long *nSamplesProcessed) +{ + CAutoLock lck(this); + // Either call directly or queue up the samples + + if (!IsQueued()) { + + // If we already had a bad return code then just return + + if (S_OK != m_hr) { + + // If we've never received anything since the last Flush() + // and the sticky return code is not S_OK we must be + // flushing + // ((!A || B) is equivalent to A implies B) + ASSERT(!m_bFlushed || m_bFlushing); + + // We're supposed to Release() them anyway! + *nSamplesProcessed = 0; + for (int i = 0; i < nSamples; i++) { + DbgLog((LOG_TRACE, 3, TEXT("COutputQueue (direct) : Discarding %d samples code 0x%8.8X"), + nSamples, m_hr)); + ppSamples[i]->Release(); + } + + return m_hr; + } + // + // If we're flushing the sticky return code should be S_FALSE + // + ASSERT(!m_bFlushing); + m_bFlushed = FALSE; + + ASSERT(m_nBatched < m_lBatchSize); + ASSERT(m_nBatched == 0 || m_bBatchExact); + + // Loop processing the samples in batches + + LONG iLost = 0; + long iDone; + for (iDone = 0; + iDone < nSamples || (m_nBatched != 0 && m_bSendAnyway); + ) { + +//pragma message (REMIND("Implement threshold scheme")) + ASSERT(m_nBatched < m_lBatchSize); + if (iDone < nSamples) { + m_ppSamples[m_nBatched++] = ppSamples[iDone++]; + } + if (m_nBatched == m_lBatchSize || + nSamples == 0 && (m_bSendAnyway || !m_bBatchExact)) { + LONG nDone; + DbgLog((LOG_TRACE, 4, TEXT("Batching %d samples"), + m_nBatched)); + + if (m_hr == S_OK) { + m_hr = m_pInputPin->ReceiveMultiple(m_ppSamples, + m_nBatched, + &nDone); + } else { + nDone = 0; + } + iLost += m_nBatched - nDone; + for (LONG i = 0; i < m_nBatched; i++) { + m_ppSamples[i]->Release(); + } + m_nBatched = 0; + } + } + *nSamplesProcessed = iDone - iLost; + if (*nSamplesProcessed < 0) { + *nSamplesProcessed = 0; + } + return m_hr; + } else { + /* We're sending to our thread */ + + if (m_hr != S_OK) { + *nSamplesProcessed = 0; + DbgLog((LOG_TRACE, 3, TEXT("COutputQueue (queued) : Discarding %d samples code 0x%8.8X"), + nSamples, m_hr)); + for (int i = 0; i < nSamples; i++) { + ppSamples[i]->Release(); + } + return m_hr; + } + m_bFlushed = FALSE; + for (long i = 0; i < nSamples; i++) { + QueueSample(ppSamples[i]); + } + *nSamplesProcessed = nSamples; + if (!m_bBatchExact || + m_nBatched + m_List->GetCount() >= m_lBatchSize) { + NotifyThread(); + } + return S_OK; + } +} + +// Get ready for new data - cancels sticky m_hr +void COutputQueue::Reset() +{ + if (!IsQueued()) { + m_hr = S_OK; + } else { + CAutoLock lck(this); + QueueSample(RESET_PACKET); + NotifyThread(); + m_evFlushComplete.Wait(); + } +} + +// Remove and Release() all queued and Batched samples +void COutputQueue::FreeSamples() +{ + CAutoLock lck(this); + if (IsQueued()) { + while (TRUE) { + IMediaSample *pSample = m_List->RemoveHead(); + // inform derived class we took something off the queue + if (m_hEventPop) { + //DbgLog((LOG_TRACE,3,TEXT("Queue: Delivered SET EVENT"))); + SetEvent(m_hEventPop); + } + + if (pSample == NULL) { + break; + } + if (!IsSpecialSample(pSample)) { + pSample->Release(); + } else { + if (pSample == NEW_SEGMENT) { + // Free NEW_SEGMENT packet + NewSegmentPacket *ppacket = + (NewSegmentPacket *) m_List->RemoveHead(); + // inform derived class we took something off the queue + if (m_hEventPop) { + //DbgLog((LOG_TRACE,3,TEXT("Queue: Delivered SET EVENT"))); + SetEvent(m_hEventPop); + } + + ASSERT(ppacket != NULL); + delete ppacket; + } + } + } + } + for (int i = 0; i < m_nBatched; i++) { + m_ppSamples[i]->Release(); + } + m_nBatched = 0; +} + +// Notify the thread if there is something to do +// +// The critical section MUST be held when this is called +void COutputQueue::NotifyThread() +{ + // Optimize - no need to signal if it's not waiting + ASSERT(IsQueued()); + if (m_lWaiting) { + ReleaseSemaphore(m_hSem, m_lWaiting, NULL); + m_lWaiting = 0; + } +} + +// See if there's any work to do +// Returns +// TRUE if there is nothing on the queue and nothing in the batch +// and all data has been sent +// FALSE otherwise +// +BOOL COutputQueue::IsIdle() +{ + CAutoLock lck(this); + + // We're idle if + // there is no thread (!IsQueued()) OR + // the thread is waiting for more work (m_lWaiting != 0) + // AND + // there's nothing in the current batch (m_nBatched == 0) + + if (IsQueued() && m_lWaiting == 0 || m_nBatched != 0) { + return FALSE; + } else { + + // If we're idle it shouldn't be possible for there + // to be anything on the work queue + + ASSERT(!IsQueued() || m_List->GetCount() == 0); + return TRUE; + } +} + + +void COutputQueue::SetPopEvent(HANDLE hEvent) +{ + m_hEventPop = hEvent; +} diff --git a/plugins/GSdx/baseclasses/outputq.h b/plugins/GSdx/baseclasses/outputq.h new file mode 100644 index 0000000000..d021922a53 --- /dev/null +++ b/plugins/GSdx/baseclasses/outputq.h @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// File: OutputQ.h +// +// Desc: DirectShow base classes - defines the COutputQueue class, which +// makes a queue of samples and sends them to an output pin. The +// class will optionally send the samples to the pin directly. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +typedef CGenericList CSampleList; + +class COutputQueue : public CCritSec +{ +public: + // Constructor + COutputQueue(IPin *pInputPin, // Pin to send stuff to + HRESULT *phr, // 'Return code' + BOOL bAuto = TRUE, // Ask pin if blocks + BOOL bQueue = TRUE, // Send through queue (ignored if + // bAuto set) + LONG lBatchSize = 1, // Batch + BOOL bBatchExact = FALSE,// Batch exactly to BatchSize + LONG lListSize = // Likely number in the list + DEFAULTCACHE, + DWORD dwPriority = // Priority of thread to create + THREAD_PRIORITY_NORMAL, + bool bFlushingOpt = false // flushing optimization + ); + ~COutputQueue(); + + // enter flush state - discard all data + void BeginFlush(); // Begin flushing samples + + // re-enable receives (pass this downstream) + void EndFlush(); // Complete flush of samples - downstream + // pin guaranteed not to block at this stage + + void EOS(); // Call this on End of stream + + void SendAnyway(); // Send batched samples anyway (if bBatchExact set) + + void NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate); + + HRESULT Receive(IMediaSample *pSample); + + // do something with these media samples + HRESULT ReceiveMultiple ( + IMediaSample **pSamples, + long nSamples, + long *nSamplesProcessed); + + void Reset(); // Reset m_hr ready for more data + + // See if its idle or not + BOOL IsIdle(); + + // give the class an event to fire after everything removed from the queue + void SetPopEvent(HANDLE hEvent); + +protected: + static DWORD WINAPI InitialThreadProc(LPVOID pv); + DWORD ThreadProc(); + BOOL IsQueued() + { + return m_List != NULL; + } + + // The critical section MUST be held when this is called + void QueueSample(IMediaSample *pSample); + + BOOL IsSpecialSample(IMediaSample *pSample) + { + return (DWORD_PTR)pSample > (DWORD_PTR)(LONG_PTR)(-16); + } + + // Remove and Release() batched and queued samples + void FreeSamples(); + + // Notify the thread there is something to do + void NotifyThread(); + + +protected: + // Queue 'messages' + #define SEND_PACKET ((IMediaSample *)(LONG_PTR)(-2)) // Send batch + #define EOS_PACKET ((IMediaSample *)(LONG_PTR)(-3)) // End of stream + #define RESET_PACKET ((IMediaSample *)(LONG_PTR)(-4)) // Reset m_hr + #define NEW_SEGMENT ((IMediaSample *)(LONG_PTR)(-5)) // send NewSegment + + // new segment packet is always followed by one of these + struct NewSegmentPacket { + REFERENCE_TIME tStart; + REFERENCE_TIME tStop; + double dRate; + }; + + // Remember input stuff + IPin * const m_pPin; + IMemInputPin * m_pInputPin; + BOOL const m_bBatchExact; + LONG const m_lBatchSize; + + CSampleList * m_List; + HANDLE m_hSem; + CAMEvent m_evFlushComplete; + HANDLE m_hThread; + IMediaSample ** m_ppSamples; + LONG m_nBatched; + + // Wait optimization + LONG m_lWaiting; + // Flush synchronization + BOOL m_bFlushing; + + // flushing optimization. some downstream filters have trouble + // with the queue's flushing optimization. other rely on it + BOOL m_bFlushed; + bool m_bFlushingOpt; + + // Terminate now + BOOL m_bTerminate; + + // Send anyway flag for batching + BOOL m_bSendAnyway; + + // Deferred 'return code' + BOOL volatile m_hr; + + // an event that can be fired after every deliver + HANDLE m_hEventPop; +}; + diff --git a/plugins/GSdx/baseclasses/pstream.cpp b/plugins/GSdx/baseclasses/pstream.cpp new file mode 100644 index 0000000000..af85fee9da --- /dev/null +++ b/plugins/GSdx/baseclasses/pstream.cpp @@ -0,0 +1,196 @@ +//------------------------------------------------------------------------------ +// File: PStream.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" + +#ifdef PERF +#include +#endif + + +// +// Constructor +// +CPersistStream::CPersistStream(IUnknown *punk, HRESULT *phr) + : mPS_fDirty(FALSE) +{ + mPS_dwFileVersion = GetSoftwareVersion(); +} + + +// +// Destructor +// +CPersistStream::~CPersistStream() { + // Nothing to do +} + +#if 0 +SAMPLE CODE TO COPY - not active at the moment + +// +// NonDelegatingQueryInterface +// +// This object supports IPersist & IPersistStream +STDMETHODIMP CPersistStream::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + if (riid == IID_IPersist) { + return GetInterface((IPersist *) this, ppv); + } + else if (riid == IID_IPersistStream) { + return GetInterface((IPersistStream *) this, ppv); + } + else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } +} +#endif + + +// +// WriteToStream +// +// Writes to the stream (default action is to write nothing) +HRESULT CPersistStream::WriteToStream(IStream *pStream) +{ + // You can override this to do things like + // hr = pStream->Write(MyStructure, sizeof(MyStructure), NULL); + + return NOERROR; +} + + + +HRESULT CPersistStream::ReadFromStream(IStream * pStream) +{ + // You can override this to do things like + // hr = pStream->Read(MyStructure, sizeof(MyStructure), NULL); + + return NOERROR; +} + + +// +// Load +// +// Load all the data from the given stream +STDMETHODIMP CPersistStream::Load(LPSTREAM pStm) +{ + HRESULT hr; + // Load the version number then the data + mPS_dwFileVersion = ReadInt(pStm, hr); + if (FAILED(hr)) { + return hr; + } + + return ReadFromStream(pStm); +} // Load + + + +// +// Save +// +// Save the contents of this Stream. +STDMETHODIMP CPersistStream::Save(LPSTREAM pStm, BOOL fClearDirty) +{ + + HRESULT hr = WriteInt(pStm, GetSoftwareVersion()); + if (FAILED(hr)) { + return hr; + } + + hr = WriteToStream(pStm); + if (FAILED(hr)) { + return hr; + } + + mPS_fDirty = !fClearDirty; + + return hr; +} // Save + + +// WriteInt +// +// Writes an integer to an IStream as 11 UNICODE characters followed by one space. +// You could use this for shorts or unsigneds or anything (up to 32 bits) +// where the value isn't actually truncated by squeezing it into 32 bits. +// Values such as (unsigned) 0x80000000 would come out as -2147483648 +// but would then load as 0x80000000 through ReadInt. Cast as you please. + +STDAPI WriteInt(IStream *pIStream, int n) +{ + WCHAR Buff[13]; // Allows for trailing null that we don't write + (void)StringCchPrintfW(Buff, NUMELMS(Buff), L"%011d ",n); + return pIStream->Write(&(Buff[0]), 12*sizeof(WCHAR), NULL); +} // WriteInt + + +// ReadInt +// +// Reads an integer from an IStream. +// Read as 4 bytes. You could use this for shorts or unsigneds or anything +// where the value isn't actually truncated by squeezing it into 32 bits +// Striped down subset of what sscanf can do (without dragging in the C runtime) + +STDAPI_(int) ReadInt(IStream *pIStream, HRESULT &hr) +{ + + int Sign = 1; + unsigned int n = 0; // result wil be n*Sign + WCHAR wch; + + hr = pIStream->Read( &wch, sizeof(wch), NULL); + if (FAILED(hr)) { + return 0; + } + + if (wch==L'-'){ + Sign = -1; + hr = pIStream->Read( &wch, sizeof(wch), NULL); + if (FAILED(hr)) { + return 0; + } + } + + for( ; ; ) { + if (wch>=L'0' && wch<=L'9') { + n = 10*n+(int)(wch-L'0'); + } else if ( wch == L' ' + || wch == L'\t' + || wch == L'\r' + || wch == L'\n' + || wch == L'\0' + ) { + break; + } else { + hr = VFW_E_INVALID_FILE_FORMAT; + return 0; + } + + hr = pIStream->Read( &wch, sizeof(wch), NULL); + if (FAILED(hr)) { + return 0; + } + } + + if (n==0x80000000 && Sign==-1) { + // This is the negative number that has no positive version! + return (int)n; + } + else return (int)n * Sign; +} // ReadInt + + +// The microsoft C/C++ compile generates level 4 warnings to the effect that +// a particular inline function (from some base class) was not needed. +// This line gets rid of hundreds of such unwanted messages and makes +// -W4 compilation feasible: +#pragma warning(disable: 4514) diff --git a/plugins/GSdx/baseclasses/pstream.h b/plugins/GSdx/baseclasses/pstream.h new file mode 100644 index 0000000000..95ec88cdd5 --- /dev/null +++ b/plugins/GSdx/baseclasses/pstream.h @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// File: PStream.h +// +// Desc: DirectShow base classes - defines a class for persistent properties +// of filters. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __PSTREAM__ +#define __PSTREAM__ + +// Base class for persistent properties of filters +// (i.e. filter properties in saved graphs) + +// The simplest way to use this is: +// 1. Arrange for your filter to inherit this class +// 2. Implement in your class WriteToStream and ReadFromStream +// These will override the "do nothing" functions here. +// 3. Change your NonDelegatingQueryInterface to handle IPersistStream +// 4. Implement SizeMax to return the number of bytes of data you save. +// If you save UNICODE data, don't forget a char is 2 bytes. +// 5. Whenever your data changes, call SetDirty() +// +// At some point you may decide to alter, or extend the format of your data. +// At that point you will wish that you had a version number in all the old +// saved graphs, so that you can tell, when you read them, whether they +// represent the old or new form. To assist you in this, this class +// writes and reads a version number. +// When it writes, it calls GetSoftwareVersion() to enquire what version +// of the software we have at the moment. (In effect this is a version number +// of the data layout in the file). It writes this as the first thing in the data. +// If you want to change the version, implement (override) GetSoftwareVersion(). +// It reads this from the file into mPS_dwFileVersion before calling ReadFromStream, +// so in ReadFromStream you can check mPS_dwFileVersion to see if you are reading +// an old version file. +// Normally you should accept files whose version is no newer than the software +// version that's reading them. + + +// CPersistStream +// +// Implements IPersistStream. +// See 'OLE Programmers Reference (Vol 1):Structured Storage Overview' for +// more implementation information. +class CPersistStream : public IPersistStream { + private: + + // Internal state: + + protected: + DWORD mPS_dwFileVersion; // version number of file (being read) + BOOL mPS_fDirty; + + public: + + // IPersistStream methods + + STDMETHODIMP IsDirty() + {return (mPS_fDirty ? S_OK : S_FALSE);} // note FALSE means clean + STDMETHODIMP Load(LPSTREAM pStm); + STDMETHODIMP Save(LPSTREAM pStm, BOOL fClearDirty); + STDMETHODIMP GetSizeMax(ULARGE_INTEGER * pcbSize) + // Allow 24 bytes for version. + { pcbSize->QuadPart = 12*sizeof(WCHAR)+SizeMax(); return NOERROR; } + + // implementation + + CPersistStream(IUnknown *punk, HRESULT *phr); + ~CPersistStream(); + + HRESULT SetDirty(BOOL fDirty) + { mPS_fDirty = fDirty; return NOERROR;} + + + // override to reveal IPersist & IPersistStream + // STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // --- IPersist --- + + // You must override this to provide your own class id + STDMETHODIMP GetClassID(CLSID *pClsid) PURE; + + // overrideable if you want + // file version number. Override it if you ever change format + virtual DWORD GetSoftwareVersion(void) { return 0; } + + + //========================================================================= + // OVERRIDE THESE to read and write your data + // OVERRIDE THESE to read and write your data + // OVERRIDE THESE to read and write your data + + virtual int SizeMax() {return 0;} + virtual HRESULT WriteToStream(IStream *pStream); + virtual HRESULT ReadFromStream(IStream *pStream); + //========================================================================= + + private: + +}; + + +// --- Useful helpers --- + + +// Writes an int to an IStream as UNICODE. +STDAPI WriteInt(IStream *pIStream, int n); + +// inverse of WriteInt +STDAPI_(int) ReadInt(IStream *pIStream, HRESULT &hr); + +#endif // __PSTREAM__ diff --git a/plugins/GSdx/baseclasses/pullpin.cpp b/plugins/GSdx/baseclasses/pullpin.cpp new file mode 100644 index 0000000000..da347f570a --- /dev/null +++ b/plugins/GSdx/baseclasses/pullpin.cpp @@ -0,0 +1,527 @@ +//------------------------------------------------------------------------------ +// File: PullPin.cpp +// +// Desc: DirectShow base classes - implements CPullPin class that pulls data +// from IAsyncReader. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include "pullpin.h" + + + +CPullPin::CPullPin() + : m_pReader(NULL), + m_pAlloc(NULL), + m_State(TM_Exit) +{ +} + +CPullPin::~CPullPin() +{ + Disconnect(); +} + +// returns S_OK if successfully connected to an IAsyncReader interface +// from this object +// Optional allocator should be proposed as a preferred allocator if +// necessary +HRESULT +CPullPin::Connect(IUnknown* pUnk, IMemAllocator* pAlloc, BOOL bSync) +{ + CAutoLock lock(&m_AccessLock); + + if (m_pReader) { + return VFW_E_ALREADY_CONNECTED; + } + + HRESULT hr = pUnk->QueryInterface(IID_IAsyncReader, (void**)&m_pReader); + if (FAILED(hr)) { + return(hr); + } + + hr = DecideAllocator(pAlloc, NULL); + if (FAILED(hr)) { + Disconnect(); + return hr; + } + + LONGLONG llTotal, llAvail; + hr = m_pReader->Length(&llTotal, &llAvail); + if (FAILED(hr)) { + Disconnect(); + return hr; + } + + // convert from file position to reference time + m_tDuration = llTotal * UNITS; + m_tStop = m_tDuration; + m_tStart = 0; + + m_bSync = bSync; + + return S_OK; +} + +// disconnect any connection made in Connect +HRESULT +CPullPin::Disconnect() +{ + CAutoLock lock(&m_AccessLock); + + StopThread(); + + if (m_pReader) { + m_pReader->Release(); + m_pReader = NULL; + } + + if (m_pAlloc) { + m_pAlloc->Release(); + m_pAlloc = NULL; + } + + return S_OK; +} + +// agree an allocator using RequestAllocator - optional +// props param specifies your requirements (non-zero fields). +// returns an error code if fail to match requirements. +// optional IMemAllocator interface is offered as a preferred allocator +// but no error occurs if it can't be met. +HRESULT +CPullPin::DecideAllocator( + IMemAllocator * pAlloc, + ALLOCATOR_PROPERTIES * pProps) +{ + ALLOCATOR_PROPERTIES *pRequest; + ALLOCATOR_PROPERTIES Request; + if (pProps == NULL) { + Request.cBuffers = 3; + Request.cbBuffer = 64*1024; + Request.cbAlign = 0; + Request.cbPrefix = 0; + pRequest = &Request; + } else { + pRequest = pProps; + } + HRESULT hr = m_pReader->RequestAllocator( + pAlloc, + pRequest, + &m_pAlloc); + return hr; +} + +// start pulling data +HRESULT +CPullPin::Active(void) +{ + ASSERT(!ThreadExists()); + return StartThread(); +} + +// stop pulling data +HRESULT +CPullPin::Inactive(void) +{ + StopThread(); + + return S_OK; +} + +HRESULT +CPullPin::Seek(REFERENCE_TIME tStart, REFERENCE_TIME tStop) +{ + CAutoLock lock(&m_AccessLock); + + ThreadMsg AtStart = m_State; + + if (AtStart == TM_Start) { + BeginFlush(); + PauseThread(); + EndFlush(); + } + + m_tStart = tStart; + m_tStop = tStop; + + HRESULT hr = S_OK; + if (AtStart == TM_Start) { + hr = StartThread(); + } + + return hr; +} + +HRESULT +CPullPin::Duration(REFERENCE_TIME* ptDuration) +{ + *ptDuration = m_tDuration; + return S_OK; +} + + +HRESULT +CPullPin::StartThread() +{ + CAutoLock lock(&m_AccessLock); + + if (!m_pAlloc || !m_pReader) { + return E_UNEXPECTED; + } + + HRESULT hr; + if (!ThreadExists()) { + + // commit allocator + hr = m_pAlloc->Commit(); + if (FAILED(hr)) { + return hr; + } + + // start thread + if (!Create()) { + return E_FAIL; + } + } + + m_State = TM_Start; + hr = (HRESULT) CallWorker(m_State); + return hr; +} + +HRESULT +CPullPin::PauseThread() +{ + CAutoLock lock(&m_AccessLock); + + if (!ThreadExists()) { + return E_UNEXPECTED; + } + + // need to flush to ensure the thread is not blocked + // in WaitForNext + HRESULT hr = m_pReader->BeginFlush(); + if (FAILED(hr)) { + return hr; + } + + m_State = TM_Pause; + hr = CallWorker(TM_Pause); + + m_pReader->EndFlush(); + return hr; +} + +HRESULT +CPullPin::StopThread() +{ + CAutoLock lock(&m_AccessLock); + + if (!ThreadExists()) { + return S_FALSE; + } + + // need to flush to ensure the thread is not blocked + // in WaitForNext + HRESULT hr = m_pReader->BeginFlush(); + if (FAILED(hr)) { + return hr; + } + + m_State = TM_Exit; + hr = CallWorker(TM_Exit); + + m_pReader->EndFlush(); + + // wait for thread to completely exit + Close(); + + // decommit allocator + if (m_pAlloc) { + m_pAlloc->Decommit(); + } + + return S_OK; +} + + +DWORD +CPullPin::ThreadProc(void) +{ + while(1) { + DWORD cmd = GetRequest(); + switch(cmd) { + case TM_Exit: + Reply(S_OK); + return 0; + + case TM_Pause: + // we are paused already + Reply(S_OK); + break; + + case TM_Start: + Reply(S_OK); + Process(); + break; + } + + // at this point, there should be no outstanding requests on the + // upstream filter. + // We should force begin/endflush to ensure that this is true. + // !!!Note that we may currently be inside a BeginFlush/EndFlush pair + // on another thread, but the premature EndFlush will do no harm now + // that we are idle. + m_pReader->BeginFlush(); + CleanupCancelled(); + m_pReader->EndFlush(); + } +} + +HRESULT +CPullPin::QueueSample( + REFERENCE_TIME& tCurrent, + REFERENCE_TIME tAlignStop, + BOOL bDiscontinuity + ) +{ + IMediaSample* pSample; + + HRESULT hr = m_pAlloc->GetBuffer(&pSample, NULL, NULL, 0); + if (FAILED(hr)) { + return hr; + } + + LONGLONG tStopThis = tCurrent + (pSample->GetSize() * UNITS); + if (tStopThis > tAlignStop) { + tStopThis = tAlignStop; + } + pSample->SetTime(&tCurrent, &tStopThis); + tCurrent = tStopThis; + + pSample->SetDiscontinuity(bDiscontinuity); + + hr = m_pReader->Request( + pSample, + 0); + if (FAILED(hr)) { + pSample->Release(); + + CleanupCancelled(); + OnError(hr); + } + return hr; +} + +HRESULT +CPullPin::CollectAndDeliver( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop) +{ + IMediaSample* pSample = NULL; // better be sure pSample is set + DWORD_PTR dwUnused; + HRESULT hr = m_pReader->WaitForNext( + INFINITE, + &pSample, + &dwUnused); + if (FAILED(hr)) { + if (pSample) { + pSample->Release(); + } + } else { + hr = DeliverSample(pSample, tStart, tStop); + } + if (FAILED(hr)) { + CleanupCancelled(); + OnError(hr); + } + return hr; + +} + +HRESULT +CPullPin::DeliverSample( + IMediaSample* pSample, + REFERENCE_TIME tStart, + REFERENCE_TIME tStop + ) +{ + // fix up sample if past actual stop (for sector alignment) + REFERENCE_TIME t1, t2; + pSample->GetTime(&t1, &t2); + if (t2 > tStop) { + t2 = tStop; + } + + // adjust times to be relative to (aligned) start time + t1 -= tStart; + t2 -= tStart; + pSample->SetTime(&t1, &t2); + + + HRESULT hr = Receive(pSample); + pSample->Release(); + return hr; +} + +void +CPullPin::Process(void) +{ + // is there anything to do? + if (m_tStop <= m_tStart) { + EndOfStream(); + return; + } + + BOOL bDiscontinuity = TRUE; + + // if there is more than one sample at the allocator, + // then try to queue 2 at once in order to overlap. + // -- get buffer count and required alignment + ALLOCATOR_PROPERTIES Actual; + HRESULT hr = m_pAlloc->GetProperties(&Actual); + + // align the start position downwards + REFERENCE_TIME tStart = AlignDown(m_tStart / UNITS, Actual.cbAlign) * UNITS; + REFERENCE_TIME tCurrent = tStart; + + REFERENCE_TIME tStop = m_tStop; + if (tStop > m_tDuration) { + tStop = m_tDuration; + } + + // align the stop position - may be past stop, but that + // doesn't matter + REFERENCE_TIME tAlignStop = AlignUp(tStop / UNITS, Actual.cbAlign) * UNITS; + + + DWORD dwRequest; + + if (!m_bSync) { + + // Break out of the loop either if we get to the end or we're asked + // to do something else + while (tCurrent < tAlignStop) { + + // Break out without calling EndOfStream if we're asked to + // do something different + if (CheckRequest(&dwRequest)) { + return; + } + + // queue a first sample + if (Actual.cBuffers > 1) { + + hr = QueueSample(tCurrent, tAlignStop, TRUE); + bDiscontinuity = FALSE; + + if (FAILED(hr)) { + return; + } + } + + + + // loop queueing second and waiting for first.. + while (tCurrent < tAlignStop) { + + hr = QueueSample(tCurrent, tAlignStop, bDiscontinuity); + bDiscontinuity = FALSE; + + if (FAILED(hr)) { + return; + } + + hr = CollectAndDeliver(tStart, tStop); + if (S_OK != hr) { + + // stop if error, or if downstream filter said + // to stop. + return; + } + } + + if (Actual.cBuffers > 1) { + hr = CollectAndDeliver(tStart, tStop); + if (FAILED(hr)) { + return; + } + } + } + } else { + + // sync version of above loop + while (tCurrent < tAlignStop) { + + // Break out without calling EndOfStream if we're asked to + // do something different + if (CheckRequest(&dwRequest)) { + return; + } + + IMediaSample* pSample; + + hr = m_pAlloc->GetBuffer(&pSample, NULL, NULL, 0); + if (FAILED(hr)) { + OnError(hr); + return; + } + + LONGLONG tStopThis = tCurrent + (pSample->GetSize() * UNITS); + if (tStopThis > tAlignStop) { + tStopThis = tAlignStop; + } + pSample->SetTime(&tCurrent, &tStopThis); + tCurrent = tStopThis; + + if (bDiscontinuity) { + pSample->SetDiscontinuity(TRUE); + bDiscontinuity = FALSE; + } + + hr = m_pReader->SyncReadAligned(pSample); + + if (FAILED(hr)) { + pSample->Release(); + OnError(hr); + return; + } + + hr = DeliverSample(pSample, tStart, tStop); + if (hr != S_OK) { + if (FAILED(hr)) { + OnError(hr); + } + return; + } + } + } + + EndOfStream(); +} + +// after a flush, cancelled i/o will be waiting for collection +// and release +void +CPullPin::CleanupCancelled(void) +{ + while (1) { + IMediaSample * pSample; + DWORD_PTR dwUnused; + + HRESULT hr = m_pReader->WaitForNext( + 0, // no wait + &pSample, + &dwUnused); + if(pSample) { + pSample->Release(); + } else { + // no more samples + return; + } + } +} diff --git a/plugins/GSdx/baseclasses/pullpin.h b/plugins/GSdx/baseclasses/pullpin.h new file mode 100644 index 0000000000..254ffe248a --- /dev/null +++ b/plugins/GSdx/baseclasses/pullpin.h @@ -0,0 +1,152 @@ +//------------------------------------------------------------------------------ +// File: PullPin.h +// +// Desc: DirectShow base classes - defines CPullPin class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __PULLPIN_H__ +#define __PULLPIN_H__ + +// +// CPullPin +// +// object supporting pulling data from an IAsyncReader interface. +// Given a start/stop position, calls a pure Receive method with each +// IMediaSample received. +// +// This is essentially for use in a MemInputPin when it finds itself +// connected to an IAsyncReader pin instead of a pushing pin. +// + +class CPullPin : public CAMThread +{ + IAsyncReader* m_pReader; + REFERENCE_TIME m_tStart; + REFERENCE_TIME m_tStop; + REFERENCE_TIME m_tDuration; + BOOL m_bSync; + + enum ThreadMsg { + TM_Pause, // stop pulling and wait for next message + TM_Start, // start pulling + TM_Exit, // stop and exit + }; + + ThreadMsg m_State; + + // override pure thread proc from CAMThread + DWORD ThreadProc(void); + + // running pull method (check m_bSync) + void Process(void); + + // clean up any cancelled i/o after a flush + void CleanupCancelled(void); + + // suspend thread from pulling, eg during seek + HRESULT PauseThread(); + + // start thread pulling - create thread if necy + HRESULT StartThread(); + + // stop and close thread + HRESULT StopThread(); + + // called from ProcessAsync to queue and collect requests + HRESULT QueueSample( + REFERENCE_TIME& tCurrent, + REFERENCE_TIME tAlignStop, + BOOL bDiscontinuity); + + HRESULT CollectAndDeliver( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop); + + HRESULT DeliverSample( + IMediaSample* pSample, + REFERENCE_TIME tStart, + REFERENCE_TIME tStop); + +protected: + IMemAllocator * m_pAlloc; + +public: + CPullPin(); + virtual ~CPullPin(); + + // returns S_OK if successfully connected to an IAsyncReader interface + // from this object + // Optional allocator should be proposed as a preferred allocator if + // necessary + // bSync is TRUE if we are to use sync reads instead of the + // async methods. + HRESULT Connect(IUnknown* pUnk, IMemAllocator* pAlloc, BOOL bSync); + + // disconnect any connection made in Connect + HRESULT Disconnect(); + + // agree an allocator using RequestAllocator - optional + // props param specifies your requirements (non-zero fields). + // returns an error code if fail to match requirements. + // optional IMemAllocator interface is offered as a preferred allocator + // but no error occurs if it can't be met. + virtual HRESULT DecideAllocator( + IMemAllocator* pAlloc, + ALLOCATOR_PROPERTIES * pProps); + + // set start and stop position. if active, will start immediately at + // the new position. Default is 0 to duration + HRESULT Seek(REFERENCE_TIME tStart, REFERENCE_TIME tStop); + + // return the total duration + HRESULT Duration(REFERENCE_TIME* ptDuration); + + // start pulling data + HRESULT Active(void); + + // stop pulling data + HRESULT Inactive(void); + + // helper functions + LONGLONG AlignDown(LONGLONG ll, LONG lAlign) { + // aligning downwards is just truncation + return ll & ~(lAlign-1); + }; + + LONGLONG AlignUp(LONGLONG ll, LONG lAlign) { + // align up: round up to next boundary + return (ll + (lAlign -1)) & ~(lAlign -1); + }; + + // GetReader returns the (addrefed) IAsyncReader interface + // for SyncRead etc + IAsyncReader* GetReader() { + m_pReader->AddRef(); + return m_pReader; + }; + + // -- pure -- + + // override this to handle data arrival + // return value other than S_OK will stop data + virtual HRESULT Receive(IMediaSample*) PURE; + + // override this to handle end-of-stream + virtual HRESULT EndOfStream(void) PURE; + + // called on runtime errors that will have caused pulling + // to stop + // these errors are all returned from the upstream filter, who + // will have already reported any errors to the filtergraph. + virtual void OnError(HRESULT hr) PURE; + + // flush this pin and all downstream + virtual HRESULT BeginFlush() PURE; + virtual HRESULT EndFlush() PURE; + +}; + +#endif //__PULLPIN_H__ diff --git a/plugins/GSdx/baseclasses/refclock.cpp b/plugins/GSdx/baseclasses/refclock.cpp new file mode 100644 index 0000000000..bd8cc13577 --- /dev/null +++ b/plugins/GSdx/baseclasses/refclock.cpp @@ -0,0 +1,340 @@ +//------------------------------------------------------------------------------ +// File: RefClock.cpp +// +// Desc: DirectShow base classes - implements the IReferenceClock interface. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include + + + +// 'this' used in constructor list +#pragma warning(disable:4355) + + +STDMETHODIMP CBaseReferenceClock::NonDelegatingQueryInterface( + REFIID riid, + void ** ppv) +{ + HRESULT hr; + + if (riid == IID_IReferenceClock) + { + hr = GetInterface((IReferenceClock *) this, ppv); + } + else + { + hr = CUnknown::NonDelegatingQueryInterface(riid, ppv); + } + return hr; +} + +CBaseReferenceClock::~CBaseReferenceClock() +{ + + if (m_TimerResolution) timeEndPeriod(m_TimerResolution); + + m_pSchedule->DumpLinkedList(); + + if (m_hThread) + { + m_bAbort = TRUE; + TriggerThread(); + WaitForSingleObject( m_hThread, INFINITE ); + EXECUTE_ASSERT( CloseHandle(m_hThread) ); + m_hThread = 0; + EXECUTE_ASSERT( CloseHandle(m_pSchedule->GetEvent()) ); + delete m_pSchedule; + } +} + +// A derived class may supply a hThreadEvent if it has its own thread that will take care +// of calling the schedulers Advise method. (Refere to CBaseReferenceClock::AdviseThread() +// to see what such a thread has to do.) +CBaseReferenceClock::CBaseReferenceClock( TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr, CAMSchedule * pShed ) +: CUnknown( pName, pUnk ) +, m_rtLastGotTime(0) +, m_TimerResolution(0) +, m_bAbort( FALSE ) +, m_pSchedule( pShed ? pShed : new CAMSchedule(CreateEvent(NULL, FALSE, FALSE, NULL)) ) +, m_hThread(0) +{ + + + ASSERT(m_pSchedule); + if (!m_pSchedule) + { + *phr = E_OUTOFMEMORY; + } + else + { + // Set up the highest resolution timer we can manage + TIMECAPS tc; + m_TimerResolution = (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(tc))) + ? tc.wPeriodMin + : 1; + + timeBeginPeriod(m_TimerResolution); + + /* Initialise our system times - the derived clock should set the right values */ + m_dwPrevSystemTime = timeGetTime(); + m_rtPrivateTime = (UNITS / MILLISECONDS) * m_dwPrevSystemTime; + + #ifdef PERF + m_idGetSystemTime = MSR_REGISTER(TEXT("CBaseReferenceClock::GetTime")); + #endif + + if ( !pShed ) + { + DWORD ThreadID; + m_hThread = ::CreateThread(NULL, // Security attributes + (DWORD) 0, // Initial stack size + AdviseThreadFunction, // Thread start address + (LPVOID) this, // Thread parameter + (DWORD) 0, // Creation flags + &ThreadID); // Thread identifier + + if (m_hThread) + { + SetThreadPriority( m_hThread, THREAD_PRIORITY_TIME_CRITICAL ); + } + else + { + *phr = E_FAIL; + EXECUTE_ASSERT( CloseHandle(m_pSchedule->GetEvent()) ); + delete m_pSchedule; + } + } + } +} + +void CBaseReferenceClock::Restart (IN REFERENCE_TIME rtMinTime) +{ + Lock(); + m_rtLastGotTime = rtMinTime ; + Unlock(); +} + +STDMETHODIMP CBaseReferenceClock::GetTime(REFERENCE_TIME *pTime) +{ + HRESULT hr; + if (pTime) + { + REFERENCE_TIME rtNow; + Lock(); + rtNow = GetPrivateTime(); + if (rtNow > m_rtLastGotTime) + { + m_rtLastGotTime = rtNow; + hr = S_OK; + } + else + { + hr = S_FALSE; + } + *pTime = m_rtLastGotTime; + Unlock(); + MSR_INTEGER(m_idGetSystemTime, LONG((*pTime) / (UNITS/MILLISECONDS)) ); + } + else hr = E_POINTER; + + return hr; +} + +/* Ask for an async notification that a time has elapsed */ + +STDMETHODIMP CBaseReferenceClock::AdviseTime( + REFERENCE_TIME baseTime, // base reference time + REFERENCE_TIME streamTime, // stream offset time + HEVENT hEvent, // advise via this event + DWORD_PTR *pdwAdviseCookie) // where your cookie goes +{ + CheckPointer(pdwAdviseCookie, E_POINTER); + *pdwAdviseCookie = 0; + + // Check that the event is not already set + ASSERT(WAIT_TIMEOUT == WaitForSingleObject(HANDLE(hEvent),0)); + + HRESULT hr; + + const REFERENCE_TIME lRefTime = baseTime + streamTime; + if ( lRefTime <= 0 || lRefTime == MAX_TIME ) + { + hr = E_INVALIDARG; + } + else + { + *pdwAdviseCookie = m_pSchedule->AddAdvisePacket( lRefTime, 0, HANDLE(hEvent), FALSE ); + hr = *pdwAdviseCookie ? NOERROR : E_OUTOFMEMORY; + } + return hr; +} + + +/* Ask for an asynchronous periodic notification that a time has elapsed */ + +STDMETHODIMP CBaseReferenceClock::AdvisePeriodic( + REFERENCE_TIME StartTime, // starting at this time + REFERENCE_TIME PeriodTime, // time between notifications + HSEMAPHORE hSemaphore, // advise via a semaphore + DWORD_PTR *pdwAdviseCookie) // where your cookie goes +{ + CheckPointer(pdwAdviseCookie, E_POINTER); + *pdwAdviseCookie = 0; + + HRESULT hr; + if (StartTime > 0 && PeriodTime > 0 && StartTime != MAX_TIME ) + { + *pdwAdviseCookie = m_pSchedule->AddAdvisePacket( StartTime, PeriodTime, HANDLE(hSemaphore), TRUE ); + hr = *pdwAdviseCookie ? NOERROR : E_OUTOFMEMORY; + } + else hr = E_INVALIDARG; + + return hr; +} + + +STDMETHODIMP CBaseReferenceClock::Unadvise(DWORD_PTR dwAdviseCookie) +{ + return m_pSchedule->Unadvise(dwAdviseCookie); +} + + +REFERENCE_TIME CBaseReferenceClock::GetPrivateTime() +{ + CAutoLock cObjectLock(this); + + + /* If the clock has wrapped then the current time will be less than + * the last time we were notified so add on the extra milliseconds + * + * The time period is long enough so that the likelihood of + * successive calls spanning the clock cycle is not considered. + */ + + DWORD dwTime = timeGetTime(); + { + m_rtPrivateTime += Int32x32To64(UNITS / MILLISECONDS, (DWORD)(dwTime - m_dwPrevSystemTime)); + m_dwPrevSystemTime = dwTime; + } + + return m_rtPrivateTime; +} + + +/* Adjust the current time by the input value. This allows an + external time source to work out some of the latency of the clock + system and adjust the "current" time accordingly. The intent is + that the time returned to the user is synchronised to a clock + source and allows drift to be catered for. + + For example: if the clock source detects a drift it can pass a delta + to the current time rather than having to set an explicit time. +*/ + +STDMETHODIMP CBaseReferenceClock::SetTimeDelta(const REFERENCE_TIME & TimeDelta) +{ +#ifdef DEBUG + + // Just break if passed an improper time delta value + LONGLONG llDelta = TimeDelta > 0 ? TimeDelta : -TimeDelta; + if (llDelta > UNITS * 1000) { + DbgLog((LOG_TRACE, 0, TEXT("Bad Time Delta"))); + //DebugBreak(); + } + + // We're going to calculate a "severity" for the time change. Max -1 + // min 8. We'll then use this as the debug logging level for a + // debug log message. + const LONG usDelta = LONG(TimeDelta/10); // Delta in micro-secs + + DWORD delta = abs(usDelta); // varying delta + // Severity == 8 - ceil(log(abs( micro-secs delta))) + int Severity = 8; + while ( delta > 0 ) + { + delta >>= 3; // div 8 + Severity--; + } + + // Sev == 0 => > 2 second delta! + DbgLog((LOG_TIMING, Severity < 0 ? 0 : Severity, + TEXT("Sev %2i: CSystemClock::SetTimeDelta(%8ld us) %lu -> %lu ms."), + Severity, usDelta, DWORD(ConvertToMilliseconds(m_rtPrivateTime)), + DWORD(ConvertToMilliseconds(TimeDelta+m_rtPrivateTime)) )); + + // Don't want the DbgBreak to fire when running stress on debug-builds. + #ifdef BREAK_ON_SEVERE_TIME_DELTA + if (Severity < 0) + DbgBreakPoint(TEXT("SetTimeDelta > 16 seconds!"), + TEXT(__FILE__),__LINE__); + #endif + +#endif + + CAutoLock cObjectLock(this); + m_rtPrivateTime += TimeDelta; + // If time goes forwards, and we have advises, then we need to + // trigger the thread so that it can re-evaluate its wait time. + // Since we don't want the cost of the thread switches if the change + // is really small, only do it if clock goes forward by more than + // 0.5 millisecond. If the time goes backwards, the thread will + // wake up "early" (relativly speaking) and will re-evaluate at + // that time. + if ( TimeDelta > 5000 && m_pSchedule->GetAdviseCount() > 0 ) TriggerThread(); + return NOERROR; +} + +// Thread stuff + +DWORD __stdcall CBaseReferenceClock::AdviseThreadFunction(LPVOID p) +{ + return DWORD(reinterpret_cast(p)->AdviseThread()); +} + +HRESULT CBaseReferenceClock::AdviseThread() +{ + DWORD dwWait = INFINITE; + + // The first thing we do is wait until something interesting happens + // (meaning a first advise or shutdown). This prevents us calling + // GetPrivateTime immediately which is goodness as that is a virtual + // routine and the derived class may not yet be constructed. (This + // thread is created in the base class constructor.) + + while ( !m_bAbort ) + { + // Wait for an interesting event to happen + DbgLog((LOG_TIMING, 3, TEXT("CBaseRefClock::AdviseThread() Delay: %lu ms"), dwWait )); + WaitForSingleObject(m_pSchedule->GetEvent(), dwWait); + if (m_bAbort) break; + + // There are several reasons why we need to work from the internal + // time, mainly to do with what happens when time goes backwards. + // Mainly, it stop us looping madly if an event is just about to + // expire when the clock goes backward (i.e. GetTime stop for a + // while). + const REFERENCE_TIME rtNow = GetPrivateTime(); + + DbgLog((LOG_TIMING, 3, + TEXT("CBaseRefClock::AdviseThread() Woke at = %lu ms"), + ConvertToMilliseconds(rtNow) )); + + // We must add in a millisecond, since this is the resolution of our + // WaitForSingleObject timer. Failure to do so will cause us to loop + // franticly for (approx) 1 a millisecond. + m_rtNextAdvise = m_pSchedule->Advise( 10000 + rtNow ); + LONGLONG llWait = m_rtNextAdvise - rtNow; + + ASSERT( llWait > 0 ); + + llWait = ConvertToMilliseconds(llWait); + // DON'T replace this with a max!! (The type's of these things is VERY important) + dwWait = (llWait > REFERENCE_TIME(UINT_MAX)) ? UINT_MAX : DWORD(llWait); + }; + return NOERROR; +} diff --git a/plugins/GSdx/baseclasses/refclock.h b/plugins/GSdx/baseclasses/refclock.h new file mode 100644 index 0000000000..a864f72b14 --- /dev/null +++ b/plugins/GSdx/baseclasses/refclock.h @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// File: RefClock.h +// +// Desc: DirectShow base classes - defines the IReferenceClock interface. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __BASEREFCLOCK__ +#define __BASEREFCLOCK__ + +#include "dsschedule.h" + +const UINT RESOLUTION = 1; /* High resolution timer */ +const INT ADVISE_CACHE = 4; /* Default cache size */ +const LONGLONG MAX_TIME = 0x7FFFFFFFFFFFFFFF; /* Maximum LONGLONG value */ + +inline LONGLONG WINAPI ConvertToMilliseconds(const REFERENCE_TIME& RT) +{ + /* This converts an arbitrary value representing a reference time + into a MILLISECONDS value for use in subsequent system calls */ + + return (RT / (UNITS / MILLISECONDS)); +} + +/* This class hierarchy will support an IReferenceClock interface so + that an audio card (or other externally driven clock) can update the + system wide clock that everyone uses. + + The interface will be pretty thin with probably just one update method + This interface has not yet been defined. + */ + +/* This abstract base class implements the IReferenceClock + * interface. Classes that actually provide clock signals (from + * whatever source) have to be derived from this class. + * + * The abstract class provides implementations for: + * CUnknown support + * locking support (CCritSec) + * client advise code (creates a thread) + * + * Question: what can we do about quality? Change the timer + * resolution to lower the system load? Up the priority of the + * timer thread to force more responsive signals? + * + * During class construction we create a worker thread that is destroyed during + * destuction. This thread executes a series of WaitForSingleObject calls, + * waking up when a command is given to the thread or the next wake up point + * is reached. The wakeup points are determined by clients making Advise + * calls. + * + * Each advise call defines a point in time when they wish to be notified. A + * periodic advise is a series of these such events. We maintain a list of + * advise links and calculate when the nearest event notification is due for. + * We then call WaitForSingleObject with a timeout equal to this time. The + * handle we wait on is used by the class to signal that something has changed + * and that we must reschedule the next event. This typically happens when + * someone comes in and asks for an advise link while we are waiting for an + * event to timeout. + * + * While we are modifying the list of advise requests we + * are protected from interference through a critical section. Clients are NOT + * advised through callbacks. One shot clients have an event set, while + * periodic clients have a semaphore released for each event notification. A + * semaphore allows a client to be kept up to date with the number of events + * actually triggered and be assured that they can't miss multiple events being + * set. + * + * Keeping track of advises is taken care of by the CAMSchedule class. + */ + +class CBaseReferenceClock +: public CUnknown, public IReferenceClock, public CCritSec +{ +protected: + virtual ~CBaseReferenceClock(); // Don't let me be created on the stack! +public: + CBaseReferenceClock(TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr, CAMSchedule * pSched = 0 ); + + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,void ** ppv); + + DECLARE_IUNKNOWN + + /* IReferenceClock methods */ + // Derived classes must implement GetPrivateTime(). All our GetTime + // does is call GetPrivateTime and then check so that time does not + // go backwards. A return code of S_FALSE implies that the internal + // clock has gone backwards and GetTime time has halted until internal + // time has caught up. (Don't know if this will be much use to folk, + // but it seems odd not to use the return code for something useful.) + STDMETHODIMP GetTime(REFERENCE_TIME *pTime); + // When this is called, it sets m_rtLastGotTime to the time it returns. + + /* Provide standard mechanisms for scheduling events */ + + /* Ask for an async notification that a time has elapsed */ + STDMETHODIMP AdviseTime( + REFERENCE_TIME baseTime, // base reference time + REFERENCE_TIME streamTime, // stream offset time + HEVENT hEvent, // advise via this event + DWORD_PTR *pdwAdviseCookie // where your cookie goes + ); + + /* Ask for an asynchronous periodic notification that a time has elapsed */ + STDMETHODIMP AdvisePeriodic( + REFERENCE_TIME StartTime, // starting at this time + REFERENCE_TIME PeriodTime, // time between notifications + HSEMAPHORE hSemaphore, // advise via a semaphore + DWORD_PTR *pdwAdviseCookie // where your cookie goes + ); + + /* Cancel a request for notification(s) - if the notification was + * a one shot timer then this function doesn't need to be called + * as the advise is automatically cancelled, however it does no + * harm to explicitly cancel a one-shot advise. It is REQUIRED that + * clients call Unadvise to clear a Periodic advise setting. + */ + + STDMETHODIMP Unadvise(DWORD_PTR dwAdviseCookie); + + /* Methods for the benefit of derived classes or outer objects */ + + // GetPrivateTime() is the REAL clock. GetTime is just a cover for + // it. Derived classes will probably override this method but not + // GetTime() itself. + // The important point about GetPrivateTime() is it's allowed to go + // backwards. Our GetTime() will keep returning the LastGotTime + // until GetPrivateTime() catches up. + virtual REFERENCE_TIME GetPrivateTime(); + + /* Provide a method for correcting drift */ + STDMETHODIMP SetTimeDelta( const REFERENCE_TIME& TimeDelta ); + + CAMSchedule * GetSchedule() const { return m_pSchedule; } + +private: + REFERENCE_TIME m_rtPrivateTime; // Current best estimate of time + DWORD m_dwPrevSystemTime; // Last vaule we got from timeGetTime + REFERENCE_TIME m_rtLastGotTime; // Last time returned by GetTime + REFERENCE_TIME m_rtNextAdvise; // Time of next advise + UINT m_TimerResolution; + +#ifdef PERF + int m_idGetSystemTime; +#endif + +// Thread stuff +public: + void TriggerThread() // Wakes thread up. Need to do this if + { // time to next advise needs reevaluating. + EXECUTE_ASSERT(SetEvent(m_pSchedule->GetEvent())); + } + + +private: + BOOL m_bAbort; // Flag used for thread shutdown + HANDLE m_hThread; // Thread handle + + HRESULT AdviseThread(); // Method in which the advise thread runs + static DWORD __stdcall AdviseThreadFunction(LPVOID); // Function used to get there + +protected: + CAMSchedule * const m_pSchedule; + + void Restart (IN REFERENCE_TIME rtMinTime = 0I64) ; +}; + +#endif + diff --git a/plugins/GSdx/baseclasses/reftime.h b/plugins/GSdx/baseclasses/reftime.h new file mode 100644 index 0000000000..87c43b9fef --- /dev/null +++ b/plugins/GSdx/baseclasses/reftime.h @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// File: RefTime.h +// +// Desc: DirectShow base classes - defines CRefTime, a class that manages +// reference times. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// +// CRefTime +// +// Manage reference times. +// Shares same data layout as REFERENCE_TIME, but adds some (nonvirtual) +// functions providing simple comparison, conversion and arithmetic. +// +// A reference time (at the moment) is a unit of seconds represented in +// 100ns units as is used in the Win32 FILETIME structure. BUT the time +// a REFERENCE_TIME represents is NOT the time elapsed since 1/1/1601 it +// will either be stream time or reference time depending upon context +// +// This class provides simple arithmetic operations on reference times +// +// keep non-virtual otherwise the data layout will not be the same as +// REFERENCE_TIME + + +// ----- +// note that you are safe to cast a CRefTime* to a REFERENCE_TIME*, but +// you will need to do so explicitly +// ----- + + +#ifndef __REFTIME__ +#define __REFTIME__ + + +const LONGLONG MILLISECONDS = (1000); // 10 ^ 3 +const LONGLONG NANOSECONDS = (1000000000); // 10 ^ 9 +const LONGLONG UNITS = (NANOSECONDS / 100); // 10 ^ 7 + +/* Unfortunately an inline function here generates a call to __allmul + - even for constants! +*/ +#define MILLISECONDS_TO_100NS_UNITS(lMs) \ + Int32x32To64((lMs), (UNITS / MILLISECONDS)) + +class CRefTime +{ +public: + + // *MUST* be the only data member so that this class is exactly + // equivalent to a REFERENCE_TIME. + // Also, must be *no virtual functions* + + REFERENCE_TIME m_time; + + inline CRefTime() + { + // default to 0 time + m_time = 0; + }; + + inline CRefTime(LONG msecs) + { + m_time = MILLISECONDS_TO_100NS_UNITS(msecs); + }; + + inline CRefTime(REFERENCE_TIME rt) + { + m_time = rt; + }; + + inline operator REFERENCE_TIME() const + { + return m_time; + }; + + inline CRefTime& operator=(const CRefTime& rt) + { + m_time = rt.m_time; + return *this; + }; + + inline CRefTime& operator=(const LONGLONG ll) + { + m_time = ll; + return *this; + }; + + inline CRefTime& operator+=(const CRefTime& rt) + { + return (*this = *this + rt); + }; + + inline CRefTime& operator-=(const CRefTime& rt) + { + return (*this = *this - rt); + }; + + inline LONG Millisecs(void) + { + return (LONG)(m_time / (UNITS / MILLISECONDS)); + }; + + inline LONGLONG GetUnits(void) + { + return m_time; + }; +}; + +const LONGLONG TimeZero = 0; + +#endif /* __REFTIME__ */ + diff --git a/plugins/GSdx/baseclasses/renbase.cpp b/plugins/GSdx/baseclasses/renbase.cpp new file mode 100644 index 0000000000..633f26aadb --- /dev/null +++ b/plugins/GSdx/baseclasses/renbase.cpp @@ -0,0 +1,2844 @@ +//------------------------------------------------------------------------------ +// File: RenBase.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" // DirectShow base class definitions +#include // Needed for definition of timeGetTime +#include // Standard data type limit definitions +#include "measure.h" // Used for time critical log functions + +#pragma warning(disable:4355) + +// Helper function for clamping time differences +int inline TimeDiff(REFERENCE_TIME rt) +{ + if (rt < - (50 * UNITS)) { + return -(50 * UNITS); + } else + if (rt > 50 * UNITS) { + return 50 * UNITS; + } else return (int)rt; +} + +// Implements the CBaseRenderer class + +CBaseRenderer::CBaseRenderer(REFCLSID RenderClass, // CLSID for this renderer + TCHAR *pName, // Debug ONLY description + LPUNKNOWN pUnk, // Aggregated owner object + HRESULT *phr) : // General OLE return code + + CBaseFilter(pName,pUnk,&m_InterfaceLock,RenderClass), + m_evComplete(TRUE), + m_bAbort(FALSE), + m_pPosition(NULL), + m_ThreadSignal(TRUE), + m_bStreaming(FALSE), + m_bEOS(FALSE), + m_bEOSDelivered(FALSE), + m_pMediaSample(NULL), + m_dwAdvise(0), + m_pQSink(NULL), + m_pInputPin(NULL), + m_bRepaintStatus(TRUE), + m_SignalTime(0), + m_bInReceive(FALSE), + m_EndOfStreamTimer(0) +{ + Ready(); +#ifdef PERF + m_idBaseStamp = MSR_REGISTER(TEXT("BaseRenderer: sample time stamp")); + m_idBaseRenderTime = MSR_REGISTER(TEXT("BaseRenderer: draw time (msec)")); + m_idBaseAccuracy = MSR_REGISTER(TEXT("BaseRenderer: Accuracy (msec)")); +#endif +} + + +// Delete the dynamically allocated IMediaPosition and IMediaSeeking helper +// object. The object is created when somebody queries us. These are standard +// control interfaces for seeking and setting start/stop positions and rates. +// We will probably also have made an input pin based on CRendererInputPin +// that has to be deleted, it's created when an enumerator calls our GetPin + +CBaseRenderer::~CBaseRenderer() +{ + ASSERT(m_bStreaming == FALSE); + ASSERT(m_EndOfStreamTimer == 0); + StopStreaming(); + ClearPendingSample(); + + // Delete any IMediaPosition implementation + + if (m_pPosition) { + delete m_pPosition; + m_pPosition = NULL; + } + + // Delete any input pin created + + if (m_pInputPin) { + delete m_pInputPin; + m_pInputPin = NULL; + } + + // Release any Quality sink + + ASSERT(m_pQSink == NULL); +} + + +// This returns the IMediaPosition and IMediaSeeking interfaces + +HRESULT CBaseRenderer::GetMediaPositionInterface(REFIID riid,void **ppv) +{ + CAutoLock cObjectCreationLock(&m_ObjectCreationLock); + if (m_pPosition) { + return m_pPosition->NonDelegatingQueryInterface(riid,ppv); + } + + HRESULT hr = NOERROR; + + // Create implementation of this dynamically since sometimes we may + // never try and do a seek. The helper object implements a position + // control interface (IMediaPosition) which in fact simply takes the + // calls normally from the filter graph and passes them upstream + + m_pPosition = new CRendererPosPassThru(NAME("Renderer CPosPassThru"), + CBaseFilter::GetOwner(), + (HRESULT *) &hr, + GetPin(0)); + if (m_pPosition == NULL) { + return E_OUTOFMEMORY; + } + + if (FAILED(hr)) { + delete m_pPosition; + m_pPosition = NULL; + return E_NOINTERFACE; + } + return GetMediaPositionInterface(riid,ppv); +} + + +// Overriden to say what interfaces we support and where + +STDMETHODIMP CBaseRenderer::NonDelegatingQueryInterface(REFIID riid,void **ppv) +{ + // Do we have this interface + + if (riid == IID_IMediaPosition || riid == IID_IMediaSeeking) { + return GetMediaPositionInterface(riid,ppv); + } else { + return CBaseFilter::NonDelegatingQueryInterface(riid,ppv); + } +} + + +// This is called whenever we change states, we have a manual reset event that +// is signalled whenever we don't won't the source filter thread to wait in us +// (such as in a stopped state) and likewise is not signalled whenever it can +// wait (during paused and running) this function sets or resets the thread +// event. The event is used to stop source filter threads waiting in Receive + +HRESULT CBaseRenderer::SourceThreadCanWait(BOOL bCanWait) +{ + if (bCanWait == TRUE) { + m_ThreadSignal.Reset(); + } else { + m_ThreadSignal.Set(); + } + return NOERROR; +} + + +#ifdef DEBUG +// Dump the current renderer state to the debug terminal. The hardest part of +// the renderer is the window where we unlock everything to wait for a clock +// to signal it is time to draw or for the application to cancel everything +// by stopping the filter. If we get things wrong we can leave the thread in +// WaitForRenderTime with no way for it to ever get out and we will deadlock + +void CBaseRenderer::DisplayRendererState() +{ + DbgLog((LOG_TIMING, 1, TEXT("\nTimed out in WaitForRenderTime"))); + + // No way should this be signalled at this point + + BOOL bSignalled = m_ThreadSignal.Check(); + DbgLog((LOG_TIMING, 1, TEXT("Signal sanity check %d"),bSignalled)); + + // Now output the current renderer state variables + + DbgLog((LOG_TIMING, 1, TEXT("Filter state %d"),m_State)); + + DbgLog((LOG_TIMING, 1, TEXT("Abort flag %d"),m_bAbort)); + + DbgLog((LOG_TIMING, 1, TEXT("Streaming flag %d"),m_bStreaming)); + + DbgLog((LOG_TIMING, 1, TEXT("Clock advise link %d"),m_dwAdvise)); + + DbgLog((LOG_TIMING, 1, TEXT("Current media sample %x"),m_pMediaSample)); + + DbgLog((LOG_TIMING, 1, TEXT("EOS signalled %d"),m_bEOS)); + + DbgLog((LOG_TIMING, 1, TEXT("EOS delivered %d"),m_bEOSDelivered)); + + DbgLog((LOG_TIMING, 1, TEXT("Repaint status %d"),m_bRepaintStatus)); + + + // Output the delayed end of stream timer information + + DbgLog((LOG_TIMING, 1, TEXT("End of stream timer %x"),m_EndOfStreamTimer)); + + DbgLog((LOG_TIMING, 1, TEXT("Deliver time %s"),CDisp((LONGLONG)m_SignalTime))); + + + // Should never timeout during a flushing state + + BOOL bFlushing = m_pInputPin->IsFlushing(); + DbgLog((LOG_TIMING, 1, TEXT("Flushing sanity check %d"),bFlushing)); + + // Display the time we were told to start at + DbgLog((LOG_TIMING, 1, TEXT("Last run time %s"),CDisp((LONGLONG)m_tStart.m_time))); + + // Have we got a reference clock + if (m_pClock == NULL) return; + + // Get the current time from the wall clock + + CRefTime CurrentTime,StartTime,EndTime; + m_pClock->GetTime((REFERENCE_TIME*) &CurrentTime); + CRefTime Offset = CurrentTime - m_tStart; + + // Display the current time from the clock + + DbgLog((LOG_TIMING, 1, TEXT("Clock time %s"),CDisp((LONGLONG)CurrentTime.m_time))); + + DbgLog((LOG_TIMING, 1, TEXT("Time difference %dms"),Offset.Millisecs())); + + + // Do we have a sample ready to render + if (m_pMediaSample == NULL) return; + + m_pMediaSample->GetTime((REFERENCE_TIME*)&StartTime, (REFERENCE_TIME*)&EndTime); + DbgLog((LOG_TIMING, 1, TEXT("Next sample stream times (Start %d End %d ms)"), + StartTime.Millisecs(),EndTime.Millisecs())); + + // Calculate how long it is until it is due for rendering + CRefTime Wait = (m_tStart + StartTime) - CurrentTime; + DbgLog((LOG_TIMING, 1, TEXT("Wait required %d ms"),Wait.Millisecs())); +} +#endif + + +// Wait until the clock sets the timer event or we're otherwise signalled. We +// set an arbitrary timeout for this wait and if it fires then we display the +// current renderer state on the debugger. It will often fire if the filter's +// left paused in an application however it may also fire during stress tests +// if the synchronisation with application seeks and state changes is faulty + +#define RENDER_TIMEOUT 10000 + +HRESULT CBaseRenderer::WaitForRenderTime() +{ + HANDLE WaitObjects[] = { m_ThreadSignal, m_RenderEvent }; + DWORD Result = WAIT_TIMEOUT; + + // Wait for either the time to arrive or for us to be stopped + + OnWaitStart(); + while (Result == WAIT_TIMEOUT) { + Result = WaitForMultipleObjects(2,WaitObjects,FALSE,RENDER_TIMEOUT); + +#ifdef DEBUG + if (Result == WAIT_TIMEOUT) DisplayRendererState(); +#endif + + } + OnWaitEnd(); + + // We may have been awoken without the timer firing + + if (Result == WAIT_OBJECT_0) { + return VFW_E_STATE_CHANGED; + } + + SignalTimerFired(); + return NOERROR; +} + + +// Poll waiting for Receive to complete. This really matters when +// Receive may set the palette and cause window messages +// The problem is that if we don't really wait for a renderer to +// stop processing we can deadlock waiting for a transform which +// is calling the renderer's Receive() method because the transform's +// Stop method doesn't know to process window messages to unblock +// the renderer's Receive processing +void CBaseRenderer::WaitForReceiveToComplete() +{ + for (;;) { + if (!m_bInReceive) { + break; + } + + MSG msg; + // Receive all interthread sendmessages + PeekMessage(&msg, NULL, WM_NULL, WM_NULL, PM_NOREMOVE); + + Sleep(1); + } + + // If the wakebit for QS_POSTMESSAGE is set, the PeekMessage call + // above just cleared the changebit which will cause some messaging + // calls to block (waitMessage, MsgWaitFor...) now. + // Post a dummy message to set the QS_POSTMESSAGE bit again + if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) { + // Send dummy message + PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0); + } +} + +// A filter can have four discrete states, namely Stopped, Running, Paused, +// Intermediate. We are in an intermediate state if we are currently trying +// to pause but haven't yet got the first sample (or if we have been flushed +// in paused state and therefore still have to wait for a sample to arrive) + +// This class contains an event called m_evComplete which is signalled when +// the current state is completed and is not signalled when we are waiting to +// complete the last state transition. As mentioned above the only time we +// use this at the moment is when we wait for a media sample in paused state +// If while we are waiting we receive an end of stream notification from the +// source filter then we know no data is imminent so we can reset the event +// This means that when we transition to paused the source filter must call +// end of stream on us or send us an image otherwise we'll hang indefinately + + +// Simple internal way of getting the real state + +FILTER_STATE CBaseRenderer::GetRealState() { + return m_State; +} + + +// The renderer doesn't complete the full transition to paused states until +// it has got one media sample to render. If you ask it for its state while +// it's waiting it will return the state along with VFW_S_STATE_INTERMEDIATE + +STDMETHODIMP CBaseRenderer::GetState(DWORD dwMSecs,FILTER_STATE *State) +{ + CheckPointer(State,E_POINTER); + + if (WaitDispatchingMessages(m_evComplete, dwMSecs) == WAIT_TIMEOUT) { + *State = m_State; + return VFW_S_STATE_INTERMEDIATE; + } + *State = m_State; + return NOERROR; +} + + +// If we're pausing and we have no samples we don't complete the transition +// to State_Paused and we return S_FALSE. However if the m_bAbort flag has +// been set then all samples are rejected so there is no point waiting for +// one. If we do have a sample then return NOERROR. We will only ever return +// VFW_S_STATE_INTERMEDIATE from GetState after being paused with no sample +// (calling GetState after either being stopped or Run will NOT return this) + +HRESULT CBaseRenderer::CompleteStateChange(FILTER_STATE OldState) +{ + // Allow us to be paused when disconnected + + if (m_pInputPin->IsConnected() == FALSE) { + Ready(); + return S_OK; + } + + // Have we run off the end of stream + + if (IsEndOfStream() == TRUE) { + Ready(); + return S_OK; + } + + // Make sure we get fresh data after being stopped + + if (HaveCurrentSample() == TRUE) { + if (OldState != State_Stopped) { + Ready(); + return S_OK; + } + } + NotReady(); + return S_FALSE; +} + + +// When we stop the filter the things we do are:- + +// Decommit the allocator being used in the connection +// Release the source filter if it's waiting in Receive +// Cancel any advise link we set up with the clock +// Any end of stream signalled is now obsolete so reset +// Allow us to be stopped when we are not connected + +STDMETHODIMP CBaseRenderer::Stop() +{ + CAutoLock cRendererLock(&m_InterfaceLock); + + // Make sure there really is a state change + + if (m_State == State_Stopped) { + return NOERROR; + } + + // Is our input pin connected + + if (m_pInputPin->IsConnected() == FALSE) { + NOTE("Input pin is not connected"); + m_State = State_Stopped; + return NOERROR; + } + + CBaseFilter::Stop(); + + // If we are going into a stopped state then we must decommit whatever + // allocator we are using it so that any source filter waiting in the + // GetBuffer can be released and unlock themselves for a state change + + if (m_pInputPin->Allocator()) { + m_pInputPin->Allocator()->Decommit(); + } + + // Cancel any scheduled rendering + + SetRepaintStatus(TRUE); + StopStreaming(); + SourceThreadCanWait(FALSE); + ResetEndOfStream(); + CancelNotification(); + + // There should be no outstanding clock advise + ASSERT(CancelNotification() == S_FALSE); + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + ASSERT(m_EndOfStreamTimer == 0); + + Ready(); + WaitForReceiveToComplete(); + m_bAbort = FALSE; + + return NOERROR; +} + + +// When we pause the filter the things we do are:- + +// Commit the allocator being used in the connection +// Allow a source filter thread to wait in Receive +// Cancel any clock advise link (we may be running) +// Possibly complete the state change if we have data +// Allow us to be paused when we are not connected + +STDMETHODIMP CBaseRenderer::Pause() +{ + CAutoLock cRendererLock(&m_InterfaceLock); + FILTER_STATE OldState = m_State; + ASSERT(m_pInputPin->IsFlushing() == FALSE); + + // Make sure there really is a state change + + if (m_State == State_Paused) { + return CompleteStateChange(State_Paused); + } + + // Has our input pin been connected + + if (m_pInputPin->IsConnected() == FALSE) { + NOTE("Input pin is not connected"); + m_State = State_Paused; + return CompleteStateChange(State_Paused); + } + + // Pause the base filter class + + HRESULT hr = CBaseFilter::Pause(); + if (FAILED(hr)) { + NOTE("Pause failed"); + return hr; + } + + // Enable EC_REPAINT events again + + SetRepaintStatus(TRUE); + StopStreaming(); + SourceThreadCanWait(TRUE); + CancelNotification(); + ResetEndOfStreamTimer(); + + // If we are going into a paused state then we must commit whatever + // allocator we are using it so that any source filter can call the + // GetBuffer and expect to get a buffer without returning an error + + if (m_pInputPin->Allocator()) { + m_pInputPin->Allocator()->Commit(); + } + + // There should be no outstanding advise + ASSERT(CancelNotification() == S_FALSE); + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + ASSERT(m_EndOfStreamTimer == 0); + ASSERT(m_pInputPin->IsFlushing() == FALSE); + + // When we come out of a stopped state we must clear any image we were + // holding onto for frame refreshing. Since renderers see state changes + // first we can reset ourselves ready to accept the source thread data + // Paused or running after being stopped causes the current position to + // be reset so we're not interested in passing end of stream signals + + if (OldState == State_Stopped) { + m_bAbort = FALSE; + ClearPendingSample(); + } + return CompleteStateChange(OldState); +} + + +// When we run the filter the things we do are:- + +// Commit the allocator being used in the connection +// Allow a source filter thread to wait in Receive +// Signal the render event just to get us going +// Start the base class by calling StartStreaming +// Allow us to be run when we are not connected +// Signal EC_COMPLETE if we are not connected + +STDMETHODIMP CBaseRenderer::Run(REFERENCE_TIME StartTime) +{ + CAutoLock cRendererLock(&m_InterfaceLock); + FILTER_STATE OldState = m_State; + + // Make sure there really is a state change + + if (m_State == State_Running) { + return NOERROR; + } + + // Send EC_COMPLETE if we're not connected + + if (m_pInputPin->IsConnected() == FALSE) { + NotifyEvent(EC_COMPLETE,S_OK,(LONG_PTR)(IBaseFilter *)this); + m_State = State_Running; + return NOERROR; + } + + Ready(); + + // Pause the base filter class + + HRESULT hr = CBaseFilter::Run(StartTime); + if (FAILED(hr)) { + NOTE("Run failed"); + return hr; + } + + // Allow the source thread to wait + ASSERT(m_pInputPin->IsFlushing() == FALSE); + SourceThreadCanWait(TRUE); + SetRepaintStatus(FALSE); + + // There should be no outstanding advise + ASSERT(CancelNotification() == S_FALSE); + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + ASSERT(m_EndOfStreamTimer == 0); + ASSERT(m_pInputPin->IsFlushing() == FALSE); + + // If we are going into a running state then we must commit whatever + // allocator we are using it so that any source filter can call the + // GetBuffer and expect to get a buffer without returning an error + + if (m_pInputPin->Allocator()) { + m_pInputPin->Allocator()->Commit(); + } + + // When we come out of a stopped state we must clear any image we were + // holding onto for frame refreshing. Since renderers see state changes + // first we can reset ourselves ready to accept the source thread data + // Paused or running after being stopped causes the current position to + // be reset so we're not interested in passing end of stream signals + + if (OldState == State_Stopped) { + m_bAbort = FALSE; + ClearPendingSample(); + } + return StartStreaming(); +} + + +// Return the number of input pins we support + +int CBaseRenderer::GetPinCount() +{ + return 1; +} + + +// We only support one input pin and it is numbered zero + +CBasePin *CBaseRenderer::GetPin(int n) +{ + CAutoLock cObjectCreationLock(&m_ObjectCreationLock); + + // Should only ever be called with zero + ASSERT(n == 0); + + if (n != 0) { + return NULL; + } + + // Create the input pin if not already done so + + if (m_pInputPin == NULL) { + + // hr must be initialized to NOERROR because + // CRendererInputPin's constructor only changes + // hr's value if an error occurs. + HRESULT hr = NOERROR; + + m_pInputPin = new CRendererInputPin(this,&hr,L"In"); + if (NULL == m_pInputPin) { + return NULL; + } + + if (FAILED(hr)) { + delete m_pInputPin; + m_pInputPin = NULL; + return NULL; + } + } + return m_pInputPin; +} + + +// If "In" then return the IPin for our input pin, otherwise NULL and error + +STDMETHODIMP CBaseRenderer::FindPin(LPCWSTR Id, IPin **ppPin) +{ + CheckPointer(ppPin,E_POINTER); + + if (0==lstrcmpW(Id,L"In")) { + *ppPin = GetPin(0); + ASSERT(*ppPin); + (*ppPin)->AddRef(); + } else { + *ppPin = NULL; + return VFW_E_NOT_FOUND; + } + return NOERROR; +} + + +// Called when the input pin receives an EndOfStream notification. If we have +// not got a sample, then notify EC_COMPLETE now. If we have samples, then set +// m_bEOS and check for this on completing samples. If we're waiting to pause +// then complete the transition to paused state by setting the state event + +HRESULT CBaseRenderer::EndOfStream() +{ + // Ignore these calls if we are stopped + + if (m_State == State_Stopped) { + return NOERROR; + } + + // If we have a sample then wait for it to be rendered + + m_bEOS = TRUE; + if (m_pMediaSample) { + return NOERROR; + } + + // If we are waiting for pause then we are now ready since we cannot now + // carry on waiting for a sample to arrive since we are being told there + // won't be any. This sets an event that the GetState function picks up + + Ready(); + + // Only signal completion now if we are running otherwise queue it until + // we do run in StartStreaming. This is used when we seek because a seek + // causes a pause where early notification of completion is misleading + + if (m_bStreaming) { + SendEndOfStream(); + } + return NOERROR; +} + + +// When we are told to flush we should release the source thread + +HRESULT CBaseRenderer::BeginFlush() +{ + // If paused then report state intermediate until we get some data + + if (m_State == State_Paused) { + NotReady(); + } + + SourceThreadCanWait(FALSE); + CancelNotification(); + ClearPendingSample(); + // Wait for Receive to complete + WaitForReceiveToComplete(); + + return NOERROR; +} + + +// After flushing the source thread can wait in Receive again + +HRESULT CBaseRenderer::EndFlush() +{ + // Reset the current sample media time + if (m_pPosition) m_pPosition->ResetMediaTime(); + + // There should be no outstanding advise + + ASSERT(CancelNotification() == S_FALSE); + SourceThreadCanWait(TRUE); + return NOERROR; +} + + +// We can now send EC_REPAINTs if so required + +HRESULT CBaseRenderer::CompleteConnect(IPin *pReceivePin) +{ + // The caller should always hold the interface lock because + // the function uses CBaseFilter::m_State. + ASSERT(CritCheckIn(&m_InterfaceLock)); + + m_bAbort = FALSE; + + if (State_Running == GetRealState()) { + HRESULT hr = StartStreaming(); + if (FAILED(hr)) { + return hr; + } + + SetRepaintStatus(FALSE); + } else { + SetRepaintStatus(TRUE); + } + + return NOERROR; +} + + +// Called when we go paused or running + +HRESULT CBaseRenderer::Active() +{ + return NOERROR; +} + + +// Called when we go into a stopped state + +HRESULT CBaseRenderer::Inactive() +{ + if (m_pPosition) { + m_pPosition->ResetMediaTime(); + } + // People who derive from this may want to override this behaviour + // to keep hold of the sample in some circumstances + ClearPendingSample(); + + return NOERROR; +} + + +// Tell derived classes about the media type agreed + +HRESULT CBaseRenderer::SetMediaType(const CMediaType *pmt) +{ + return NOERROR; +} + + +// When we break the input pin connection we should reset the EOS flags. When +// we are asked for either IMediaPosition or IMediaSeeking we will create a +// CPosPassThru object to handles media time pass through. When we're handed +// samples we store (by calling CPosPassThru::RegisterMediaTime) their media +// times so we can then return a real current position of data being rendered + +HRESULT CBaseRenderer::BreakConnect() +{ + // Do we have a quality management sink + + if (m_pQSink) { + m_pQSink->Release(); + m_pQSink = NULL; + } + + // Check we have a valid connection + + if (m_pInputPin->IsConnected() == FALSE) { + return S_FALSE; + } + + // Check we are stopped before disconnecting + if (m_State != State_Stopped && !m_pInputPin->CanReconnectWhenActive()) { + return VFW_E_NOT_STOPPED; + } + + SetRepaintStatus(FALSE); + ResetEndOfStream(); + ClearPendingSample(); + m_bAbort = FALSE; + + if (State_Running == m_State) { + StopStreaming(); + } + + return NOERROR; +} + + +// Retrieves the sample times for this samples (note the sample times are +// passed in by reference not value). We return S_FALSE to say schedule this +// sample according to the times on the sample. We also return S_OK in +// which case the object should simply render the sample data immediately + +HRESULT CBaseRenderer::GetSampleTimes(IMediaSample *pMediaSample, + REFERENCE_TIME *pStartTime, + REFERENCE_TIME *pEndTime) +{ + ASSERT(m_dwAdvise == 0); + ASSERT(pMediaSample); + + // If the stop time for this sample is before or the same as start time, + // then just ignore it (release it) and schedule the next one in line + // Source filters should always fill in the start and end times properly! + + if (SUCCEEDED(pMediaSample->GetTime(pStartTime, pEndTime))) { + if (*pEndTime < *pStartTime) { + return VFW_E_START_TIME_AFTER_END; + } + } else { + // no time set in the sample... draw it now? + return S_OK; + } + + // Can't synchronise without a clock so we return S_OK which tells the + // caller that the sample should be rendered immediately without going + // through the overhead of setting a timer advise link with the clock + + if (m_pClock == NULL) { + return S_OK; + } + return ShouldDrawSampleNow(pMediaSample,pStartTime,pEndTime); +} + + +// By default all samples are drawn according to their time stamps so we +// return S_FALSE. Returning S_OK means draw immediately, this is used +// by the derived video renderer class in its quality management. + +HRESULT CBaseRenderer::ShouldDrawSampleNow(IMediaSample *pMediaSample, + REFERENCE_TIME *ptrStart, + REFERENCE_TIME *ptrEnd) +{ + return S_FALSE; +} + + +// We must always reset the current advise time to zero after a timer fires +// because there are several possible ways which lead us not to do any more +// scheduling such as the pending image being cleared after state changes + +void CBaseRenderer::SignalTimerFired() +{ + m_dwAdvise = 0; +} + + +// Cancel any notification currently scheduled. This is called by the owning +// window object when it is told to stop streaming. If there is no timer link +// outstanding then calling this is benign otherwise we go ahead and cancel +// We must always reset the render event as the quality management code can +// signal immediate rendering by setting the event without setting an advise +// link. If we're subsequently stopped and run the first attempt to setup an +// advise link with the reference clock will find the event still signalled + +HRESULT CBaseRenderer::CancelNotification() +{ + ASSERT(m_dwAdvise == 0 || m_pClock); + DWORD_PTR dwAdvise = m_dwAdvise; + + // Have we a live advise link + + if (m_dwAdvise) { + m_pClock->Unadvise(m_dwAdvise); + SignalTimerFired(); + ASSERT(m_dwAdvise == 0); + } + + // Clear the event and return our status + + m_RenderEvent.Reset(); + return (dwAdvise ? S_OK : S_FALSE); +} + + +// Responsible for setting up one shot advise links with the clock +// Return FALSE if the sample is to be dropped (not drawn at all) +// Return TRUE if the sample is to be drawn and in this case also +// arrange for m_RenderEvent to be set at the appropriate time + +BOOL CBaseRenderer::ScheduleSample(IMediaSample *pMediaSample) +{ + REFERENCE_TIME StartSample, EndSample; + + // Is someone pulling our leg + + if (pMediaSample == NULL) { + return FALSE; + } + + // Get the next sample due up for rendering. If there aren't any ready + // then GetNextSampleTimes returns an error. If there is one to be done + // then it succeeds and yields the sample times. If it is due now then + // it returns S_OK other if it's to be done when due it returns S_FALSE + + HRESULT hr = GetSampleTimes(pMediaSample, &StartSample, &EndSample); + if (FAILED(hr)) { + return FALSE; + } + + // If we don't have a reference clock then we cannot set up the advise + // time so we simply set the event indicating an image to render. This + // will cause us to run flat out without any timing or synchronisation + + if (hr == S_OK) { + EXECUTE_ASSERT(SetEvent((HANDLE) m_RenderEvent)); + return TRUE; + } + + ASSERT(m_dwAdvise == 0); + ASSERT(m_pClock); + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + + // We do have a valid reference clock interface so we can ask it to + // set an event when the image comes due for rendering. We pass in + // the reference time we were told to start at and also the current + // stream time which is the offset from the start reference time + + hr = m_pClock->AdviseTime( + (REFERENCE_TIME) m_tStart, // Start run time + StartSample, // Stream time + (HEVENT)(HANDLE) m_RenderEvent, // Render notification + &m_dwAdvise); // Advise cookie + + if (SUCCEEDED(hr)) { + return TRUE; + } + + // We could not schedule the next sample for rendering despite the fact + // we have a valid sample here. This is a fair indication that either + // the system clock is wrong or the time stamp for the sample is duff + + ASSERT(m_dwAdvise == 0); + return FALSE; +} + + +// This is called when a sample comes due for rendering. We pass the sample +// on to the derived class. After rendering we will initialise the timer for +// the next sample, NOTE signal that the last one fired first, if we don't +// do this it thinks there is still one outstanding that hasn't completed + +HRESULT CBaseRenderer::Render(IMediaSample *pMediaSample) +{ + // If the media sample is NULL then we will have been notified by the + // clock that another sample is ready but in the mean time someone has + // stopped us streaming which causes the next sample to be released + + if (pMediaSample == NULL) { + return S_FALSE; + } + + // If we have stopped streaming then don't render any more samples, the + // thread that got in and locked us and then reset this flag does not + // clear the pending sample as we can use it to refresh any output device + + if (m_bStreaming == FALSE) { + return S_FALSE; + } + + // Time how long the rendering takes + + OnRenderStart(pMediaSample); + DoRenderSample(pMediaSample); + OnRenderEnd(pMediaSample); + + return NOERROR; +} + + +// Checks if there is a sample waiting at the renderer + +BOOL CBaseRenderer::HaveCurrentSample() +{ + CAutoLock cRendererLock(&m_RendererLock); + return (m_pMediaSample == NULL ? FALSE : TRUE); +} + + +// Returns the current sample waiting at the video renderer. We AddRef the +// sample before returning so that should it come due for rendering the +// person who called this method will hold the remaining reference count +// that will stop the sample being added back onto the allocator free list + +IMediaSample *CBaseRenderer::GetCurrentSample() +{ + CAutoLock cRendererLock(&m_RendererLock); + if (m_pMediaSample) { + m_pMediaSample->AddRef(); + } + return m_pMediaSample; +} + + +// Called when the source delivers us a sample. We go through a few checks to +// make sure the sample can be rendered. If we are running (streaming) then we +// have the sample scheduled with the reference clock, if we are not streaming +// then we have received an sample in paused mode so we can complete any state +// transition. On leaving this function everything will be unlocked so an app +// thread may get in and change our state to stopped (for example) in which +// case it will also signal the thread event so that our wait call is stopped + +HRESULT CBaseRenderer::PrepareReceive(IMediaSample *pMediaSample) +{ + CAutoLock cInterfaceLock(&m_InterfaceLock); + m_bInReceive = TRUE; + + // Check our flushing and filter state + + // This function must hold the interface lock because it calls + // CBaseInputPin::Receive() and CBaseInputPin::Receive() uses + // CBasePin::m_bRunTimeError. + HRESULT hr = m_pInputPin->CBaseInputPin::Receive(pMediaSample); + + if (hr != NOERROR) { + m_bInReceive = FALSE; + return E_FAIL; + } + + // Has the type changed on a media sample. We do all rendering + // synchronously on the source thread, which has a side effect + // that only one buffer is ever outstanding. Therefore when we + // have Receive called we can go ahead and change the format + // Since the format change can cause a SendMessage we just don't + // lock + if (m_pInputPin->SampleProps()->pMediaType) { + hr = m_pInputPin->SetMediaType( + (CMediaType *)m_pInputPin->SampleProps()->pMediaType); + if (FAILED(hr)) { + m_bInReceive = FALSE; + return hr; + } + } + + + CAutoLock cSampleLock(&m_RendererLock); + + ASSERT(IsActive() == TRUE); + ASSERT(m_pInputPin->IsFlushing() == FALSE); + ASSERT(m_pInputPin->IsConnected() == TRUE); + ASSERT(m_pMediaSample == NULL); + + // Return an error if we already have a sample waiting for rendering + // source pins must serialise the Receive calls - we also check that + // no data is being sent after the source signalled an end of stream + + if (m_pMediaSample || m_bEOS || m_bAbort) { + Ready(); + m_bInReceive = FALSE; + return E_UNEXPECTED; + } + + // Store the media times from this sample + if (m_pPosition) m_pPosition->RegisterMediaTime(pMediaSample); + + // Schedule the next sample if we are streaming + + if ((m_bStreaming == TRUE) && (ScheduleSample(pMediaSample) == FALSE)) { + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + ASSERT(CancelNotification() == S_FALSE); + m_bInReceive = FALSE; + return VFW_E_SAMPLE_REJECTED; + } + + // Store the sample end time for EC_COMPLETE handling + m_SignalTime = m_pInputPin->SampleProps()->tStop; + + // BEWARE we sometimes keep the sample even after returning the thread to + // the source filter such as when we go into a stopped state (we keep it + // to refresh the device with) so we must AddRef it to keep it safely. If + // we start flushing the source thread is released and any sample waiting + // will be released otherwise GetBuffer may never return (see BeginFlush) + + m_pMediaSample = pMediaSample; + m_pMediaSample->AddRef(); + + if (m_bStreaming == FALSE) { + SetRepaintStatus(TRUE); + } + return NOERROR; +} + + +// Called by the source filter when we have a sample to render. Under normal +// circumstances we set an advise link with the clock, wait for the time to +// arrive and then render the data using the PURE virtual DoRenderSample that +// the derived class will have overriden. After rendering the sample we may +// also signal EOS if it was the last one sent before EndOfStream was called + +HRESULT CBaseRenderer::Receive(IMediaSample *pSample) +{ + ASSERT(pSample); + + // It may return VFW_E_SAMPLE_REJECTED code to say don't bother + + HRESULT hr = PrepareReceive(pSample); + ASSERT(m_bInReceive == SUCCEEDED(hr)); + if (FAILED(hr)) { + if (hr == VFW_E_SAMPLE_REJECTED) { + return NOERROR; + } + return hr; + } + + // We realize the palette in "PrepareRender()" so we have to give away the + // filter lock here. + if (m_State == State_Paused) { + PrepareRender(); + // no need to use InterlockedExchange + m_bInReceive = FALSE; + { + // We must hold both these locks + CAutoLock cRendererLock(&m_InterfaceLock); + if (m_State == State_Stopped) + return NOERROR; + + m_bInReceive = TRUE; + CAutoLock cSampleLock(&m_RendererLock); + OnReceiveFirstSample(pSample); + } + Ready(); + } + // Having set an advise link with the clock we sit and wait. We may be + // awoken by the clock firing or by a state change. The rendering call + // will lock the critical section and check we can still render the data + + hr = WaitForRenderTime(); + if (FAILED(hr)) { + m_bInReceive = FALSE; + return NOERROR; + } + + PrepareRender(); + + // Set this here and poll it until we work out the locking correctly + // It can't be right that the streaming stuff grabs the interface + // lock - after all we want to be able to wait for this stuff + // to complete + m_bInReceive = FALSE; + + // We must hold both these locks + CAutoLock cRendererLock(&m_InterfaceLock); + + // since we gave away the filter wide lock, the sate of the filter could + // have chnaged to Stopped + if (m_State == State_Stopped) + return NOERROR; + + CAutoLock cSampleLock(&m_RendererLock); + + // Deal with this sample + + Render(m_pMediaSample); + ClearPendingSample(); + SendEndOfStream(); + CancelNotification(); + return NOERROR; +} + + +// This is called when we stop or are inactivated to clear the pending sample +// We release the media sample interface so that they can be allocated to the +// source filter again, unless of course we are changing state to inactive in +// which case GetBuffer will return an error. We must also reset the current +// media sample to NULL so that we know we do not currently have an image + +HRESULT CBaseRenderer::ClearPendingSample() +{ + CAutoLock cRendererLock(&m_RendererLock); + if (m_pMediaSample) { + m_pMediaSample->Release(); + m_pMediaSample = NULL; + } + return NOERROR; +} + + +// Used to signal end of stream according to the sample end time + +void CALLBACK EndOfStreamTimer(UINT uID, // Timer identifier + UINT uMsg, // Not currently used + DWORD_PTR dwUser,// User information + DWORD_PTR dw1, // Windows reserved + DWORD_PTR dw2) // is also reserved +{ + CBaseRenderer *pRenderer = (CBaseRenderer *) dwUser; + NOTE1("EndOfStreamTimer called (%d)",uID); + pRenderer->TimerCallback(); +} + +// Do the timer callback work +void CBaseRenderer::TimerCallback() +{ + // Lock for synchronization (but don't hold this lock when calling + // timeKillEvent) + CAutoLock cRendererLock(&m_RendererLock); + + // See if we should signal end of stream now + + if (m_EndOfStreamTimer) { + m_EndOfStreamTimer = 0; + SendEndOfStream(); + } +} + + +// If we are at the end of the stream signal the filter graph but do not set +// the state flag back to FALSE. Once we drop off the end of the stream we +// leave the flag set (until a subsequent ResetEndOfStream). Each sample we +// get delivered will update m_SignalTime to be the last sample's end time. +// We must wait this long before signalling end of stream to the filtergraph + +#define TIMEOUT_DELIVERYWAIT 50 +#define TIMEOUT_RESOLUTION 10 + +HRESULT CBaseRenderer::SendEndOfStream() +{ + ASSERT(CritCheckIn(&m_RendererLock)); + if (m_bEOS == FALSE || m_bEOSDelivered || m_EndOfStreamTimer) { + return NOERROR; + } + + // If there is no clock then signal immediately + if (m_pClock == NULL) { + return NotifyEndOfStream(); + } + + // How long into the future is the delivery time + + REFERENCE_TIME Signal = m_tStart + m_SignalTime; + REFERENCE_TIME CurrentTime; + m_pClock->GetTime(&CurrentTime); + LONG Delay = LONG((Signal - CurrentTime) / 10000); + + // Dump the timing information to the debugger + + NOTE1("Delay until end of stream delivery %d",Delay); + NOTE1("Current %s",(LPCTSTR)CDisp((LONGLONG)CurrentTime)); + NOTE1("Signal %s",(LPCTSTR)CDisp((LONGLONG)Signal)); + + // Wait for the delivery time to arrive + + if (Delay < TIMEOUT_DELIVERYWAIT) { + return NotifyEndOfStream(); + } + + // Signal a timer callback on another worker thread + + m_EndOfStreamTimer = CompatibleTimeSetEvent((UINT) Delay, // Period of timer + TIMEOUT_RESOLUTION, // Timer resolution + EndOfStreamTimer, // Callback function + DWORD_PTR(this), // Used information + TIME_ONESHOT); // Type of callback + if (m_EndOfStreamTimer == 0) { + return NotifyEndOfStream(); + } + return NOERROR; +} + + +// Signals EC_COMPLETE to the filtergraph manager + +HRESULT CBaseRenderer::NotifyEndOfStream() +{ + CAutoLock cRendererLock(&m_RendererLock); + ASSERT(m_bEOSDelivered == FALSE); + ASSERT(m_EndOfStreamTimer == 0); + + // Has the filter changed state + + if (m_bStreaming == FALSE) { + ASSERT(m_EndOfStreamTimer == 0); + return NOERROR; + } + + // Reset the end of stream timer + m_EndOfStreamTimer = 0; + + // If we've been using the IMediaPosition interface, set it's start + // and end media "times" to the stop position by hand. This ensures + // that we actually get to the end, even if the MPEG guestimate has + // been bad or if the quality management dropped the last few frames + + if (m_pPosition) m_pPosition->EOS(); + m_bEOSDelivered = TRUE; + NOTE("Sending EC_COMPLETE..."); + return NotifyEvent(EC_COMPLETE,S_OK,(LONG_PTR)(IBaseFilter *)this); +} + + +// Reset the end of stream flag, this is typically called when we transfer to +// stopped states since that resets the current position back to the start so +// we will receive more samples or another EndOfStream if there aren't any. We +// keep two separate flags one to say we have run off the end of the stream +// (this is the m_bEOS flag) and another to say we have delivered EC_COMPLETE +// to the filter graph. We need the latter otherwise we can end up sending an +// EC_COMPLETE every time the source changes state and calls our EndOfStream + +HRESULT CBaseRenderer::ResetEndOfStream() +{ + ResetEndOfStreamTimer(); + CAutoLock cRendererLock(&m_RendererLock); + + m_bEOS = FALSE; + m_bEOSDelivered = FALSE; + m_SignalTime = 0; + + return NOERROR; +} + + +// Kills any outstanding end of stream timer + +void CBaseRenderer::ResetEndOfStreamTimer() +{ + ASSERT(CritCheckOut(&m_RendererLock)); + if (m_EndOfStreamTimer) { + timeKillEvent(m_EndOfStreamTimer); + m_EndOfStreamTimer = 0; + } +} + + +// This is called when we start running so that we can schedule any pending +// image we have with the clock and display any timing information. If we +// don't have any sample but we have queued an EOS flag then we send it. If +// we do have a sample then we wait until that has been rendered before we +// signal the filter graph otherwise we may change state before it's done + +HRESULT CBaseRenderer::StartStreaming() +{ + CAutoLock cRendererLock(&m_RendererLock); + if (m_bStreaming == TRUE) { + return NOERROR; + } + + // Reset the streaming times ready for running + + m_bStreaming = TRUE; + + timeBeginPeriod(1); + OnStartStreaming(); + + // There should be no outstanding advise + ASSERT(WAIT_TIMEOUT == WaitForSingleObject((HANDLE)m_RenderEvent,0)); + ASSERT(CancelNotification() == S_FALSE); + + // If we have an EOS and no data then deliver it now + + if (m_pMediaSample == NULL) { + return SendEndOfStream(); + } + + // Have the data rendered + + ASSERT(m_pMediaSample); + if (!ScheduleSample(m_pMediaSample)) + m_RenderEvent.Set(); + + return NOERROR; +} + + +// This is called when we stop streaming so that we can set our internal flag +// indicating we are not now to schedule any more samples arriving. The state +// change methods in the filter implementation take care of cancelling any +// clock advise link we have set up and clearing any pending sample we have + +HRESULT CBaseRenderer::StopStreaming() +{ + CAutoLock cRendererLock(&m_RendererLock); + m_bEOSDelivered = FALSE; + + if (m_bStreaming == TRUE) { + m_bStreaming = FALSE; + OnStopStreaming(); + timeEndPeriod(1); + } + return NOERROR; +} + + +// We have a boolean flag that is reset when we have signalled EC_REPAINT to +// the filter graph. We set this when we receive an image so that should any +// conditions arise again we can send another one. By having a flag we ensure +// we don't flood the filter graph with redundant calls. We do not set the +// event when we receive an EndOfStream call since there is no point in us +// sending further EC_REPAINTs. In particular the AutoShowWindow method and +// the DirectDraw object use this method to control the window repainting + +void CBaseRenderer::SetRepaintStatus(BOOL bRepaint) +{ + CAutoLock cSampleLock(&m_RendererLock); + m_bRepaintStatus = bRepaint; +} + + +// Pass the window handle to the upstream filter + +void CBaseRenderer::SendNotifyWindow(IPin *pPin,HWND hwnd) +{ + IMediaEventSink *pSink; + + // Does the pin support IMediaEventSink + HRESULT hr = pPin->QueryInterface(IID_IMediaEventSink,(void **)&pSink); + if (SUCCEEDED(hr)) { + pSink->Notify(EC_NOTIFY_WINDOW,LONG_PTR(hwnd),0); + pSink->Release(); + } + NotifyEvent(EC_NOTIFY_WINDOW,LONG_PTR(hwnd),0); +} + + +// Signal an EC_REPAINT to the filter graph. This can be used to have data +// sent to us. For example when a video window is first displayed it may +// not have an image to display, at which point it signals EC_REPAINT. The +// filtergraph will either pause the graph if stopped or if already paused +// it will call put_CurrentPosition of the current position. Setting the +// current position to itself has the stream flushed and the image resent + +#define RLOG(_x_) DbgLog((LOG_TRACE,1,TEXT(_x_))); + +void CBaseRenderer::SendRepaint() +{ + CAutoLock cSampleLock(&m_RendererLock); + ASSERT(m_pInputPin); + + // We should not send repaint notifications when... + // - An end of stream has been notified + // - Our input pin is being flushed + // - The input pin is not connected + // - We have aborted a video playback + // - There is a repaint already sent + + if (m_bAbort == FALSE) { + if (m_pInputPin->IsConnected() == TRUE) { + if (m_pInputPin->IsFlushing() == FALSE) { + if (IsEndOfStream() == FALSE) { + if (m_bRepaintStatus == TRUE) { + IPin *pPin = (IPin *) m_pInputPin; + NotifyEvent(EC_REPAINT,(LONG_PTR) pPin,0); + SetRepaintStatus(FALSE); + RLOG("Sending repaint"); + } + } + } + } + } +} + + +// When a video window detects a display change (WM_DISPLAYCHANGE message) it +// can send an EC_DISPLAY_CHANGED event code along with the renderer pin. The +// filtergraph will stop everyone and reconnect our input pin. As we're then +// reconnected we can accept the media type that matches the new display mode +// since we may no longer be able to draw the current image type efficiently + +BOOL CBaseRenderer::OnDisplayChange() +{ + // Ignore if we are not connected yet + + CAutoLock cSampleLock(&m_RendererLock); + if (m_pInputPin->IsConnected() == FALSE) { + return FALSE; + } + + RLOG("Notification of EC_DISPLAY_CHANGE"); + + // Pass our input pin as parameter on the event + + IPin *pPin = (IPin *) m_pInputPin; + m_pInputPin->AddRef(); + NotifyEvent(EC_DISPLAY_CHANGED,(LONG_PTR) pPin,0); + SetAbortSignal(TRUE); + ClearPendingSample(); + m_pInputPin->Release(); + + return TRUE; +} + + +// Called just before we start drawing. +// Store the current time in m_trRenderStart to allow the rendering time to be +// logged. Log the time stamp of the sample and how late it is (neg is early) + +void CBaseRenderer::OnRenderStart(IMediaSample *pMediaSample) +{ +#ifdef PERF + REFERENCE_TIME trStart, trEnd; + pMediaSample->GetTime(&trStart, &trEnd); + + MSR_INTEGER(m_idBaseStamp, (int)trStart); // dump low order 32 bits + + m_pClock->GetTime(&m_trRenderStart); + MSR_INTEGER(0, (int)m_trRenderStart); + REFERENCE_TIME trStream; + trStream = m_trRenderStart-m_tStart; // convert reftime to stream time + MSR_INTEGER(0,(int)trStream); + + const int trLate = (int)(trStream - trStart); + MSR_INTEGER(m_idBaseAccuracy, trLate/10000); // dump in mSec +#endif + +} // OnRenderStart + + +// Called directly after drawing an image. +// calculate the time spent drawing and log it. + +void CBaseRenderer::OnRenderEnd(IMediaSample *pMediaSample) +{ +#ifdef PERF + REFERENCE_TIME trNow; + m_pClock->GetTime(&trNow); + MSR_INTEGER(0,(int)trNow); + int t = (int)((trNow - m_trRenderStart)/10000); // convert UNITS->msec + MSR_INTEGER(m_idBaseRenderTime, t); +#endif +} // OnRenderEnd + + + + +// Constructor must be passed the base renderer object + +CRendererInputPin::CRendererInputPin(CBaseRenderer *pRenderer, + HRESULT *phr, + LPCWSTR pPinName) : + CBaseInputPin(NAME("Renderer pin"), + pRenderer, + &pRenderer->m_InterfaceLock, + (HRESULT *) phr, + pPinName) +{ + m_pRenderer = pRenderer; + ASSERT(m_pRenderer); +} + + +// Signals end of data stream on the input pin + +STDMETHODIMP CRendererInputPin::EndOfStream() +{ + CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock); + CAutoLock cSampleLock(&m_pRenderer->m_RendererLock); + + // Make sure we're streaming ok + + HRESULT hr = CheckStreaming(); + if (hr != NOERROR) { + return hr; + } + + // Pass it onto the renderer + + hr = m_pRenderer->EndOfStream(); + if (SUCCEEDED(hr)) { + hr = CBaseInputPin::EndOfStream(); + } + return hr; +} + + +// Signals start of flushing on the input pin - we do the final reset end of +// stream with the renderer lock unlocked but with the interface lock locked +// We must do this because we call timeKillEvent, our timer callback method +// has to take the renderer lock to serialise our state. Therefore holding a +// renderer lock when calling timeKillEvent could cause a deadlock condition + +STDMETHODIMP CRendererInputPin::BeginFlush() +{ + CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock); + { + CAutoLock cSampleLock(&m_pRenderer->m_RendererLock); + CBaseInputPin::BeginFlush(); + m_pRenderer->BeginFlush(); + } + return m_pRenderer->ResetEndOfStream(); +} + + +// Signals end of flushing on the input pin + +STDMETHODIMP CRendererInputPin::EndFlush() +{ + CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock); + CAutoLock cSampleLock(&m_pRenderer->m_RendererLock); + + HRESULT hr = m_pRenderer->EndFlush(); + if (SUCCEEDED(hr)) { + hr = CBaseInputPin::EndFlush(); + } + return hr; +} + + +// Pass the sample straight through to the renderer object + +STDMETHODIMP CRendererInputPin::Receive(IMediaSample *pSample) +{ + HRESULT hr = m_pRenderer->Receive(pSample); + if (FAILED(hr)) { + + // A deadlock could occur if the caller holds the renderer lock and + // attempts to acquire the interface lock. + ASSERT(CritCheckOut(&m_pRenderer->m_RendererLock)); + + { + // The interface lock must be held when the filter is calling + // IsStopped() or IsFlushing(). The interface lock must also + // be held because the function uses m_bRunTimeError. + CAutoLock cRendererLock(&m_pRenderer->m_InterfaceLock); + + // We do not report errors which occur while the filter is stopping, + // flushing or if the m_bAbort flag is set . Errors are expected to + // occur during these operations and the streaming thread correctly + // handles the errors. + if (!IsStopped() && !IsFlushing() && !m_pRenderer->m_bAbort && !m_bRunTimeError) { + + // EC_ERRORABORT's first parameter is the error which caused + // the event and its' last parameter is 0. See the Direct + // Show SDK documentation for more information. + m_pRenderer->NotifyEvent(EC_ERRORABORT,hr,0); + + { + CAutoLock alRendererLock(&m_pRenderer->m_RendererLock); + if (m_pRenderer->IsStreaming() && !m_pRenderer->IsEndOfStreamDelivered()) { + m_pRenderer->NotifyEndOfStream(); + } + } + + m_bRunTimeError = TRUE; + } + } + } + + return hr; +} + + +// Called when the input pin is disconnected + +HRESULT CRendererInputPin::BreakConnect() +{ + HRESULT hr = m_pRenderer->BreakConnect(); + if (FAILED(hr)) { + return hr; + } + return CBaseInputPin::BreakConnect(); +} + + +// Called when the input pin is connected + +HRESULT CRendererInputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = m_pRenderer->CompleteConnect(pReceivePin); + if (FAILED(hr)) { + return hr; + } + return CBaseInputPin::CompleteConnect(pReceivePin); +} + + +// Give the pin id of our one and only pin + +STDMETHODIMP CRendererInputPin::QueryId(LPWSTR *Id) +{ + CheckPointer(Id,E_POINTER); + + const size_t len = 4; + *Id = (LPWSTR)CoTaskMemAlloc(len * sizeof(WCHAR)); + if (*Id == NULL) { + return E_OUTOFMEMORY; + } + (void)StringCchCopyW(*Id, len, L"In"); + return NOERROR; +} + + +// Will the filter accept this media type + +HRESULT CRendererInputPin::CheckMediaType(const CMediaType *pmt) +{ + return m_pRenderer->CheckMediaType(pmt); +} + + +// Called when we go paused or running + +HRESULT CRendererInputPin::Active() +{ + return m_pRenderer->Active(); +} + + +// Called when we go into a stopped state + +HRESULT CRendererInputPin::Inactive() +{ + // The caller must hold the interface lock because + // this function uses m_bRunTimeError. + ASSERT(CritCheckIn(&m_pRenderer->m_InterfaceLock)); + + m_bRunTimeError = FALSE; + + return m_pRenderer->Inactive(); +} + + +// Tell derived classes about the media type agreed + +HRESULT CRendererInputPin::SetMediaType(const CMediaType *pmt) +{ + HRESULT hr = CBaseInputPin::SetMediaType(pmt); + if (FAILED(hr)) { + return hr; + } + return m_pRenderer->SetMediaType(pmt); +} + + +// We do not keep an event object to use when setting up a timer link with +// the clock but are given a pointer to one by the owning object through the +// SetNotificationObject method - this must be initialised before starting +// We can override the default quality management process to have it always +// draw late frames, this is currently done by having the following registry +// key (actually an INI key) called DrawLateFrames set to 1 (default is 0) + +const TCHAR AMQUALITY[] = TEXT("ActiveMovie"); +const TCHAR DRAWLATEFRAMES[] = TEXT("DrawLateFrames"); + +CBaseVideoRenderer::CBaseVideoRenderer( + REFCLSID RenderClass, // CLSID for this renderer + TCHAR *pName, // Debug ONLY description + LPUNKNOWN pUnk, // Aggregated owner object + HRESULT *phr) : // General OLE return code + + CBaseRenderer(RenderClass,pName,pUnk,phr), + m_cFramesDropped(0), + m_cFramesDrawn(0), + m_bSupplierHandlingQuality(FALSE) +{ + ResetStreamingTimes(); + +#ifdef PERF + m_idTimeStamp = MSR_REGISTER(TEXT("Frame time stamp")); + m_idEarliness = MSR_REGISTER(TEXT("Earliness fudge")); + m_idTarget = MSR_REGISTER(TEXT("Target (mSec)")); + m_idSchLateTime = MSR_REGISTER(TEXT("mSec late when scheduled")); + m_idDecision = MSR_REGISTER(TEXT("Scheduler decision code")); + m_idQualityRate = MSR_REGISTER(TEXT("Quality rate sent")); + m_idQualityTime = MSR_REGISTER(TEXT("Quality time sent")); + m_idWaitReal = MSR_REGISTER(TEXT("Render wait")); + // m_idWait = MSR_REGISTER(TEXT("wait time recorded (msec)")); + m_idFrameAccuracy = MSR_REGISTER(TEXT("Frame accuracy (msecs)")); + m_bDrawLateFrames = GetProfileInt(AMQUALITY, DRAWLATEFRAMES, FALSE); + //m_idSendQuality = MSR_REGISTER(TEXT("Processing Quality message")); + + m_idRenderAvg = MSR_REGISTER(TEXT("Render draw time Avg")); + m_idFrameAvg = MSR_REGISTER(TEXT("FrameAvg")); + m_idWaitAvg = MSR_REGISTER(TEXT("WaitAvg")); + m_idDuration = MSR_REGISTER(TEXT("Duration")); + m_idThrottle = MSR_REGISTER(TEXT("Audio-video throttle wait")); + // m_idDebug = MSR_REGISTER(TEXT("Debug stuff")); +#endif // PERF +} // Constructor + + +// Destructor is just a placeholder + +CBaseVideoRenderer::~CBaseVideoRenderer() +{ + ASSERT(m_dwAdvise == 0); +} + + +// The timing functions in this class are called by the window object and by +// the renderer's allocator. +// The windows object calls timing functions as it receives media sample +// images for drawing using GDI. +// The allocator calls timing functions when it starts passing DCI/DirectDraw +// surfaces which are not rendered in the same way; The decompressor writes +// directly to the surface with no separate rendering, so those code paths +// call direct into us. Since we only ever hand out DCI/DirectDraw surfaces +// when we have allocated one and only one image we know there cannot be any +// conflict between the two. +// +// We use timeGetTime to return the timing counts we use (since it's relative +// performance we are interested in rather than absolute compared to a clock) +// The window object sets the accuracy of the system clock (normally 1ms) by +// calling timeBeginPeriod/timeEndPeriod when it changes streaming states + + +// Reset all times controlling streaming. +// Set them so that +// 1. Frames will not initially be dropped +// 2. The first frame will definitely be drawn (achieved by saying that there +// has not ben a frame drawn for a long time). + +HRESULT CBaseVideoRenderer::ResetStreamingTimes() +{ + m_trLastDraw = -1000; // set up as first frame since ages (1 sec) ago + m_tStreamingStart = timeGetTime(); + m_trRenderAvg = 0; + m_trFrameAvg = -1; // -1000 fps == "unset" + m_trDuration = 0; // 0 - strange value + m_trRenderLast = 0; + m_trWaitAvg = 0; + m_tRenderStart = 0; + m_cFramesDrawn = 0; + m_cFramesDropped = 0; + m_iTotAcc = 0; + m_iSumSqAcc = 0; + m_iSumSqFrameTime = 0; + m_trFrame = 0; // hygiene - not really needed + m_trLate = 0; // hygiene - not really needed + m_iSumFrameTime = 0; + m_nNormal = 0; + m_trEarliness = 0; + m_trTarget = -300000; // 30mSec early + m_trThrottle = 0; + m_trRememberStampForPerf = 0; + +#ifdef PERF + m_trRememberFrameForPerf = 0; +#endif + + return NOERROR; +} // ResetStreamingTimes + + +// Reset all times controlling streaming. Note that we're now streaming. We +// don't need to set the rendering event to have the source filter released +// as it is done during the Run processing. When we are run we immediately +// release the source filter thread and draw any image waiting (that image +// may already have been drawn once as a poster frame while we were paused) + +HRESULT CBaseVideoRenderer::OnStartStreaming() +{ + ResetStreamingTimes(); + return NOERROR; +} // OnStartStreaming + + +// Called at end of streaming. Fixes times for property page report + +HRESULT CBaseVideoRenderer::OnStopStreaming() +{ + m_tStreamingStart = timeGetTime()-m_tStreamingStart; + return NOERROR; +} // OnStopStreaming + + +// Called when we start waiting for a rendering event. +// Used to update times spent waiting and not waiting. + +void CBaseVideoRenderer::OnWaitStart() +{ + MSR_START(m_idWaitReal); +} // OnWaitStart + + +// Called when we are awoken from the wait in the window OR by our allocator +// when it is hanging around until the next sample is due for rendering on a +// DCI/DirectDraw surface. We add the wait time into our rolling average. +// We grab the interface lock so that we're serialised with the application +// thread going through the run code - which in due course ends up calling +// ResetStreaming times - possibly as we run through this section of code + +void CBaseVideoRenderer::OnWaitEnd() +{ +#ifdef PERF + MSR_STOP(m_idWaitReal); + // for a perf build we want to know just exactly how late we REALLY are. + // even if this means that we have to look at the clock again. + + REFERENCE_TIME trRealStream; // the real time now expressed as stream time. +#if 0 + m_pClock->GetTime(&trRealStream); // Calling clock here causes W95 deadlock! +#else + // We will be discarding overflows like mad here! + // This is wrong really because timeGetTime() can wrap but it's + // only for PERF + REFERENCE_TIME tr = timeGetTime()*10000; + trRealStream = tr + m_llTimeOffset; +#endif + trRealStream -= m_tStart; // convert to stream time (this is a reftime) + + if (m_trRememberStampForPerf==0) { + // This is probably the poster frame at the start, and it is not scheduled + // in the usual way at all. Just count it. The rememberstamp gets set + // in ShouldDrawSampleNow, so this does invalid frame recording until we + // actually start playing. + PreparePerformanceData(0, 0); + } else { + int trLate = (int)(trRealStream - m_trRememberStampForPerf); + int trFrame = (int)(tr - m_trRememberFrameForPerf); + PreparePerformanceData(trLate, trFrame); + } + m_trRememberFrameForPerf = tr; +#endif //PERF +} // OnWaitEnd + + +// Put data on one side that describes the lateness of the current frame. +// We don't yet know whether it will actually be drawn. In direct draw mode, +// this decision is up to the filter upstream, and it could change its mind. +// The rules say that if it did draw it must call Receive(). One way or +// another we eventually get into either OnRenderStart or OnDirectRender and +// these both call RecordFrameLateness to update the statistics. + +void CBaseVideoRenderer::PreparePerformanceData(int trLate, int trFrame) +{ + m_trLate = trLate; + m_trFrame = trFrame; +} // PreparePerformanceData + + +// update the statistics: +// m_iTotAcc, m_iSumSqAcc, m_iSumSqFrameTime, m_iSumFrameTime, m_cFramesDrawn +// Note that because the properties page reports using these variables, +// 1. We need to be inside a critical section +// 2. They must all be updated together. Updating the sums here and the count +// elsewhere can result in imaginary jitter (i.e. attempts to find square roots +// of negative numbers) in the property page code. + +void CBaseVideoRenderer::RecordFrameLateness(int trLate, int trFrame) +{ + // Record how timely we are. + int tLate = trLate/10000; + + // Best estimate of moment of appearing on the screen is average of + // start and end draw times. Here we have only the end time. This may + // tend to show us as spuriously late by up to 1/2 frame rate achieved. + // Decoder probably monitors draw time. We don't bother. + MSR_INTEGER( m_idFrameAccuracy, tLate ); + + // This is a kludge - we can get frames that are very late + // especially (at start-up) and they invalidate the statistics. + // So ignore things that are more than 1 sec off. + if (tLate>1000 || tLate<-1000) { + if (m_cFramesDrawn<=1) { + tLate = 0; + } else if (tLate>0) { + tLate = 1000; + } else { + tLate = -1000; + } + } + // The very first frame often has a invalid time, so don't + // count it into the statistics. (???) + if (m_cFramesDrawn>1) { + m_iTotAcc += tLate; + m_iSumSqAcc += (tLate*tLate); + } + + // calculate inter-frame time. Doesn't make sense for first frame + // second frame suffers from invalid first frame stamp. + if (m_cFramesDrawn>2) { + int tFrame = trFrame/10000; // convert to mSec else it overflows + + // This is a kludge. It can overflow anyway (a pause can cause + // a very long inter-frame time) and it overflows at 2**31/10**7 + // or about 215 seconds i.e. 3min 35sec + if (tFrame>1000||tFrame<0) tFrame = 1000; + m_iSumSqFrameTime += tFrame*tFrame; + ASSERT(m_iSumSqFrameTime>=0); + m_iSumFrameTime += tFrame; + } + ++m_cFramesDrawn; + +} // RecordFrameLateness + + +void CBaseVideoRenderer::ThrottleWait() +{ + if (m_trThrottle>0) { + int iThrottle = m_trThrottle/10000; // convert to mSec + MSR_INTEGER( m_idThrottle, iThrottle); + DbgLog((LOG_TRACE, 0, TEXT("Throttle %d ms"), iThrottle)); + Sleep(iThrottle); + } else { + Sleep(0); + } +} // ThrottleWait + + +// Whenever a frame is rendered it goes though either OnRenderStart +// or OnDirectRender. Data that are generated during ShouldDrawSample +// are added to the statistics by calling RecordFrameLateness from both +// these two places. + +// Called in place of OnRenderStart..OnRenderEnd +// When a DirectDraw image is drawn +void CBaseVideoRenderer::OnDirectRender(IMediaSample *pMediaSample) +{ + m_trRenderAvg = 0; + m_trRenderLast = 5000000; // If we mode switch, we do NOT want this + // to inhibit the new average getting going! + // so we set it to half a second + // MSR_INTEGER(m_idRenderAvg, m_trRenderAvg/10000); + RecordFrameLateness(m_trLate, m_trFrame); + ThrottleWait(); +} // OnDirectRender + + +// Called just before we start drawing. All we do is to get the current clock +// time (from the system) and return. We have to store the start render time +// in a member variable because it isn't used until we complete the drawing +// The rest is just performance logging. + +void CBaseVideoRenderer::OnRenderStart(IMediaSample *pMediaSample) +{ + RecordFrameLateness(m_trLate, m_trFrame); + m_tRenderStart = timeGetTime(); +} // OnRenderStart + + +// Called directly after drawing an image. We calculate the time spent in the +// drawing code and if this doesn't appear to have any odd looking spikes in +// it then we add it to the current average draw time. Measurement spikes may +// occur if the drawing thread is interrupted and switched to somewhere else. + +void CBaseVideoRenderer::OnRenderEnd(IMediaSample *pMediaSample) +{ + // The renderer time can vary erratically if we are interrupted so we do + // some smoothing to help get more sensible figures out but even that is + // not enough as figures can go 9,10,9,9,83,9 and we must disregard 83 + + int tr = (timeGetTime() - m_tRenderStart)*10000; // convert mSec->UNITS + if (tr < m_trRenderAvg*2 || tr < 2 * m_trRenderLast) { + // DO_MOVING_AVG(m_trRenderAvg, tr); + m_trRenderAvg = (tr + (AVGPERIOD-1)*m_trRenderAvg)/AVGPERIOD; + } + m_trRenderLast = tr; + ThrottleWait(); +} // OnRenderEnd + + +STDMETHODIMP CBaseVideoRenderer::SetSink( IQualityControl * piqc) +{ + + m_pQSink = piqc; + + return NOERROR; +} // SetSink + + +STDMETHODIMP CBaseVideoRenderer::Notify( IBaseFilter * pSelf, Quality q) +{ + // NOTE: We are NOT getting any locks here. We could be called + // asynchronously and possibly even on a time critical thread of + // someone else's - so we do the minumum. We only set one state + // variable (an integer) and if that happens to be in the middle + // of another thread reading it they will just get either the new + // or the old value. Locking would achieve no more than this. + + // It might be nice to check that we are being called from m_pGraph, but + // it turns out to be a millisecond or so per throw! + + // This is heuristics, these numbers are aimed at being "what works" + // rather than anything based on some theory. + // We use a hyperbola because it's easy to calculate and it includes + // a panic button asymptote (which we push off just to the left) + // The throttling fits the following table (roughly) + // Proportion Throttle (msec) + // >=1000 0 + // 900 3 + // 800 7 + // 700 11 + // 600 17 + // 500 25 + // 400 35 + // 300 50 + // 200 72 + // 125 100 + // 100 112 + // 50 146 + // 0 200 + + // (some evidence that we could go for a sharper kink - e.g. no throttling + // until below the 750 mark - might give fractionally more frames on a + // P60-ish machine). The easy way to get these coefficients is to use + // Renbase.xls follow the instructions therein using excel solver. + + if (q.Proportion>=1000) { m_trThrottle = 0; } + else { + // The DWORD is to make quite sure I get unsigned arithmetic + // as the constant is between 2**31 and 2**32 + m_trThrottle = -330000 + (388880000/(q.Proportion+167)); + } + return NOERROR; +} // Notify + + +// Send a message to indicate what our supplier should do about quality. +// Theory: +// What a supplier wants to know is "is the frame I'm working on NOW +// going to be late?". +// F1 is the frame at the supplier (as above) +// Tf1 is the due time for F1 +// T1 is the time at that point (NOW!) +// Tr1 is the time that f1 WILL actually be rendered +// L1 is the latency of the graph for frame F1 = Tr1-T1 +// D1 (for delay) is how late F1 will be beyond its due time i.e. +// D1 = (Tr1-Tf1) which is what the supplier really wants to know. +// Unfortunately Tr1 is in the future and is unknown, so is L1 +// +// We could estimate L1 by its value for a previous frame, +// L0 = Tr0-T0 and work off +// D1' = ((T1+L0)-Tf1) = (T1 + (Tr0-T0) -Tf1) +// Rearranging terms: +// D1' = (T1-T0) + (Tr0-Tf1) +// adding (Tf0-Tf0) and rearranging again: +// = (T1-T0) + (Tr0-Tf0) + (Tf0-Tf1) +// = (T1-T0) - (Tf1-Tf0) + (Tr0-Tf0) +// But (Tr0-Tf0) is just D0 - how late frame zero was, and this is the +// Late field in the quality message that we send. +// The other two terms just state what correction should be applied before +// using the lateness of F0 to predict the lateness of F1. +// (T1-T0) says how much time has actually passed (we have lost this much) +// (Tf1-Tf0) says how much time should have passed if we were keeping pace +// (we have gained this much). +// +// Suppliers should therefore work off: +// Quality.Late + (T1-T0) - (Tf1-Tf0) +// and see if this is "acceptably late" or even early (i.e. negative). +// They get T1 and T0 by polling the clock, they get Tf1 and Tf0 from +// the time stamps in the frames. They get Quality.Late from us. +// + +HRESULT CBaseVideoRenderer::SendQuality(REFERENCE_TIME trLate, + REFERENCE_TIME trRealStream) +{ + Quality q; + HRESULT hr; + + // If we are the main user of time, then report this as Flood/Dry. + // If our suppliers are, then report it as Famine/Glut. + // + // We need to take action, but avoid hunting. Hunting is caused by + // 1. Taking too much action too soon and overshooting + // 2. Taking too long to react (so averaging can CAUSE hunting). + // + // The reason why we use trLate as well as Wait is to reduce hunting; + // if the wait time is coming down and about to go into the red, we do + // NOT want to rely on some average which is only telling is that it used + // to be OK once. + + q.TimeStamp = (REFERENCE_TIME)trRealStream; + + if (m_trFrameAvg<0) { + q.Type = Famine; // guess + } + // Is the greater part of the time taken bltting or something else + else if (m_trFrameAvg > 2*m_trRenderAvg) { + q.Type = Famine; // mainly other + } else { + q.Type = Flood; // mainly bltting + } + + q.Proportion = 1000; // default + + if (m_trFrameAvg<0) { + // leave it alone - we don't know enough + } + else if ( trLate> 0 ) { + // try to catch up over the next second + // We could be Really, REALLY late, but rendering all the frames + // anyway, just because it's so cheap. + + q.Proportion = 1000 - (int)((trLate)/(UNITS/1000)); + if (q.Proportion<500) { + q.Proportion = 500; // don't go daft. (could've been negative!) + } else { + } + + } else if ( m_trWaitAvg>20000 + && trLate<-20000 + ){ + // Go cautiously faster - aim at 2mSec wait. + if (m_trWaitAvg>=m_trFrameAvg) { + // This can happen because of some fudges. + // The waitAvg is how long we originally planned to wait + // The frameAvg is more honest. + // It means that we are spending a LOT of time waiting + q.Proportion = 2000; // double. + } else { + if (m_trFrameAvg+20000 > m_trWaitAvg) { + q.Proportion + = 1000 * (m_trFrameAvg / (m_trFrameAvg + 20000 - m_trWaitAvg)); + } else { + // We're apparently spending more than the whole frame time waiting. + // Assume that the averages are slightly out of kilter, but that we + // are indeed doing a lot of waiting. (This leg probably never + // happens, but the code avoids any potential divide by zero). + q.Proportion = 2000; + } + } + + if (q.Proportion>2000) { + q.Proportion = 2000; // don't go crazy. + } + } + + // Tell the supplier how late frames are when they get rendered + // That's how late we are now. + // If we are in directdraw mode then the guy upstream can see the drawing + // times and we'll just report on the start time. He can figure out any + // offset to apply. If we are in DIB Section mode then we will apply an + // extra offset which is half of our drawing time. This is usually small + // but can sometimes be the dominant effect. For this we will use the + // average drawing time rather than the last frame. If the last frame took + // a long time to draw and made us late, that's already in the lateness + // figure. We should not add it in again unless we expect the next frame + // to be the same. We don't, we expect the average to be a better shot. + // In direct draw mode the RenderAvg will be zero. + + q.Late = trLate + m_trRenderAvg/2; + + // log what we're doing + MSR_INTEGER(m_idQualityRate, q.Proportion); + MSR_INTEGER( m_idQualityTime, (int)q.Late / 10000 ); + + // A specific sink interface may be set through IPin + + if (m_pQSink==NULL) { + // Get our input pin's peer. We send quality management messages + // to any nominated receiver of these things (set in the IPin + // interface), or else to our source filter. + + IQualityControl *pQC = NULL; + IPin *pOutputPin = m_pInputPin->GetConnected(); + ASSERT(pOutputPin != NULL); + + // And get an AddRef'd quality control interface + + hr = pOutputPin->QueryInterface(IID_IQualityControl,(void**) &pQC); + if (SUCCEEDED(hr)) { + m_pQSink = pQC; + } + } + if (m_pQSink) { + return m_pQSink->Notify(this,q); + } + + return S_FALSE; + +} // SendQuality + + +// We are called with a valid IMediaSample image to decide whether this is to +// be drawn or not. There must be a reference clock in operation. +// Return S_OK if it is to be drawn Now (as soon as possible) +// Return S_FALSE if it is to be drawn when it's due +// Return an error if we want to drop it +// m_nNormal=-1 indicates that we dropped the previous frame and so this +// one should be drawn early. Respect it and update it. +// Use current stream time plus a number of heuristics (detailed below) +// to make the decision + +HRESULT CBaseVideoRenderer::ShouldDrawSampleNow(IMediaSample *pMediaSample, + REFERENCE_TIME *ptrStart, + REFERENCE_TIME *ptrEnd) +{ + + // Don't call us unless there's a clock interface to synchronise with + ASSERT(m_pClock); + + MSR_INTEGER(m_idTimeStamp, (int)((*ptrStart)>>32)); // high order 32 bits + MSR_INTEGER(m_idTimeStamp, (int)(*ptrStart)); // low order 32 bits + + // We lose a bit of time depending on the monitor type waiting for the next + // screen refresh. On average this might be about 8mSec - so it will be + // later than we think when the picture appears. To compensate a bit + // we bias the media samples by -8mSec i.e. 80000 UNITs. + // We don't ever make a stream time negative (call it paranoia) + if (*ptrStart>=80000) { + *ptrStart -= 80000; + *ptrEnd -= 80000; // bias stop to to retain valid frame duration + } + + // Cache the time stamp now. We will want to compare what we did with what + // we started with (after making the monitor allowance). + m_trRememberStampForPerf = *ptrStart; + + // Get reference times (current and late) + REFERENCE_TIME trRealStream; // the real time now expressed as stream time. + m_pClock->GetTime(&trRealStream); +#ifdef PERF + // While the reference clock is expensive: + // Remember the offset from timeGetTime and use that. + // This overflows all over the place, but when we subtract to get + // differences the overflows all cancel out. + m_llTimeOffset = trRealStream-timeGetTime()*10000; +#endif + trRealStream -= m_tStart; // convert to stream time (this is a reftime) + + // We have to wory about two versions of "lateness". The truth, which we + // try to work out here and the one measured against m_trTarget which + // includes long term feedback. We report statistics against the truth + // but for operational decisions we work to the target. + // We use TimeDiff to make sure we get an integer because we + // may actually be late (or more likely early if there is a big time + // gap) by a very long time. + const int trTrueLate = TimeDiff(trRealStream - *ptrStart); + const int trLate = trTrueLate; + + MSR_INTEGER(m_idSchLateTime, trTrueLate/10000); + + // Send quality control messages upstream, measured against target + HRESULT hr = SendQuality(trLate, trRealStream); + // Note: the filter upstream is allowed to this FAIL meaning "you do it". + m_bSupplierHandlingQuality = (hr==S_OK); + + // Decision time! Do we drop, draw when ready or draw immediately? + + const int trDuration = (int)(*ptrEnd - *ptrStart); + { + // We need to see if the frame rate of the file has just changed. + // This would make comparing our previous frame rate with the current + // frame rate inefficent. Hang on a moment though. I've seen files + // where the frames vary between 33 and 34 mSec so as to average + // 30fps. A minor variation like that won't hurt us. + int t = m_trDuration/32; + if ( trDuration > m_trDuration+t + || trDuration < m_trDuration-t + ) { + // There's a major variation. Reset the average frame rate to + // exactly the current rate to disable decision 9002 for this frame, + // and remember the new rate. + m_trFrameAvg = trDuration; + m_trDuration = trDuration; + } + } + + MSR_INTEGER(m_idEarliness, m_trEarliness/10000); + MSR_INTEGER(m_idRenderAvg, m_trRenderAvg/10000); + MSR_INTEGER(m_idFrameAvg, m_trFrameAvg/10000); + MSR_INTEGER(m_idWaitAvg, m_trWaitAvg/10000); + MSR_INTEGER(m_idDuration, trDuration/10000); + +#ifdef PERF + if (S_OK==pMediaSample->IsDiscontinuity()) { + MSR_INTEGER(m_idDecision, 9000); + } +#endif + + // Control the graceful slide back from slow to fast machine mode. + // After a frame drop accept an early frame and set the earliness to here + // If this frame is already later than the earliness then slide it to here + // otherwise do the standard slide (reduce by about 12% per frame). + // Note: earliness is normally NEGATIVE + BOOL bJustDroppedFrame + = ( m_bSupplierHandlingQuality + // Can't use the pin sample properties because we might + // not be in Receive when we call this + && (S_OK == pMediaSample->IsDiscontinuity()) // he just dropped one + ) + || (m_nNormal==-1); // we just dropped one + + + // Set m_trEarliness (slide back from slow to fast machine mode) + if (trLate>0) { + m_trEarliness = 0; // we are no longer in fast machine mode at all! + } else if ( (trLate>=m_trEarliness) || bJustDroppedFrame) { + m_trEarliness = trLate; // Things have slipped of their own accord + } else { + m_trEarliness = m_trEarliness - m_trEarliness/8; // graceful slide + } + + // prepare the new wait average - but don't pollute the old one until + // we have finished with it. + int trWaitAvg; + { + // We never mix in a negative wait. This causes us to believe in fast machines + // slightly more. + int trL = trLate<0 ? -trLate : 0; + trWaitAvg = (trL + m_trWaitAvg*(AVGPERIOD-1))/AVGPERIOD; + } + + + int trFrame; + { + REFERENCE_TIME tr = trRealStream - m_trLastDraw; // Cd be large - 4 min pause! + if (tr>10000000) { + tr = 10000000; // 1 second - arbitrarily. + } + trFrame = int(tr); + } + + // We will DRAW this frame IF... + if ( + // ...the time we are spending drawing is a small fraction of the total + // observed inter-frame time so that dropping it won't help much. + (3*m_trRenderAvg <= m_trFrameAvg) + + // ...or our supplier is NOT handling things and the next frame would + // be less timely than this one or our supplier CLAIMS to be handling + // things, and is now less than a full FOUR frames late. + || ( m_bSupplierHandlingQuality + ? (trLate <= trDuration*4) + : (trLate+trLate < trDuration) + ) + + // ...or we are on average waiting for over eight milliseconds then + // this may be just a glitch. Draw it and we'll hope to catch up. + || (m_trWaitAvg > 80000) + + // ...or we haven't drawn an image for over a second. We will update + // the display, which stops the video looking hung. + // Do this regardless of how late this media sample is. + || ((trRealStream - m_trLastDraw) > UNITS) + + ) { + HRESULT Result; + + // We are going to play this frame. We may want to play it early. + // We will play it early if we think we are in slow machine mode. + // If we think we are NOT in slow machine mode, we will still play + // it early by m_trEarliness as this controls the graceful slide back. + // and in addition we aim at being m_trTarget late rather than "on time". + + BOOL bPlayASAP = FALSE; + + // we will play it AT ONCE (slow machine mode) if... + + // ...we are playing catch-up + if ( bJustDroppedFrame) { + bPlayASAP = TRUE; + MSR_INTEGER(m_idDecision, 9001); + } + + // ...or if we are running below the true frame rate + // exact comparisons are glitchy, for these measurements, + // so add an extra 5% or so + else if ( (m_trFrameAvg > trDuration + trDuration/16) + + // It's possible to get into a state where we are losing ground, but + // are a very long way ahead. To avoid this or recover from it + // we refuse to play early by more than 10 frames. + && (trLate > - trDuration*10) + ){ + bPlayASAP = TRUE; + MSR_INTEGER(m_idDecision, 9002); + } +#if 0 + // ...or if we have been late and are less than one frame early + else if ( (trLate + trDuration > 0) + && (m_trWaitAvg<=20000) + ) { + bPlayASAP = TRUE; + MSR_INTEGER(m_idDecision, 9003); + } +#endif + // We will NOT play it at once if we are grossly early. On very slow frame + // rate movies - e.g. clock.avi - it is not a good idea to leap ahead just + // because we got starved (for instance by the net) and dropped one frame + // some time or other. If we are more than 900mSec early, then wait. + if (trLate<-9000000) { + bPlayASAP = FALSE; + } + + if (bPlayASAP) { + + m_nNormal = 0; + MSR_INTEGER(m_idDecision, 0); + // When we are here, we are in slow-machine mode. trLate may well + // oscillate between negative and positive when the supplier is + // dropping frames to keep sync. We should not let that mislead + // us into thinking that we have as much as zero spare time! + // We just update with a zero wait. + m_trWaitAvg = (m_trWaitAvg*(AVGPERIOD-1))/AVGPERIOD; + + // Assume that we draw it immediately. Update inter-frame stats + m_trFrameAvg = (trFrame + m_trFrameAvg*(AVGPERIOD-1))/AVGPERIOD; +#ifndef PERF + // If this is NOT a perf build, then report what we know so far + // without looking at the clock any more. This assumes that we + // actually wait for exactly the time we hope to. It also reports + // how close we get to the manipulated time stamps that we now have + // rather than the ones we originally started with. It will + // therefore be a little optimistic. However it's fast. + PreparePerformanceData(trTrueLate, trFrame); +#endif + m_trLastDraw = trRealStream; + if (m_trEarliness > trLate) { + m_trEarliness = trLate; // if we are actually early, this is neg + } + Result = S_OK; // Draw it now + + } else { + ++m_nNormal; + // Set the average frame rate to EXACTLY the ideal rate. + // If we are exiting slow-machine mode then we will have caught up + // and be running ahead, so as we slide back to exact timing we will + // have a longer than usual gap at this point. If we record this + // real gap then we'll think that we're running slow and go back + // into slow-machine mode and vever get it straight. + m_trFrameAvg = trDuration; + MSR_INTEGER(m_idDecision, 1); + + // Play it early by m_trEarliness and by m_trTarget + + { + int trE = m_trEarliness; + if (trE < -m_trFrameAvg) { + trE = -m_trFrameAvg; + } + *ptrStart += trE; // N.B. earliness is negative + } + + int Delay = -trTrueLate; + Result = Delay<=0 ? S_OK : S_FALSE; // OK = draw now, FALSE = wait + + m_trWaitAvg = trWaitAvg; + + // Predict when it will actually be drawn and update frame stats + + if (Result==S_FALSE) { // We are going to wait + trFrame = TimeDiff(*ptrStart-m_trLastDraw); + m_trLastDraw = *ptrStart; + } else { + // trFrame is already = trRealStream-m_trLastDraw; + m_trLastDraw = trRealStream; + } +#ifndef PERF + int iAccuracy; + if (Delay>0) { + // Report lateness based on when we intend to play it + iAccuracy = TimeDiff(*ptrStart-m_trRememberStampForPerf); + } else { + // Report lateness based on playing it *now*. + iAccuracy = trTrueLate; // trRealStream-RememberStampForPerf; + } + PreparePerformanceData(iAccuracy, trFrame); +#endif + } + return Result; + } + + // We are going to drop this frame! + // Of course in DirectDraw mode the guy upstream may draw it anyway. + + // This will probably give a large negative wack to the wait avg. + m_trWaitAvg = trWaitAvg; + +#ifdef PERF + // Respect registry setting - debug only! + if (m_bDrawLateFrames) { + return S_OK; // draw it when it's ready + } // even though it's late. +#endif + + // We are going to drop this frame so draw the next one early + // n.b. if the supplier is doing direct draw then he may draw it anyway + // but he's doing something funny to arrive here in that case. + + MSR_INTEGER(m_idDecision, 2); + m_nNormal = -1; + return E_FAIL; // drop it + +} // ShouldDrawSampleNow + + +// NOTE we're called by both the window thread and the source filter thread +// so we have to be protected by a critical section (locked before called) +// Also, when the window thread gets signalled to render an image, it always +// does so regardless of how late it is. All the degradation is done when we +// are scheduling the next sample to be drawn. Hence when we start an advise +// link to draw a sample, that sample's time will always become the last one +// drawn - unless of course we stop streaming in which case we cancel links + +BOOL CBaseVideoRenderer::ScheduleSample(IMediaSample *pMediaSample) +{ + // We override ShouldDrawSampleNow to add quality management + + BOOL bDrawImage = CBaseRenderer::ScheduleSample(pMediaSample); + if (bDrawImage == FALSE) { + ++m_cFramesDropped; + return FALSE; + } + + // m_cFramesDrawn must NOT be updated here. It has to be updated + // in RecordFrameLateness at the same time as the other statistics. + return TRUE; +} + + +// Implementation of IQualProp interface needed to support the property page +// This is how the property page gets the data out of the scheduler. We are +// passed into the constructor the owning object in the COM sense, this will +// either be the video renderer or an external IUnknown if we're aggregated. +// We initialise our CUnknown base class with this interface pointer. Then +// all we have to do is to override NonDelegatingQueryInterface to expose +// our IQualProp interface. The AddRef and Release are handled automatically +// by the base class and will be passed on to the appropriate outer object + +STDMETHODIMP CBaseVideoRenderer::get_FramesDroppedInRenderer(int *pcFramesDropped) +{ + CheckPointer(pcFramesDropped,E_POINTER); + CAutoLock cVideoLock(&m_InterfaceLock); + *pcFramesDropped = m_cFramesDropped; + return NOERROR; +} // get_FramesDroppedInRenderer + + +// Set *pcFramesDrawn to the number of frames drawn since +// streaming started. + +STDMETHODIMP CBaseVideoRenderer::get_FramesDrawn( int *pcFramesDrawn) +{ + CheckPointer(pcFramesDrawn,E_POINTER); + CAutoLock cVideoLock(&m_InterfaceLock); + *pcFramesDrawn = m_cFramesDrawn; + return NOERROR; +} // get_FramesDrawn + + +// Set iAvgFrameRate to the frames per hundred secs since +// streaming started. 0 otherwise. + +STDMETHODIMP CBaseVideoRenderer::get_AvgFrameRate( int *piAvgFrameRate) +{ + CheckPointer(piAvgFrameRate,E_POINTER); + CAutoLock cVideoLock(&m_InterfaceLock); + + int t; + if (m_bStreaming) { + t = timeGetTime()-m_tStreamingStart; + } else { + t = m_tStreamingStart; + } + + if (t<=0) { + *piAvgFrameRate = 0; + ASSERT(m_cFramesDrawn == 0); + } else { + // i is frames per hundred seconds + *piAvgFrameRate = MulDiv(100000, m_cFramesDrawn, t); + } + return NOERROR; +} // get_AvgFrameRate + + +// Set *piAvg to the average sync offset since streaming started +// in mSec. The sync offset is the time in mSec between when the frame +// should have been drawn and when the frame was actually drawn. + +STDMETHODIMP CBaseVideoRenderer::get_AvgSyncOffset( int *piAvg) +{ + CheckPointer(piAvg,E_POINTER); + CAutoLock cVideoLock(&m_InterfaceLock); + + if (NULL==m_pClock) { + *piAvg = 0; + return NOERROR; + } + + // Note that we didn't gather the stats on the first frame + // so we use m_cFramesDrawn-1 here + if (m_cFramesDrawn<=1) { + *piAvg = 0; + } else { + *piAvg = (int)(m_iTotAcc / (m_cFramesDrawn-1)); + } + return NOERROR; +} // get_AvgSyncOffset + + +// To avoid dragging in the maths library - a cheap +// approximate integer square root. +// We do this by getting a starting guess which is between 1 +// and 2 times too large, followed by THREE iterations of +// Newton Raphson. (That will give accuracy to the nearest mSec +// for the range in question - roughly 0..1000) +// +// It would be faster to use a linear interpolation and ONE NR, but +// who cares. If anyone does - the best linear interpolation is +// to approximates sqrt(x) by +// y = x * (sqrt(2)-1) + 1 - 1/sqrt(2) + 1/(8*(sqrt(2)-1)) +// 0r y = x*0.41421 + 0.59467 +// This minimises the maximal error in the range in question. +// (error is about +0.008883 and then one NR will give error .0000something +// (Of course these are integers, so you can't just multiply by 0.41421 +// you'd have to do some sort of MulDiv). +// Anyone wanna check my maths? (This is only for a property display!) + +int isqrt(int x) +{ + int s = 1; + // Make s an initial guess for sqrt(x) + if (x > 0x40000000) { + s = 0x8000; // prevent any conceivable closed loop + } else { + while (s*s=0) s = (s*s+x)/(2*s); + if (s>=0) s = (s*s+x)/(2*s); + } + } + return s; +} + +// +// Do estimates for standard deviations for per-frame +// statistics +// +HRESULT CBaseVideoRenderer::GetStdDev( + int nSamples, + int *piResult, + LONGLONG llSumSq, + LONGLONG iTot +) +{ + CheckPointer(piResult,E_POINTER); + CAutoLock cVideoLock(&m_InterfaceLock); + + if (NULL==m_pClock) { + *piResult = 0; + return NOERROR; + } + + // If S is the Sum of the Squares of observations and + // T the Total (i.e. sum) of the observations and there were + // N observations, then an estimate of the standard deviation is + // sqrt( (S - T**2/N) / (N-1) ) + + if (nSamples<=1) { + *piResult = 0; + } else { + LONGLONG x; + // First frames have invalid stamps, so we get no stats for them + // So we need 2 frames to get 1 datum, so N is cFramesDrawn-1 + + // so we use m_cFramesDrawn-1 here + x = llSumSq - llMulDiv(iTot, iTot, nSamples, 0); + x = x / (nSamples-1); + ASSERT(x>=0); + *piResult = isqrt((LONG)x); + } + return NOERROR; +} + +// Set *piDev to the standard deviation in mSec of the sync offset +// of each frame since streaming started. + +STDMETHODIMP CBaseVideoRenderer::get_DevSyncOffset( int *piDev) +{ + // First frames have invalid stamps, so we get no stats for them + // So we need 2 frames to get 1 datum, so N is cFramesDrawn-1 + return GetStdDev(m_cFramesDrawn - 1, + piDev, + m_iSumSqAcc, + m_iTotAcc); +} // get_DevSyncOffset + + +// Set *piJitter to the standard deviation in mSec of the inter-frame time +// of frames since streaming started. + +STDMETHODIMP CBaseVideoRenderer::get_Jitter( int *piJitter) +{ + // First frames have invalid stamps, so we get no stats for them + // So second frame gives invalid inter-frame time + // So we need 3 frames to get 1 datum, so N is cFramesDrawn-2 + return GetStdDev(m_cFramesDrawn - 2, + piJitter, + m_iSumSqFrameTime, + m_iSumFrameTime); +} // get_Jitter + + +// Overidden to return our IQualProp interface + +STDMETHODIMP +CBaseVideoRenderer::NonDelegatingQueryInterface(REFIID riid,VOID **ppv) +{ + // We return IQualProp and delegate everything else + + if (riid == IID_IQualProp) { + return GetInterface( (IQualProp *)this, ppv); + } else if (riid == IID_IQualityControl) { + return GetInterface( (IQualityControl *)this, ppv); + } + return CBaseRenderer::NonDelegatingQueryInterface(riid,ppv); +} + + +// Override JoinFilterGraph so that, just before leaving +// the graph we can send an EC_WINDOW_DESTROYED event + +STDMETHODIMP +CBaseVideoRenderer::JoinFilterGraph(IFilterGraph *pGraph,LPCWSTR pName) +{ + // Since we send EC_ACTIVATE, we also need to ensure + // we send EC_WINDOW_DESTROYED or the resource manager may be + // holding us as a focus object + if (!pGraph && m_pGraph) { + + // We were in a graph and now we're not + // Do this properly in case we are aggregated + IBaseFilter* pFilter; + QueryInterface(IID_IBaseFilter,(void **) &pFilter); + NotifyEvent(EC_WINDOW_DESTROYED, (LPARAM) pFilter, 0); + pFilter->Release(); + } + return CBaseFilter::JoinFilterGraph(pGraph, pName); +} + + +// This removes a large number of level 4 warnings from the +// Microsoft compiler which in this case are not very useful +#pragma warning(disable: 4514) + diff --git a/plugins/GSdx/baseclasses/renbase.h b/plugins/GSdx/baseclasses/renbase.h new file mode 100644 index 0000000000..f30a3fad19 --- /dev/null +++ b/plugins/GSdx/baseclasses/renbase.h @@ -0,0 +1,478 @@ +//------------------------------------------------------------------------------ +// File: RenBase.h +// +// Desc: DirectShow base classes - defines a generic ActiveX base renderer +// class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __RENBASE__ +#define __RENBASE__ + +// Forward class declarations + +class CBaseRenderer; +class CBaseVideoRenderer; +class CRendererInputPin; + +// This is our input pin class that channels calls to the renderer + +class CRendererInputPin : public CBaseInputPin +{ +protected: + + CBaseRenderer *m_pRenderer; + +public: + + CRendererInputPin(CBaseRenderer *pRenderer, + HRESULT *phr, + LPCWSTR Name); + + // Overriden from the base pin classes + + HRESULT BreakConnect(); + HRESULT CompleteConnect(IPin *pReceivePin); + HRESULT SetMediaType(const CMediaType *pmt); + HRESULT CheckMediaType(const CMediaType *pmt); + HRESULT Active(); + HRESULT Inactive(); + + // Add rendering behaviour to interface functions + + STDMETHODIMP QueryId(LPWSTR *Id); + STDMETHODIMP EndOfStream(); + STDMETHODIMP BeginFlush(); + STDMETHODIMP EndFlush(); + STDMETHODIMP Receive(IMediaSample *pMediaSample); + + // Helper + IMemAllocator inline *Allocator() const + { + return m_pAllocator; + } +}; + +// Main renderer class that handles synchronisation and state changes + +class CBaseRenderer : public CBaseFilter +{ +protected: + + friend class CRendererInputPin; + + friend void CALLBACK EndOfStreamTimer(UINT uID, // Timer identifier + UINT uMsg, // Not currently used + DWORD_PTR dwUser, // User information + DWORD_PTR dw1, // Windows reserved + DWORD_PTR dw2); // Is also reserved + + CRendererPosPassThru *m_pPosition; // Media seeking pass by object + CAMEvent m_RenderEvent; // Used to signal timer events + CAMEvent m_ThreadSignal; // Signalled to release worker thread + CAMEvent m_evComplete; // Signalled when state complete + BOOL m_bAbort; // Stop us from rendering more data + BOOL m_bStreaming; // Are we currently streaming + DWORD_PTR m_dwAdvise; // Timer advise cookie + IMediaSample *m_pMediaSample; // Current image media sample + BOOL m_bEOS; // Any more samples in the stream + BOOL m_bEOSDelivered; // Have we delivered an EC_COMPLETE + CRendererInputPin *m_pInputPin; // Our renderer input pin object + CCritSec m_InterfaceLock; // Critical section for interfaces + CCritSec m_RendererLock; // Controls access to internals + IQualityControl * m_pQSink; // QualityControl sink + BOOL m_bRepaintStatus; // Can we signal an EC_REPAINT + // Avoid some deadlocks by tracking filter during stop + volatile BOOL m_bInReceive; // Inside Receive between PrepareReceive + // And actually processing the sample + REFERENCE_TIME m_SignalTime; // Time when we signal EC_COMPLETE + UINT m_EndOfStreamTimer; // Used to signal end of stream + CCritSec m_ObjectCreationLock; // This lock protects the creation and + // of m_pPosition and m_pInputPin. It + // ensures that two threads cannot create + // either object simultaneously. + +public: + + CBaseRenderer(REFCLSID RenderClass, // CLSID for this renderer + TCHAR *pName, // Debug ONLY description + LPUNKNOWN pUnk, // Aggregated owner object + HRESULT *phr); // General OLE return code + + ~CBaseRenderer(); + + // Overriden to say what interfaces we support and where + + virtual HRESULT GetMediaPositionInterface(REFIID riid,void **ppv); + STDMETHODIMP NonDelegatingQueryInterface(REFIID, void **); + + virtual HRESULT SourceThreadCanWait(BOOL bCanWait); + +#ifdef DEBUG + // Debug only dump of the renderer state + void DisplayRendererState(); +#endif + virtual HRESULT WaitForRenderTime(); + virtual HRESULT CompleteStateChange(FILTER_STATE OldState); + + // Return internal information about this filter + + BOOL IsEndOfStream() { return m_bEOS; }; + BOOL IsEndOfStreamDelivered() { return m_bEOSDelivered; }; + BOOL IsStreaming() { return m_bStreaming; }; + void SetAbortSignal(BOOL bAbort) { m_bAbort = bAbort; }; + virtual void OnReceiveFirstSample(IMediaSample *pMediaSample) { }; + CAMEvent *GetRenderEvent() { return &m_RenderEvent; }; + + // Permit access to the transition state + + void Ready() { m_evComplete.Set(); }; + void NotReady() { m_evComplete.Reset(); }; + BOOL CheckReady() { return m_evComplete.Check(); }; + + virtual int GetPinCount(); + virtual CBasePin *GetPin(int n); + FILTER_STATE GetRealState(); + void SendRepaint(); + void SendNotifyWindow(IPin *pPin,HWND hwnd); + BOOL OnDisplayChange(); + void SetRepaintStatus(BOOL bRepaint); + + // Override the filter and pin interface functions + + STDMETHODIMP Stop(); + STDMETHODIMP Pause(); + STDMETHODIMP Run(REFERENCE_TIME StartTime); + STDMETHODIMP GetState(DWORD dwMSecs,FILTER_STATE *State); + STDMETHODIMP FindPin(LPCWSTR Id, IPin **ppPin); + + // These are available for a quality management implementation + + virtual void OnRenderStart(IMediaSample *pMediaSample); + virtual void OnRenderEnd(IMediaSample *pMediaSample); + virtual HRESULT OnStartStreaming() { return NOERROR; }; + virtual HRESULT OnStopStreaming() { return NOERROR; }; + virtual void OnWaitStart() { }; + virtual void OnWaitEnd() { }; + virtual void PrepareRender() { }; + +#ifdef PERF + REFERENCE_TIME m_trRenderStart; // Just before we started drawing + // Set in OnRenderStart, Used in OnRenderEnd + int m_idBaseStamp; // MSR_id for frame time stamp + int m_idBaseRenderTime; // MSR_id for true wait time + int m_idBaseAccuracy; // MSR_id for time frame is late (int) +#endif + + // Quality management implementation for scheduling rendering + + virtual BOOL ScheduleSample(IMediaSample *pMediaSample); + virtual HRESULT GetSampleTimes(IMediaSample *pMediaSample, + REFERENCE_TIME *pStartTime, + REFERENCE_TIME *pEndTime); + + virtual HRESULT ShouldDrawSampleNow(IMediaSample *pMediaSample, + REFERENCE_TIME *ptrStart, + REFERENCE_TIME *ptrEnd); + + // Lots of end of stream complexities + + void TimerCallback(); + void ResetEndOfStreamTimer(); + HRESULT NotifyEndOfStream(); + virtual HRESULT SendEndOfStream(); + virtual HRESULT ResetEndOfStream(); + virtual HRESULT EndOfStream(); + + // Rendering is based around the clock + + void SignalTimerFired(); + virtual HRESULT CancelNotification(); + virtual HRESULT ClearPendingSample(); + + // Called when the filter changes state + + virtual HRESULT Active(); + virtual HRESULT Inactive(); + virtual HRESULT StartStreaming(); + virtual HRESULT StopStreaming(); + virtual HRESULT BeginFlush(); + virtual HRESULT EndFlush(); + + // Deal with connections and type changes + + virtual HRESULT BreakConnect(); + virtual HRESULT SetMediaType(const CMediaType *pmt); + virtual HRESULT CompleteConnect(IPin *pReceivePin); + + // These look after the handling of data samples + + virtual HRESULT PrepareReceive(IMediaSample *pMediaSample); + virtual HRESULT Receive(IMediaSample *pMediaSample); + virtual BOOL HaveCurrentSample(); + virtual IMediaSample *GetCurrentSample(); + virtual HRESULT Render(IMediaSample *pMediaSample); + + // Derived classes MUST override these + virtual HRESULT DoRenderSample(IMediaSample *pMediaSample) PURE; + virtual HRESULT CheckMediaType(const CMediaType *) PURE; + + // Helper + void WaitForReceiveToComplete(); +}; + + +// CBaseVideoRenderer is a renderer class (see its ancestor class) and +// it handles scheduling of media samples so that they are drawn at the +// correct time by the reference clock. It implements a degradation +// strategy. Possible degradation modes are: +// Drop frames here (only useful if the drawing takes significant time) +// Signal supplier (upstream) to drop some frame(s) - i.e. one-off skip. +// Signal supplier to change the frame rate - i.e. ongoing skipping. +// Or any combination of the above. +// In order to determine what's useful to try we need to know what's going +// on. This is done by timing various operations (including the supplier). +// This timing is done by using timeGetTime as it is accurate enough and +// usually cheaper than calling the reference clock. It also tells the +// truth if there is an audio break and the reference clock stops. +// We provide a number of public entry points (named OnXxxStart, OnXxxEnd) +// which the rest of the renderer calls at significant moments. These do +// the timing. + +// the number of frames that the sliding averages are averaged over. +// the rule is (1024*NewObservation + (AVGPERIOD-1) * PreviousAverage)/AVGPERIOD +#define AVGPERIOD 4 +#define DO_MOVING_AVG(avg,obs) (avg = (1024*obs + (AVGPERIOD-1)*avg)/AVGPERIOD) +// Spot the bug in this macro - I can't. but it doesn't work! + +class CBaseVideoRenderer : public CBaseRenderer, // Base renderer class + public IQualProp, // Property page guff + public IQualityControl // Allow throttling +{ +protected: + + // Hungarian: + // tFoo is the time Foo in mSec (beware m_tStart from filter.h) + // trBar is the time Bar by the reference clock + + //****************************************************************** + // State variables to control synchronisation + //****************************************************************** + + // Control of sending Quality messages. We need to know whether + // we are in trouble (e.g. frames being dropped) and where the time + // is being spent. + + // When we drop a frame we play the next one early. + // The frame after that is likely to wait before drawing and counting this + // wait as spare time is unfair, so we count it as a zero wait. + // We therefore need to know whether we are playing frames early or not. + + int m_nNormal; // The number of consecutive frames + // drawn at their normal time (not early) + // -1 means we just dropped a frame. + +#ifdef PERF + BOOL m_bDrawLateFrames; // Don't drop any frames (debug and I'm + // not keen on people using it!) +#endif + + BOOL m_bSupplierHandlingQuality;// The response to Quality messages says + // our supplier is handling things. + // We will allow things to go extra late + // before dropping frames. We will play + // very early after he has dropped one. + + // Control of scheduling, frame dropping etc. + // We need to know where the time is being spent so as to tell whether + // we should be taking action here, signalling supplier or what. + // The variables are initialised to a mode of NOT dropping frames. + // They will tell the truth after a few frames. + // We typically record a start time for an event, later we get the time + // again and subtract to get the elapsed time, and we average this over + // a few frames. The average is used to tell what mode we are in. + + // Although these are reference times (64 bit) they are all DIFFERENCES + // between times which are small. An int will go up to 214 secs before + // overflow. Avoiding 64 bit multiplications and divisions seems + // worth while. + + + + // Audio-video throttling. If the user has turned up audio quality + // very high (in principle it could be any other stream, not just audio) + // then we can receive cries for help via the graph manager. In this case + // we put in a wait for some time after rendering each frame. + int m_trThrottle; + + // The time taken to render (i.e. BitBlt) frames controls which component + // needs to degrade. If the blt is expensive, the renderer degrades. + // If the blt is cheap it's done anyway and the supplier degrades. + int m_trRenderAvg; // Time frames are taking to blt + int m_trRenderLast; // Time for last frame blt + int m_tRenderStart; // Just before we started drawing (mSec) + // derived from timeGetTime. + + // When frames are dropped we will play the next frame as early as we can. + // If it was a false alarm and the machine is fast we slide gently back to + // normal timing. To do this, we record the offset showing just how early + // we really are. This will normally be negative meaning early or zero. + int m_trEarliness; + + // Target provides slow long-term feedback to try to reduce the + // average sync offset to zero. Whenever a frame is actually rendered + // early we add a msec or two, whenever late we take off a few. + // We add or take off 1/32 of the error time. + // Eventually we should be hovering around zero. For a really bad case + // where we were (say) 300mSec off, it might take 100 odd frames to + // settle down. The rate of change of this is intended to be slower + // than any other mechanism in Quartz, thereby avoiding hunting. + int m_trTarget; + + // The proportion of time spent waiting for the right moment to blt + // controls whether we bother to drop a frame or whether we reckon that + // we're doing well enough that we can stand a one-frame glitch. + int m_trWaitAvg; // Average of last few wait times + // (actually we just average how early + // we were). Negative here means LATE. + + // The average inter-frame time. + // This is used to calculate the proportion of the time used by the + // three operations (supplying us, waiting, rendering) + int m_trFrameAvg; // Average inter-frame time + int m_trDuration; // duration of last frame. + +#ifdef PERF + // Performance logging identifiers + int m_idTimeStamp; // MSR_id for frame time stamp + int m_idEarliness; // MSR_id for earliness fudge + int m_idTarget; // MSR_id for Target fudge + int m_idWaitReal; // MSR_id for true wait time + int m_idWait; // MSR_id for wait time recorded + int m_idFrameAccuracy; // MSR_id for time frame is late (int) + int m_idRenderAvg; // MSR_id for Render time recorded (int) + int m_idSchLateTime; // MSR_id for lateness at scheduler + int m_idQualityRate; // MSR_id for Quality rate requested + int m_idQualityTime; // MSR_id for Quality time requested + int m_idDecision; // MSR_id for decision code + int m_idDuration; // MSR_id for duration of a frame + int m_idThrottle; // MSR_id for audio-video throttling + //int m_idDebug; // MSR_id for trace style debugging + //int m_idSendQuality; // MSR_id for timing the notifications per se +#endif // PERF + REFERENCE_TIME m_trRememberStampForPerf; // original time stamp of frame + // with no earliness fudges etc. +#ifdef PERF + REFERENCE_TIME m_trRememberFrameForPerf; // time when previous frame rendered + + // debug... + int m_idFrameAvg; + int m_idWaitAvg; +#endif + + // PROPERTY PAGE + // This has edit fields that show the user what's happening + // These member variables hold these counts. + + int m_cFramesDropped; // cumulative frames dropped IN THE RENDERER + int m_cFramesDrawn; // Frames since streaming started seen BY THE + // RENDERER (some may be dropped upstream) + + // Next two support average sync offset and standard deviation of sync offset. + LONGLONG m_iTotAcc; // Sum of accuracies in mSec + LONGLONG m_iSumSqAcc; // Sum of squares of (accuracies in mSec) + + // Next two allow jitter calculation. Jitter is std deviation of frame time. + REFERENCE_TIME m_trLastDraw; // Time of prev frame (for inter-frame times) + LONGLONG m_iSumSqFrameTime; // Sum of squares of (inter-frame time in mSec) + LONGLONG m_iSumFrameTime; // Sum of inter-frame times in mSec + + // To get performance statistics on frame rate, jitter etc, we need + // to record the lateness and inter-frame time. What we actually need are the + // data above (sum, sum of squares and number of entries for each) but the data + // is generated just ahead of time and only later do we discover whether the + // frame was actually drawn or not. So we have to hang on to the data + int m_trLate; // hold onto frame lateness + int m_trFrame; // hold onto inter-frame time + + int m_tStreamingStart; // if streaming then time streaming started + // else time of last streaming session + // used for property page statistics +#ifdef PERF + LONGLONG m_llTimeOffset; // timeGetTime()*10000+m_llTimeOffset==ref time +#endif + +public: + + + CBaseVideoRenderer(REFCLSID RenderClass, // CLSID for this renderer + TCHAR *pName, // Debug ONLY description + LPUNKNOWN pUnk, // Aggregated owner object + HRESULT *phr); // General OLE return code + + ~CBaseVideoRenderer(); + + // IQualityControl methods - Notify allows audio-video throttling + + STDMETHODIMP SetSink( IQualityControl * piqc); + STDMETHODIMP Notify( IBaseFilter * pSelf, Quality q); + + // These provide a full video quality management implementation + + void OnRenderStart(IMediaSample *pMediaSample); + void OnRenderEnd(IMediaSample *pMediaSample); + void OnWaitStart(); + void OnWaitEnd(); + HRESULT OnStartStreaming(); + HRESULT OnStopStreaming(); + void ThrottleWait(); + + // Handle the statistics gathering for our quality management + + void PreparePerformanceData(int trLate, int trFrame); + virtual void RecordFrameLateness(int trLate, int trFrame); + virtual void OnDirectRender(IMediaSample *pMediaSample); + virtual HRESULT ResetStreamingTimes(); + BOOL ScheduleSample(IMediaSample *pMediaSample); + HRESULT ShouldDrawSampleNow(IMediaSample *pMediaSample, + REFERENCE_TIME *ptrStart, + REFERENCE_TIME *ptrEnd); + + virtual HRESULT SendQuality(REFERENCE_TIME trLate, REFERENCE_TIME trRealStream); + STDMETHODIMP JoinFilterGraph(IFilterGraph * pGraph, LPCWSTR pName); + + // + // Do estimates for standard deviations for per-frame + // statistics + // + // *piResult = (llSumSq - iTot * iTot / m_cFramesDrawn - 1) / + // (m_cFramesDrawn - 2) + // or 0 if m_cFramesDrawn <= 3 + // + HRESULT GetStdDev( + int nSamples, + int *piResult, + LONGLONG llSumSq, + LONGLONG iTot + ); +public: + + // IQualProp property page support + + STDMETHODIMP get_FramesDroppedInRenderer(int *cFramesDropped); + STDMETHODIMP get_FramesDrawn(int *pcFramesDrawn); + STDMETHODIMP get_AvgFrameRate(int *piAvgFrameRate); + STDMETHODIMP get_Jitter(int *piJitter); + STDMETHODIMP get_AvgSyncOffset(int *piAvg); + STDMETHODIMP get_DevSyncOffset(int *piDev); + + // Implement an IUnknown interface and expose IQualProp + + DECLARE_IUNKNOWN + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,VOID **ppv); +}; + +#endif // __RENBASE__ + diff --git a/plugins/GSdx/baseclasses/schedule.cpp b/plugins/GSdx/baseclasses/schedule.cpp new file mode 100644 index 0000000000..170f1427b7 --- /dev/null +++ b/plugins/GSdx/baseclasses/schedule.cpp @@ -0,0 +1,284 @@ +//------------------------------------------------------------------------------ +// File: Schedule.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" + +// DbgLog values (all on LOG_TIMING): +// +// 2 for schedulting, firing and shunting of events +// 3 for wait delays and wake-up times of event thread +// 4 for details of whats on the list when the thread awakes + +/* Construct & destructors */ + +CAMSchedule::CAMSchedule( HANDLE ev ) +: CBaseObject(TEXT("CAMSchedule")) +, head(&z, 0), z(0, MAX_TIME) +, m_dwNextCookie(0), m_dwAdviseCount(0) +, m_pAdviseCache(0), m_dwCacheCount(0) +, m_ev( ev ) +{ + head.m_dwAdviseCookie = z.m_dwAdviseCookie = 0; +} + +CAMSchedule::~CAMSchedule() +{ + m_Serialize.Lock(); + + // Delete cache + CAdvisePacket * p = m_pAdviseCache; + while (p) + { + CAdvisePacket *const p_next = p->m_next; + delete p; + p = p_next; + } + + ASSERT( m_dwAdviseCount == 0 ); + // Better to be safe than sorry + if ( m_dwAdviseCount > 0 ) + { + DumpLinkedList(); + while ( !head.m_next->IsZ() ) + { + head.DeleteNext(); + --m_dwAdviseCount; + } + } + + // If, in the debug version, we assert twice, it means, not only + // did we have left over advises, but we have also let m_dwAdviseCount + // get out of sync. with the number of advises actually on the list. + ASSERT( m_dwAdviseCount == 0 ); + + m_Serialize.Unlock(); +} + +/* Public methods */ + +DWORD CAMSchedule::GetAdviseCount() +{ + // No need to lock, m_dwAdviseCount is 32bits & declared volatile + return m_dwAdviseCount; +} + +REFERENCE_TIME CAMSchedule::GetNextAdviseTime() +{ + CAutoLock lck(&m_Serialize); // Need to stop the linked list from changing + return head.m_next->m_rtEventTime; +} + +DWORD_PTR CAMSchedule::AddAdvisePacket +( const REFERENCE_TIME & time1 +, const REFERENCE_TIME & time2 +, HANDLE h, BOOL periodic +) +{ + // Since we use MAX_TIME as a sentry, we can't afford to + // schedule a notification at MAX_TIME + ASSERT( time1 < MAX_TIME ); + DWORD_PTR Result; + CAdvisePacket * p; + + m_Serialize.Lock(); + + if (m_pAdviseCache) + { + p = m_pAdviseCache; + m_pAdviseCache = p->m_next; + --m_dwCacheCount; + } + else + { + p = new CAdvisePacket(); + } + if (p) + { + p->m_rtEventTime = time1; p->m_rtPeriod = time2; + p->m_hNotify = h; p->m_bPeriodic = periodic; + Result = AddAdvisePacket( p ); + } + else Result = 0; + + m_Serialize.Unlock(); + + return Result; +} + +HRESULT CAMSchedule::Unadvise(DWORD_PTR dwAdviseCookie) +{ + HRESULT hr = S_FALSE; + CAdvisePacket * p_prev = &head; + CAdvisePacket * p_n; + m_Serialize.Lock(); + while ( p_n = p_prev->Next() ) // The Next() method returns NULL when it hits z + { + if ( p_n->m_dwAdviseCookie == dwAdviseCookie ) + { + Delete( p_prev->RemoveNext() ); + --m_dwAdviseCount; + hr = S_OK; + // Having found one cookie that matches, there should be no more + #ifdef DEBUG + while (p_n = p_prev->Next()) + { + ASSERT(p_n->m_dwAdviseCookie != dwAdviseCookie); + p_prev = p_n; + } + #endif + break; + } + p_prev = p_n; + }; + m_Serialize.Unlock(); + return hr; +} + +REFERENCE_TIME CAMSchedule::Advise( const REFERENCE_TIME & rtTime ) +{ + REFERENCE_TIME rtNextTime; + CAdvisePacket * pAdvise; + + DbgLog((LOG_TIMING, 2, + TEXT("CAMSchedule::Advise( %lu ms )"), ULONG(rtTime / (UNITS / MILLISECONDS)))); + + CAutoLock lck(&m_Serialize); + + #ifdef DEBUG + if (DbgCheckModuleLevel(LOG_TIMING, 4)) DumpLinkedList(); + #endif + + // Note - DON'T cache the difference, it might overflow + while ( rtTime >= (rtNextTime = (pAdvise=head.m_next)->m_rtEventTime) && + !pAdvise->IsZ() ) + { + ASSERT(pAdvise->m_dwAdviseCookie); // If this is zero, its the head or the tail!! + + ASSERT(pAdvise->m_hNotify != INVALID_HANDLE_VALUE); + + if (pAdvise->m_bPeriodic == TRUE) + { + ReleaseSemaphore(pAdvise->m_hNotify,1,NULL); + pAdvise->m_rtEventTime += pAdvise->m_rtPeriod; + ShuntHead(); + } + else + { + ASSERT( pAdvise->m_bPeriodic == FALSE ); + EXECUTE_ASSERT(SetEvent(pAdvise->m_hNotify)); + --m_dwAdviseCount; + Delete( head.RemoveNext() ); + } + + } + + DbgLog((LOG_TIMING, 3, + TEXT("CAMSchedule::Advise() Next time stamp: %lu ms, for advise %lu."), + DWORD(rtNextTime / (UNITS / MILLISECONDS)), pAdvise->m_dwAdviseCookie )); + + return rtNextTime; +} + +/* Private methods */ + +DWORD_PTR CAMSchedule::AddAdvisePacket( CAdvisePacket * pPacket ) +{ + ASSERT(pPacket->m_rtEventTime >= 0 && pPacket->m_rtEventTime < MAX_TIME); + ASSERT(CritCheckIn(&m_Serialize)); + + CAdvisePacket * p_prev = &head; + CAdvisePacket * p_n; + + const DWORD_PTR Result = pPacket->m_dwAdviseCookie = ++m_dwNextCookie; + // This relies on the fact that z is a sentry with a maximal m_rtEventTime + for(;;p_prev = p_n) + { + p_n = p_prev->m_next; + if ( p_n->m_rtEventTime >= pPacket->m_rtEventTime ) break; + } + p_prev->InsertAfter( pPacket ); + ++m_dwAdviseCount; + + DbgLog((LOG_TIMING, 2, TEXT("Added advise %lu, for thread 0x%02X, scheduled at %lu"), + pPacket->m_dwAdviseCookie, GetCurrentThreadId(), (pPacket->m_rtEventTime / (UNITS / MILLISECONDS)) )); + + // If packet added at the head, then clock needs to re-evaluate wait time. + if ( p_prev == &head ) SetEvent( m_ev ); + + return Result; +} + +void CAMSchedule::Delete( CAdvisePacket * pPacket ) +{ + if ( m_dwCacheCount >= dwCacheMax ) delete pPacket; + else + { + m_Serialize.Lock(); + pPacket->m_next = m_pAdviseCache; + m_pAdviseCache = pPacket; + ++m_dwCacheCount; + m_Serialize.Unlock(); + } +} + + +// Takes the head of the list & repositions it +void CAMSchedule::ShuntHead() +{ + CAdvisePacket * p_prev = &head; + CAdvisePacket * p_n; + + m_Serialize.Lock(); + CAdvisePacket *const pPacket = head.m_next; + + // This will catch both an empty list, + // and if somehow a MAX_TIME time gets into the list + // (which would also break this method). + ASSERT( pPacket->m_rtEventTime < MAX_TIME ); + + // This relies on the fact that z is a sentry with a maximal m_rtEventTime + for(;;p_prev = p_n) + { + p_n = p_prev->m_next; + if ( p_n->m_rtEventTime > pPacket->m_rtEventTime ) break; + } + // If p_prev == pPacket then we're already in the right place + if (p_prev != pPacket) + { + head.m_next = pPacket->m_next; + (p_prev->m_next = pPacket)->m_next = p_n; + } + #ifdef DEBUG + DbgLog((LOG_TIMING, 2, TEXT("Periodic advise %lu, shunted to %lu"), + pPacket->m_dwAdviseCookie, (pPacket->m_rtEventTime / (UNITS / MILLISECONDS)) )); + #endif + m_Serialize.Unlock(); +} + + +#ifdef DEBUG +void CAMSchedule::DumpLinkedList() +{ + m_Serialize.Lock(); + int i=0; + DbgLog((LOG_TIMING, 1, TEXT("CAMSchedule::DumpLinkedList() this = 0x%p"), this)); + for ( CAdvisePacket * p = &head + ; p + ; p = p->m_next , i++ + ) + { + DbgLog((LOG_TIMING, 1, TEXT("Advise List # %lu, Cookie %d, RefTime %lu"), + i, + p->m_dwAdviseCookie, + p->m_rtEventTime / (UNITS / MILLISECONDS) + )); + } + m_Serialize.Unlock(); +} +#endif diff --git a/plugins/GSdx/baseclasses/schedule.h b/plugins/GSdx/baseclasses/schedule.h new file mode 100644 index 0000000000..2d01911b31 --- /dev/null +++ b/plugins/GSdx/baseclasses/schedule.h @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// File: Schedule.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __CAMSchedule__ +#define __CAMSchedule__ + +class CAMSchedule : private CBaseObject +{ +public: + virtual ~CAMSchedule(); + // ev is the event we should fire if the advise time needs re-evaluating + CAMSchedule( HANDLE ev ); + + DWORD GetAdviseCount(); + REFERENCE_TIME GetNextAdviseTime(); + + // We need a method for derived classes to add advise packets, we return the cookie + DWORD_PTR AddAdvisePacket( const REFERENCE_TIME & time1, const REFERENCE_TIME & time2, HANDLE h, BOOL periodic ); + // And a way to cancel + HRESULT Unadvise(DWORD_PTR dwAdviseCookie); + + // Tell us the time please, and we'll dispatch the expired events. We return the time of the next event. + // NB: The time returned will be "useless" if you start adding extra Advises. But that's the problem of + // whoever is using this helper class (typically a clock). + REFERENCE_TIME Advise( const REFERENCE_TIME & rtTime ); + + // Get the event handle which will be set if advise time requires re-evaluation. + HANDLE GetEvent() const { return m_ev; } + +private: + // We define the nodes that will be used in our singly linked list + // of advise packets. The list is ordered by time, with the + // elements that will expire first at the front. + class CAdvisePacket + { + public: + CAdvisePacket() + {} + + CAdvisePacket * m_next; + DWORD_PTR m_dwAdviseCookie; + REFERENCE_TIME m_rtEventTime; // Time at which event should be set + REFERENCE_TIME m_rtPeriod; // Periodic time + HANDLE m_hNotify; // Handle to event or semephore + BOOL m_bPeriodic; // TRUE => Periodic event + + CAdvisePacket( CAdvisePacket * next, LONGLONG time ) : m_next(next), m_rtEventTime(time) + {} + + void InsertAfter( CAdvisePacket * p ) + { + p->m_next = m_next; + m_next = p; + } + + int IsZ() const // That is, is it the node that represents the end of the list + { return m_next == 0; } + + CAdvisePacket * RemoveNext() + { + CAdvisePacket *const next = m_next; + CAdvisePacket *const new_next = next->m_next; + m_next = new_next; + return next; + } + + void DeleteNext() + { + delete RemoveNext(); + } + + CAdvisePacket * Next() const + { + CAdvisePacket * result = m_next; + if (result->IsZ()) result = 0; + return result; + } + + DWORD_PTR Cookie() const + { return m_dwAdviseCookie; } + }; + + // Structure is: + // head -> elmt1 -> elmt2 -> z -> null + // So an empty list is: head -> z -> null + // Having head & z as links makes insertaion, + // deletion and shunting much easier. + CAdvisePacket head, z; // z is both a tail and a sentry + + volatile DWORD_PTR m_dwNextCookie; // Strictly increasing + volatile DWORD m_dwAdviseCount; // Number of elements on list + + CCritSec m_Serialize; + + // AddAdvisePacket: adds the packet, returns the cookie (0 if failed) + DWORD_PTR AddAdvisePacket( CAdvisePacket * pPacket ); + // Event that we should set if the packed added above will be the next to fire. + const HANDLE m_ev; + + // A Shunt is where we have changed the first element in the + // list and want it re-evaluating (i.e. repositioned) in + // the list. + void ShuntHead(); + + // Rather than delete advise packets, we cache them for future use + CAdvisePacket * m_pAdviseCache; + DWORD m_dwCacheCount; + enum { dwCacheMax = 5 }; // Don't bother caching more than five + + void Delete( CAdvisePacket * pLink );// This "Delete" will cache the Link + +// Attributes and methods for debugging +public: +#ifdef DEBUG + void DumpLinkedList(); +#else + void DumpLinkedList() {} +#endif + +}; + +#endif // __CAMSchedule__ diff --git a/plugins/GSdx/baseclasses/seekpt.cpp b/plugins/GSdx/baseclasses/seekpt.cpp new file mode 100644 index 0000000000..5b90c09505 --- /dev/null +++ b/plugins/GSdx/baseclasses/seekpt.cpp @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// File: SeekPT.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include "seekpt.h" + +//================================================================== +// CreateInstance +// This goes in the factory template table to create new instances +// If there is already a mapper instance - return that, else make one +// and save it in a static variable so that forever after we can return that. +//================================================================== + +CUnknown * CSeekingPassThru::CreateInstance(LPUNKNOWN pUnk, HRESULT *phr) +{ + return new CSeekingPassThru(NAME("Seeking PassThru"),pUnk, phr); +} + + +STDMETHODIMP CSeekingPassThru::NonDelegatingQueryInterface(REFIID riid, void ** ppv) +{ + if (riid == IID_ISeekingPassThru) { + return GetInterface((ISeekingPassThru *) this, ppv); + } else { + if (m_pPosPassThru && + (riid == IID_IMediaSeeking || + riid == IID_IMediaPosition)) { + return m_pPosPassThru->NonDelegatingQueryInterface(riid,ppv); + } else { + return CUnknown::NonDelegatingQueryInterface(riid, ppv); + } + } +} + + +CSeekingPassThru::CSeekingPassThru( TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr ) + : CUnknown(pName, pUnk, phr), + m_pPosPassThru(NULL) +{ +} + + +CSeekingPassThru::~CSeekingPassThru() +{ + delete m_pPosPassThru; +} + +STDMETHODIMP CSeekingPassThru::Init(BOOL bRendererSeeking, IPin *pPin) +{ + HRESULT hr = NOERROR; + if (m_pPosPassThru) { + hr = E_FAIL; + } else { + m_pPosPassThru = + bRendererSeeking ? + new CRendererPosPassThru( + NAME("Render Seeking COM object"), + (IUnknown *)this, + &hr, + pPin) : + new CPosPassThru( + NAME("Render Seeking COM object"), + (IUnknown *)this, + &hr, + pPin); + if (!m_pPosPassThru) { + hr = E_OUTOFMEMORY; + } else { + if (FAILED(hr)) { + delete m_pPosPassThru; + m_pPosPassThru = NULL; + } + } + } + return hr; +} + diff --git a/plugins/GSdx/baseclasses/seekpt.h b/plugins/GSdx/baseclasses/seekpt.h new file mode 100644 index 0000000000..93b7061398 --- /dev/null +++ b/plugins/GSdx/baseclasses/seekpt.h @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// File: SeekPT.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __seekpt_h__ +#define __seekpt_h__ + + +class CSeekingPassThru : public ISeekingPassThru, public CUnknown +{ +public: + static CUnknown *CreateInstance(LPUNKNOWN pUnk, HRESULT *phr); + CSeekingPassThru(TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr); + ~CSeekingPassThru(); + + DECLARE_IUNKNOWN; + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv); + + STDMETHODIMP Init(BOOL bSupportRendering, IPin *pPin); + +private: + CPosPassThru *m_pPosPassThru; +}; + +#endif diff --git a/plugins/GSdx/baseclasses/source.cpp b/plugins/GSdx/baseclasses/source.cpp new file mode 100644 index 0000000000..9d58373617 --- /dev/null +++ b/plugins/GSdx/baseclasses/source.cpp @@ -0,0 +1,522 @@ +//------------------------------------------------------------------------------ +// File: Source.cpp +// +// Desc: DirectShow base classes - implements CSource, which is a Quartz +// source filter 'template.' +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// Locking Strategy. +// +// Hold the filter critical section (m_pFilter->pStateLock()) to serialise +// access to functions. Note that, in general, this lock may be held +// by a function when the worker thread may want to hold it. Therefore +// if you wish to access shared state from the worker thread you will +// need to add another critical section object. The execption is during +// the threads processing loop, when it is safe to get the filter critical +// section from within FillBuffer(). + +#include "streams.h" + + +// +// CSource::Constructor +// +// Initialise the pin count for the filter. The user will create the pins in +// the derived class. +CSource::CSource(TCHAR *pName, LPUNKNOWN lpunk, CLSID clsid) + : CBaseFilter(pName, lpunk, &m_cStateLock, clsid), + m_iPins(0), + m_paStreams(NULL) +{ +} + +CSource::CSource(TCHAR *pName, LPUNKNOWN lpunk, CLSID clsid, HRESULT *phr) + : CBaseFilter(pName, lpunk, &m_cStateLock, clsid), + m_iPins(0), + m_paStreams(NULL) +{ + UNREFERENCED_PARAMETER(phr); +} + +#ifdef UNICODE +CSource::CSource(CHAR *pName, LPUNKNOWN lpunk, CLSID clsid) + : CBaseFilter(pName, lpunk, &m_cStateLock, clsid), + m_iPins(0), + m_paStreams(NULL) +{ +} + +CSource::CSource(CHAR *pName, LPUNKNOWN lpunk, CLSID clsid, HRESULT *phr) + : CBaseFilter(pName, lpunk, &m_cStateLock, clsid), + m_iPins(0), + m_paStreams(NULL) +{ + UNREFERENCED_PARAMETER(phr); +} +#endif + +// +// CSource::Destructor +// +CSource::~CSource() +{ + /* Free our pins and pin array */ + while (m_iPins != 0) { + // deleting the pins causes them to be removed from the array... + delete m_paStreams[m_iPins - 1]; + } + + ASSERT(m_paStreams == NULL); +} + + +// +// Add a new pin +// +HRESULT CSource::AddPin(CSourceStream *pStream) +{ + CAutoLock lock(&m_cStateLock); + + /* Allocate space for this pin and the old ones */ + CSourceStream **paStreams = new CSourceStream *[m_iPins + 1]; + if (paStreams == NULL) { + return E_OUTOFMEMORY; + } + if (m_paStreams != NULL) { + CopyMemory((PVOID)paStreams, (PVOID)m_paStreams, + m_iPins * sizeof(m_paStreams[0])); + paStreams[m_iPins] = pStream; + delete [] m_paStreams; + } + m_paStreams = paStreams; + m_paStreams[m_iPins] = pStream; + m_iPins++; + return S_OK; +} + +// +// Remove a pin - pStream is NOT deleted +// +HRESULT CSource::RemovePin(CSourceStream *pStream) +{ + int i; + for (i = 0; i < m_iPins; i++) { + if (m_paStreams[i] == pStream) { + if (m_iPins == 1) { + delete [] m_paStreams; + m_paStreams = NULL; + } else { + /* no need to reallocate */ + while (++i < m_iPins) + m_paStreams[i - 1] = m_paStreams[i]; + } + m_iPins--; + return S_OK; + } + } + return S_FALSE; +} + +// +// FindPin +// +// Set *ppPin to the IPin* that has the id Id. +// or to NULL if the Id cannot be matched. +STDMETHODIMP CSource::FindPin(LPCWSTR Id, IPin **ppPin) +{ + CheckPointer(ppPin,E_POINTER); + ValidateReadWritePtr(ppPin,sizeof(IPin *)); + // The -1 undoes the +1 in QueryId and ensures that totally invalid + // strings (for which WstrToInt delivers 0) give a deliver a NULL pin. + int i = WstrToInt(Id) -1; + *ppPin = GetPin(i); + if (*ppPin!=NULL){ + (*ppPin)->AddRef(); + return NOERROR; + } else { + return VFW_E_NOT_FOUND; + } +} + +// +// FindPinNumber +// +// return the number of the pin with this IPin* or -1 if none +int CSource::FindPinNumber(IPin *iPin) { + int i; + for (i=0; in && n>=0 it follows that m_iPins>0 + // which is what used to be checked (i.e. checking that we have a pin) + if ((n >= 0) && (n < m_iPins)) { + + ASSERT(m_paStreams[n]); + return m_paStreams[n]; + } + return NULL; +} + + +// + + +// * +// * --- CSourceStream ---- +// * + +// +// Set Id to point to a CoTaskMemAlloc'd +STDMETHODIMP CSourceStream::QueryId(LPWSTR *Id) { + CheckPointer(Id,E_POINTER); + ValidateReadWritePtr(Id,sizeof(LPWSTR)); + + // We give the pins id's which are 1,2,... + // FindPinNumber returns -1 for an invalid pin + int i = 1+ m_pFilter->FindPinNumber(this); + if (i<1) return VFW_E_NOT_FOUND; + *Id = (LPWSTR)CoTaskMemAlloc(4*sizeof(WCHAR)); + if (*Id==NULL) { + return E_OUTOFMEMORY; + } + IntToWstr(i, *Id, 4); + return NOERROR; +} + + + +// +// CSourceStream::Constructor +// +// increments the number of pins present on the filter +CSourceStream::CSourceStream( + TCHAR *pObjectName, + HRESULT *phr, + CSource *ps, + LPCWSTR pPinName) + : CBaseOutputPin(pObjectName, ps, ps->pStateLock(), phr, pPinName), + m_pFilter(ps) { + + *phr = m_pFilter->AddPin(this); +} + +#ifdef UNICODE +CSourceStream::CSourceStream( + char *pObjectName, + HRESULT *phr, + CSource *ps, + LPCWSTR pPinName) + : CBaseOutputPin(pObjectName, ps, ps->pStateLock(), phr, pPinName), + m_pFilter(ps) { + + *phr = m_pFilter->AddPin(this); +} +#endif +// +// CSourceStream::Destructor +// +// Decrements the number of pins on this filter +CSourceStream::~CSourceStream(void) { + + m_pFilter->RemovePin(this); +} + + +// +// CheckMediaType +// +// Do we support this type? Provides the default support for 1 type. +HRESULT CSourceStream::CheckMediaType(const CMediaType *pMediaType) { + + CAutoLock lock(m_pFilter->pStateLock()); + + CMediaType mt; + GetMediaType(&mt); + + if (mt == *pMediaType) { + return NOERROR; + } + + return E_FAIL; +} + + +// +// GetMediaType/3 +// +// By default we support only one type +// iPosition indexes are 0-n +HRESULT CSourceStream::GetMediaType(int iPosition, CMediaType *pMediaType) { + + CAutoLock lock(m_pFilter->pStateLock()); + + if (iPosition<0) { + return E_INVALIDARG; + } + if (iPosition>0) { + return VFW_S_NO_MORE_ITEMS; + } + return GetMediaType(pMediaType); +} + + +// +// Active +// +// The pin is active - start up the worker thread +HRESULT CSourceStream::Active(void) { + + CAutoLock lock(m_pFilter->pStateLock()); + + HRESULT hr; + + if (m_pFilter->IsActive()) { + return S_FALSE; // succeeded, but did not allocate resources (they already exist...) + } + + // do nothing if not connected - its ok not to connect to + // all pins of a source filter + if (!IsConnected()) { + return NOERROR; + } + + hr = CBaseOutputPin::Active(); + if (FAILED(hr)) { + return hr; + } + + ASSERT(!ThreadExists()); + + // start the thread + if (!Create()) { + return E_FAIL; + } + + // Tell thread to initialize. If OnThreadCreate Fails, so does this. + hr = Init(); + if (FAILED(hr)) + return hr; + + return Pause(); +} + + +// +// Inactive +// +// Pin is inactive - shut down the worker thread +// Waits for the worker to exit before returning. +HRESULT CSourceStream::Inactive(void) { + + CAutoLock lock(m_pFilter->pStateLock()); + + HRESULT hr; + + // do nothing if not connected - its ok not to connect to + // all pins of a source filter + if (!IsConnected()) { + return NOERROR; + } + + // !!! need to do this before trying to stop the thread, because + // we may be stuck waiting for our own allocator!!! + + hr = CBaseOutputPin::Inactive(); // call this first to Decommit the allocator + if (FAILED(hr)) { + return hr; + } + + if (ThreadExists()) { + hr = Stop(); + + if (FAILED(hr)) { + return hr; + } + + hr = Exit(); + if (FAILED(hr)) { + return hr; + } + + Close(); // Wait for the thread to exit, then tidy up. + } + + // hr = CBaseOutputPin::Inactive(); // call this first to Decommit the allocator + //if (FAILED(hr)) { + // return hr; + //} + + return NOERROR; +} + + +// +// ThreadProc +// +// When this returns the thread exits +// Return codes > 0 indicate an error occured +DWORD CSourceStream::ThreadProc(void) { + + HRESULT hr; // the return code from calls + Command com; + + do { + com = GetRequest(); + if (com != CMD_INIT) { + DbgLog((LOG_ERROR, 1, TEXT("Thread expected init command"))); + Reply((DWORD) E_UNEXPECTED); + } + } while (com != CMD_INIT); + + DbgLog((LOG_TRACE, 1, TEXT("CSourceStream worker thread initializing"))); + + hr = OnThreadCreate(); // perform set up tasks + if (FAILED(hr)) { + DbgLog((LOG_ERROR, 1, TEXT("CSourceStream::OnThreadCreate failed. Aborting thread."))); + OnThreadDestroy(); + Reply(hr); // send failed return code from OnThreadCreate + return 1; + } + + // Initialisation suceeded + Reply(NOERROR); + + Command cmd; + do { + cmd = GetRequest(); + + switch (cmd) { + + case CMD_EXIT: + Reply(NOERROR); + break; + + case CMD_RUN: + DbgLog((LOG_ERROR, 1, TEXT("CMD_RUN received before a CMD_PAUSE???"))); + // !!! fall through??? + + case CMD_PAUSE: + Reply(NOERROR); + DoBufferProcessingLoop(); + break; + + case CMD_STOP: + Reply(NOERROR); + break; + + default: + DbgLog((LOG_ERROR, 1, TEXT("Unknown command %d received!"), cmd)); + Reply((DWORD) E_NOTIMPL); + break; + } + } while (cmd != CMD_EXIT); + + hr = OnThreadDestroy(); // tidy up. + if (FAILED(hr)) { + DbgLog((LOG_ERROR, 1, TEXT("CSourceStream::OnThreadDestroy failed. Exiting thread."))); + return 1; + } + + DbgLog((LOG_TRACE, 1, TEXT("CSourceStream worker thread exiting"))); + return 0; +} + + +// +// DoBufferProcessingLoop +// +// Grabs a buffer and calls the users processing function. +// Overridable, so that different delivery styles can be catered for. +HRESULT CSourceStream::DoBufferProcessingLoop(void) { + + Command com; + + OnThreadStartPlay(); + + do { + while (!CheckRequest(&com)) { + + IMediaSample *pSample; + + HRESULT hr = GetDeliveryBuffer(&pSample,NULL,NULL,0); + if (FAILED(hr)) { + Sleep(1); + continue; // go round again. Perhaps the error will go away + // or the allocator is decommited & we will be asked to + // exit soon. + } + + // Virtual function user will override. + hr = FillBuffer(pSample); + + if (hr == S_OK) { + hr = Deliver(pSample); + pSample->Release(); + + // downstream filter returns S_FALSE if it wants us to + // stop or an error if it's reporting an error. + if(hr != S_OK) + { + DbgLog((LOG_TRACE, 2, TEXT("Deliver() returned %08x; stopping"), hr)); + return S_OK; + } + + } else if (hr == S_FALSE) { + // derived class wants us to stop pushing data + pSample->Release(); + DeliverEndOfStream(); + return S_OK; + } else { + // derived class encountered an error + pSample->Release(); + DbgLog((LOG_ERROR, 1, TEXT("Error %08lX from FillBuffer!!!"), hr)); + DeliverEndOfStream(); + m_pFilter->NotifyEvent(EC_ERRORABORT, hr, 0); + return hr; + } + + // all paths release the sample + } + + // For all commands sent to us there must be a Reply call! + + if (com == CMD_RUN || com == CMD_PAUSE) { + Reply(NOERROR); + } else if (com != CMD_STOP) { + Reply((DWORD) E_UNEXPECTED); + DbgLog((LOG_ERROR, 1, TEXT("Unexpected command!!!"))); + } + } while (com != CMD_STOP); + + return S_FALSE; +} + diff --git a/plugins/GSdx/baseclasses/source.h b/plugins/GSdx/baseclasses/source.h new file mode 100644 index 0000000000..6dff3e7569 --- /dev/null +++ b/plugins/GSdx/baseclasses/source.h @@ -0,0 +1,172 @@ +//------------------------------------------------------------------------------ +// File: Source.h +// +// Desc: DirectShow base classes - defines classes to simplify creation of +// ActiveX source filters that support continuous generation of data. +// No support is provided for IMediaControl or IMediaPosition. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// +// Derive your source filter from CSource. +// During construction either: +// Create some CSourceStream objects to manage your pins +// Provide the user with a means of doing so eg, an IPersistFile interface. +// +// CSource provides: +// IBaseFilter interface management +// IMediaFilter interface management, via CBaseFilter +// Pin counting for CBaseFilter +// +// Derive a class from CSourceStream to manage your output pin types +// Implement GetMediaType/1 to return the type you support. If you support multiple +// types then overide GetMediaType/3, CheckMediaType and GetMediaTypeCount. +// Implement Fillbuffer() to put data into one buffer. +// +// CSourceStream provides: +// IPin management via CBaseOutputPin +// Worker thread management + +#ifndef __CSOURCE__ +#define __CSOURCE__ + +class CSourceStream; // The class that will handle each pin + + +// +// CSource +// +// Override construction to provide a means of creating +// CSourceStream derived objects - ie a way of creating pins. +class CSource : public CBaseFilter { +public: + + CSource(TCHAR *pName, LPUNKNOWN lpunk, CLSID clsid, HRESULT *phr); + CSource(TCHAR *pName, LPUNKNOWN lpunk, CLSID clsid); +#ifdef UNICODE + CSource(CHAR *pName, LPUNKNOWN lpunk, CLSID clsid, HRESULT *phr); + CSource(CHAR *pName, LPUNKNOWN lpunk, CLSID clsid); +#endif + ~CSource(); + + int GetPinCount(void); + CBasePin *GetPin(int n); + + // -- Utilities -- + + CCritSec* pStateLock(void) { return &m_cStateLock; } // provide our critical section + + HRESULT AddPin(CSourceStream *); + HRESULT RemovePin(CSourceStream *); + + STDMETHODIMP FindPin( + LPCWSTR Id, + IPin ** ppPin + ); + + int FindPinNumber(IPin *iPin); + +protected: + + int m_iPins; // The number of pins on this filter. Updated by CSourceStream + // constructors & destructors. + CSourceStream **m_paStreams; // the pins on this filter. + + CCritSec m_cStateLock; // Lock this to serialize function accesses to the filter state + +}; + + +// +// CSourceStream +// +// Use this class to manage a stream of data that comes from a +// pin. +// Uses a worker thread to put data on the pin. +class CSourceStream : public CAMThread, public CBaseOutputPin { +public: + + CSourceStream(TCHAR *pObjectName, + HRESULT *phr, + CSource *pms, + LPCWSTR pName); +#ifdef UNICODE + CSourceStream(CHAR *pObjectName, + HRESULT *phr, + CSource *pms, + LPCWSTR pName); +#endif + virtual ~CSourceStream(void); // virtual destructor ensures derived class destructors are called too. + +protected: + + CSource *m_pFilter; // The parent of this stream + + // * + // * Data Source + // * + // * The following three functions: FillBuffer, OnThreadCreate/Destroy, are + // * called from within the ThreadProc. They are used in the creation of + // * the media samples this pin will provide + // * + + // Override this to provide the worker thread a means + // of processing a buffer + virtual HRESULT FillBuffer(IMediaSample *pSamp) PURE; + + // Called as the thread is created/destroyed - use to perform + // jobs such as start/stop streaming mode + // If OnThreadCreate returns an error the thread will exit. + virtual HRESULT OnThreadCreate(void) {return NOERROR;}; + virtual HRESULT OnThreadDestroy(void) {return NOERROR;}; + virtual HRESULT OnThreadStartPlay(void) {return NOERROR;}; + + // * + // * Worker Thread + // * + + HRESULT Active(void); // Starts up the worker thread + HRESULT Inactive(void); // Exits the worker thread. + +public: + // thread commands + enum Command {CMD_INIT, CMD_PAUSE, CMD_RUN, CMD_STOP, CMD_EXIT}; + HRESULT Init(void) { return CallWorker(CMD_INIT); } + HRESULT Exit(void) { return CallWorker(CMD_EXIT); } + HRESULT Run(void) { return CallWorker(CMD_RUN); } + HRESULT Pause(void) { return CallWorker(CMD_PAUSE); } + HRESULT Stop(void) { return CallWorker(CMD_STOP); } + +protected: + Command GetRequest(void) { return (Command) CAMThread::GetRequest(); } + BOOL CheckRequest(Command *pCom) { return CAMThread::CheckRequest( (DWORD *) pCom); } + + // override these if you want to add thread commands + virtual DWORD ThreadProc(void); // the thread function + + virtual HRESULT DoBufferProcessingLoop(void); // the loop executed whilst running + + + // * + // * AM_MEDIA_TYPE support + // * + + // If you support more than one media type then override these 2 functions + virtual HRESULT CheckMediaType(const CMediaType *pMediaType); + virtual HRESULT GetMediaType(int iPosition, CMediaType *pMediaType); // List pos. 0-n + + // If you support only one type then override this fn. + // This will only be called by the default implementations + // of CheckMediaType and GetMediaType(int, CMediaType*) + // You must override this fn. or the above 2! + virtual HRESULT GetMediaType(CMediaType *pMediaType) {return E_UNEXPECTED;} + + STDMETHODIMP QueryId( + LPWSTR * Id + ); +}; + +#endif // __CSOURCE__ + diff --git a/plugins/GSdx/baseclasses/streams.h b/plugins/GSdx/baseclasses/streams.h new file mode 100644 index 0000000000..f5c9f60764 --- /dev/null +++ b/plugins/GSdx/baseclasses/streams.h @@ -0,0 +1,254 @@ +//------------------------------------------------------------------------------ +// File: Streams.h +// +// Desc: DirectShow base classes - defines overall streams architecture. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __STREAMS__ +#define __STREAMS__ + +#ifdef _MSC_VER +// disable some level-4 warnings, use #pragma warning(enable:###) to re-enable +#pragma warning(disable:4100) // warning C4100: unreferenced formal parameter +#pragma warning(disable:4127) // warning C4127: conditional expression is constant +#pragma warning(disable:4189) // warning C4189: local variable is initialized but not referenced +#pragma warning(disable:4201) // warning C4201: nonstandard extension used : nameless struct/union +#pragma warning(disable:4511) // warning C4511: copy constructor could not be generated +#pragma warning(disable:4512) // warning C4512: assignment operator could not be generated +#pragma warning(disable:4514) // warning C4514: unreferenced inline function has been removed +#pragma warning(disable:4710) // warning C4710: 'function' not inlined + +#if _MSC_VER>=1100 +#define AM_NOVTABLE __declspec(novtable) +#else +#define AM_NOVTABLE +#endif +#endif // MSC_VER + +// Because of differences between Visual C++ and older Microsoft SDKs, +// you may have defined _DEBUG without defining DEBUG. This logic +// ensures that both will be set if Visual C++ sets _DEBUG. +#ifdef _DEBUG +#ifndef DEBUG +#define DEBUG +#endif +#endif + +#include +#include +#include +#include + +// Disable warning message for C4201 - use of nameless struct/union +// Otherwise, strmif.h will generate warnings for Win32 debug builds +#pragma warning( disable : 4201 ) + +#include + +#ifndef NUMELMS + #define NUMELMS(aa) (sizeof(aa)/sizeof((aa)[0])) +#endif + +/////////////////////////////////////////////////////////////////////////// +// The following definitions come from the Platform SDK and are required if +// the applicaiton is being compiled with the headers from Visual C++ 6.0. +/////////////////////////////////////////////////////////////////////////// +#ifndef InterlockedExchangePointer + #define InterlockedExchangePointer(Target, Value) \ + (PVOID)InterlockedExchange((PLONG)(Target), (LONG)(Value)) +#endif + +#ifndef _WAVEFORMATEXTENSIBLE_ +#define _WAVEFORMATEXTENSIBLE_ +typedef struct { + WAVEFORMATEX Format; + union { + WORD wValidBitsPerSample; /* bits of precision */ + WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */ + WORD wReserved; /* If neither applies, set to zero. */ + } Samples; + DWORD dwChannelMask; /* which channels are */ + /* present in stream */ + GUID SubFormat; +} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE; +#endif // !_WAVEFORMATEXTENSIBLE_ + +#if !defined(WAVE_FORMAT_EXTENSIBLE) +#define WAVE_FORMAT_EXTENSIBLE 0xFFFE +#endif // !defined(WAVE_FORMAT_EXTENSIBLE) + +#ifndef GetWindowLongPtr + #define GetWindowLongPtrA GetWindowLongA + #define GetWindowLongPtrW GetWindowLongW + #ifdef UNICODE + #define GetWindowLongPtr GetWindowLongPtrW + #else + #define GetWindowLongPtr GetWindowLongPtrA + #endif // !UNICODE +#endif // !GetWindowLongPtr + +#ifndef SetWindowLongPtr + #define SetWindowLongPtrA SetWindowLongA + #define SetWindowLongPtrW SetWindowLongW + #ifdef UNICODE + #define SetWindowLongPtr SetWindowLongPtrW + #else + #define SetWindowLongPtr SetWindowLongPtrA + #endif // !UNICODE +#endif // !SetWindowLongPtr + +#ifndef GWLP_WNDPROC + #define GWLP_WNDPROC (-4) +#endif +#ifndef GWLP_HINSTANCE + #define GWLP_HINSTANCE (-6) +#endif +#ifndef GWLP_HWNDPARENT + #define GWLP_HWNDPARENT (-8) +#endif +#ifndef GWLP_USERDATA + #define GWLP_USERDATA (-21) +#endif +#ifndef GWLP_ID + #define GWLP_ID (-12) +#endif +#ifndef DWLP_MSGRESULT + #define DWLP_MSGRESULT 0 +#endif +#ifndef DWLP_DLGPROC + #define DWLP_DLGPROC DWLP_MSGRESULT + sizeof(LRESULT) +#endif +#ifndef DWLP_USER + #define DWLP_USER DWLP_DLGPROC + sizeof(DLGPROC) +#endif +/////////////////////////////////////////////////////////////////////////// +// End Platform SDK definitions +/////////////////////////////////////////////////////////////////////////// + +#include + +#pragma warning(disable:4201) // warning C4201: nonstandard extension used : nameless struct/union +#include // Generated IDL header file for streams interfaces + +#include "reftime.h" // Helper class for REFERENCE_TIME management +#include "wxdebug.h" // Debug support for logging and ASSERTs +#include "amvideo.h" // ActiveMovie video interfaces and definitions +//include amaudio.h explicitly if you need it. it requires the DirectX SDK. +//#include "amaudio.h" // ActiveMovie audio interfaces and definitions +#include "wxutil.h" // General helper classes for threads etc +#include "combase.h" // Base COM classes to support IUnknown +//#include "dllsetup.h" // Filter registration support functions +#include "measure.h" // Performance measurement +#include "comlite.h" // Light weight com function prototypes + +#include "cache.h" // Simple cache container class +#include "wxlist.h" // Non MFC generic list class +#include "msgthrd.h" // CMsgThread +#include "mtype.h" // Helper class for managing media types +#include "fourcc.h" // conversions between FOURCCs and GUIDs +#include "control.h" // generated from control.odl +#include "ctlutil.h" // control interface utility classes +#include "evcode.h" // event code definitions +#include "amfilter.h" // Main streams architecture class hierachy +#include "transfrm.h" // Generic transform filter +#include "transip.h" // Generic transform-in-place filter +#include "uuids.h" // declaration of type GUIDs and well-known clsids +#include "source.h" // Generic source filter +#include "outputq.h" // Output pin queueing +#include "errors.h" // HRESULT status and error definitions +#include "renbase.h" // Base class for writing ActiveX renderers +//#include "winutil.h" // Helps with filters that manage windows +//#include "winctrl.h" // Implements the IVideoWindow interface +//#include "videoctl.h" // Specifically video related classes +#include "refclock.h" // Base clock class +#include "sysclock.h" // System clock +#include "pstream.h" // IPersistStream helper class +#include "vtrans.h" // Video Transform Filter base class +#include "amextra.h" +//#include "cprop.h" // Base property page class +#include "strmctl.h" // IAMStreamControl support +#include "edevdefs.h" // External device control interface defines +#include "audevcod.h" // audio filter device error event codes + +#include + +#define NO_SHLWAPI_STRFCNS +#include +#include + +#ifndef NUMELMS + #define NUMELMS(aa) (sizeof(aa)/sizeof((aa)[0])) +#endif + +class CPinInfo : public PIN_INFO +{ +public: + CPinInfo() {pFilter = NULL;} + ~CPinInfo() {if(pFilter) pFilter->Release();} +}; + +class CFilterInfo : public FILTER_INFO +{ +public: + CFilterInfo() {pGraph = NULL;} + ~CFilterInfo() {if(pGraph) pGraph->Release();} +}; + +#define BeginEnumFilters(pFilterGraph, pEnumFilters, pBaseFilter) \ + {CComPtr pEnumFilters; \ + if(pFilterGraph && SUCCEEDED(pFilterGraph->EnumFilters(&pEnumFilters))) \ + { \ + for(CComPtr pBaseFilter; S_OK == pEnumFilters->Next(1, &pBaseFilter, 0); pBaseFilter = NULL) \ + { \ + +#define EndEnumFilters }}} + +#define BeginEnumCachedFilters(pGraphConfig, pEnumFilters, pBaseFilter) \ + {CComPtr pEnumFilters; \ + if(pGraphConfig && SUCCEEDED(pGraphConfig->EnumCacheFilter(&pEnumFilters))) \ + { \ + for(CComPtr pBaseFilter; S_OK == pEnumFilters->Next(1, &pBaseFilter, 0); pBaseFilter = NULL) \ + { \ + +#define EndEnumCachedFilters }}} + +#define BeginEnumPins(pBaseFilter, pEnumPins, pPin) \ + {CComPtr pEnumPins; \ + if(pBaseFilter && SUCCEEDED(pBaseFilter->EnumPins(&pEnumPins))) \ + { \ + for(CComPtr pPin; S_OK == pEnumPins->Next(1, &pPin, 0); pPin = NULL) \ + { \ + +#define EndEnumPins }}} + +#define BeginEnumMediaTypes(pPin, pEnumMediaTypes, pMediaType) \ + {CComPtr pEnumMediaTypes; \ + if(pPin && SUCCEEDED(pPin->EnumMediaTypes(&pEnumMediaTypes))) \ + { \ + AM_MEDIA_TYPE* pMediaType = NULL; \ + for(; S_OK == pEnumMediaTypes->Next(1, &pMediaType, NULL); DeleteMediaType(pMediaType), pMediaType = NULL) \ + { \ + +#define EndEnumMediaTypes(pMediaType) } if(pMediaType) DeleteMediaType(pMediaType); }} + +#define BeginEnumSysDev(clsid, pMoniker) \ + {CComPtr pDevEnum4$##clsid; \ + pDevEnum4$##clsid.CoCreateInstance(CLSID_SystemDeviceEnum); \ + CComPtr pClassEnum4$##clsid; \ + if(SUCCEEDED(pDevEnum4$##clsid->CreateClassEnumerator(clsid, &pClassEnum4$##clsid, 0)) \ + && pClassEnum4$##clsid) \ + { \ + for(CComPtr pMoniker; pClassEnum4$##clsid->Next(1, &pMoniker, 0) == S_OK; pMoniker = NULL) \ + { \ + +#define EndEnumSysDev }}} + +#else + #ifdef DEBUG + #pragma message("STREAMS.H included TWICE") + #endif +#endif // __STREAMS__ + diff --git a/plugins/GSdx/baseclasses/strmctl.cpp b/plugins/GSdx/baseclasses/strmctl.cpp new file mode 100644 index 0000000000..f0373d4869 --- /dev/null +++ b/plugins/GSdx/baseclasses/strmctl.cpp @@ -0,0 +1,401 @@ +//------------------------------------------------------------------------------ +// File: StrmCtl.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include "strmctl.h" + +CBaseStreamControl::CBaseStreamControl() +: m_StreamState(STREAM_FLOWING) +, m_StreamStateOnStop(STREAM_FLOWING) // means no pending stop +, m_tStartTime(MAX_TIME) +, m_tStopTime(MAX_TIME) +, m_dwStartCookie(0) +, m_dwStopCookie(0) +, m_pRefClock(NULL) +, m_FilterState(State_Stopped) +, m_bIsFlushing(FALSE) +, m_bStopSendExtra(FALSE) +{} + +CBaseStreamControl::~CBaseStreamControl() +{ + // Make sure we release the clock. + SetSyncSource(NULL); + return; +} + + +STDMETHODIMP CBaseStreamControl::StopAt(const REFERENCE_TIME * ptStop, BOOL bSendExtra, DWORD dwCookie) +{ + CAutoLock lck(&m_CritSec); + m_bStopSendExtra = FALSE; // reset + m_bStopExtraSent = FALSE; + if (ptStop) + { + if (*ptStop == MAX_TIME) + { + DbgLog((LOG_TRACE,2,TEXT("StopAt: Cancel stop"))); + CancelStop(); + // If there's now a command to start in the future, we assume + // they want to be stopped when the graph is first run + if (m_FilterState == State_Stopped && m_tStartTime < MAX_TIME) { + m_StreamState = STREAM_DISCARDING; + DbgLog((LOG_TRACE,2,TEXT("graph will begin by DISCARDING"))); + } + return NOERROR; + } + DbgLog((LOG_TRACE,2,TEXT("StopAt: %dms extra=%d"), + (int)(*ptStop/10000), bSendExtra)); + // if the first command is to stop in the future, then we assume they + // want to be started when the graph is first run + if (m_FilterState == State_Stopped && m_tStartTime > *ptStop) { + m_StreamState = STREAM_FLOWING; + DbgLog((LOG_TRACE,2,TEXT("graph will begin by FLOWING"))); + } + m_bStopSendExtra = bSendExtra; + m_tStopTime = *ptStop; + m_dwStopCookie = dwCookie; + m_StreamStateOnStop = STREAM_DISCARDING; + } + else + { + DbgLog((LOG_TRACE,2,TEXT("StopAt: now"))); + // sending an extra frame when told to stop now would mess people up + m_bStopSendExtra = FALSE; + m_tStopTime = MAX_TIME; + m_dwStopCookie = 0; + m_StreamState = STREAM_DISCARDING; + m_StreamStateOnStop = STREAM_FLOWING; // no pending stop + } + // we might change our mind what to do with a sample we're blocking + m_StreamEvent.Set(); + return NOERROR; +} + +STDMETHODIMP CBaseStreamControl::StartAt +( const REFERENCE_TIME *ptStart, DWORD dwCookie ) +{ + CAutoLock lck(&m_CritSec); + if (ptStart) + { + if (*ptStart == MAX_TIME) + { + DbgLog((LOG_TRACE,2,TEXT("StartAt: Cancel start"))); + CancelStart(); + // If there's now a command to stop in the future, we assume + // they want to be started when the graph is first run + if (m_FilterState == State_Stopped && m_tStopTime < MAX_TIME) { + DbgLog((LOG_TRACE,2,TEXT("graph will begin by FLOWING"))); + m_StreamState = STREAM_FLOWING; + } + return NOERROR; + } + DbgLog((LOG_TRACE,2,TEXT("StartAt: %dms"), (int)(*ptStart/10000))); + // if the first command is to start in the future, then we assume they + // want to be stopped when the graph is first run + if (m_FilterState == State_Stopped && m_tStopTime >= *ptStart) { + DbgLog((LOG_TRACE,2,TEXT("graph will begin by DISCARDING"))); + m_StreamState = STREAM_DISCARDING; + } + m_tStartTime = *ptStart; + m_dwStartCookie = dwCookie; + // if (m_tStopTime == m_tStartTime) CancelStop(); + } + else + { + DbgLog((LOG_TRACE,2,TEXT("StartAt: now"))); + m_tStartTime = MAX_TIME; + m_dwStartCookie = 0; + m_StreamState = STREAM_FLOWING; + } + // we might change our mind what to do with a sample we're blocking + m_StreamEvent.Set(); + return NOERROR; +} + +// Retrieve information about current settings +STDMETHODIMP CBaseStreamControl::GetInfo(AM_STREAM_INFO *pInfo) +{ + if (pInfo == NULL) + return E_POINTER; + + pInfo->tStart = m_tStartTime; + pInfo->tStop = m_tStopTime; + pInfo->dwStartCookie = m_dwStartCookie; + pInfo->dwStopCookie = m_dwStopCookie; + pInfo->dwFlags = m_bStopSendExtra ? AM_STREAM_INFO_STOP_SEND_EXTRA : 0; + pInfo->dwFlags |= m_tStartTime == MAX_TIME ? 0 : AM_STREAM_INFO_START_DEFINED; + pInfo->dwFlags |= m_tStopTime == MAX_TIME ? 0 : AM_STREAM_INFO_STOP_DEFINED; + switch (m_StreamState) { + default: + DbgBreak("Invalid stream state"); + case STREAM_FLOWING: + break; + case STREAM_DISCARDING: + pInfo->dwFlags |= AM_STREAM_INFO_DISCARDING; + break; + } + return S_OK; +} + + +void CBaseStreamControl::ExecuteStop() +{ + ASSERT(CritCheckIn(&m_CritSec)); + m_StreamState = m_StreamStateOnStop; + if (m_dwStopCookie && m_pSink) { + DbgLog((LOG_TRACE,2,TEXT("*sending EC_STREAM_CONTROL_STOPPED (%d)"), + m_dwStopCookie)); + m_pSink->Notify(EC_STREAM_CONTROL_STOPPED, (LONG_PTR)this, m_dwStopCookie); + } + CancelStop(); // This will do the tidy up +} + +void CBaseStreamControl::ExecuteStart() +{ + ASSERT(CritCheckIn(&m_CritSec)); + m_StreamState = STREAM_FLOWING; + if (m_dwStartCookie) { + DbgLog((LOG_TRACE,2,TEXT("*sending EC_STREAM_CONTROL_STARTED (%d)"), + m_dwStartCookie)); + m_pSink->Notify(EC_STREAM_CONTROL_STARTED, (LONG_PTR)this, m_dwStartCookie); + } + CancelStart(); // This will do the tidy up +} + +void CBaseStreamControl::CancelStop() +{ + ASSERT(CritCheckIn(&m_CritSec)); + m_tStopTime = MAX_TIME; + m_dwStopCookie = 0; + m_StreamStateOnStop = STREAM_FLOWING; +} + +void CBaseStreamControl::CancelStart() +{ + ASSERT(CritCheckIn(&m_CritSec)); + m_tStartTime = MAX_TIME; + m_dwStartCookie = 0; +} + + +// This guy will return one of the three StreamControlState's. Here's what the caller +// should do for each one: +// +// STREAM_FLOWING: Proceed as usual (render or pass the sample on) +// STREAM_DISCARDING: Calculate the time 'til *pSampleStart and wait that long +// for the event handle (GetStreamEventHandle()). If the +// wait expires, throw the sample away. If the event +// fires, call me back, I've changed my mind. +// I use pSampleStart (not Stop) so that live sources don't +// block for the duration of their samples, since the clock +// will always read approximately pSampleStart when called + + +// All through this code, you'll notice the following rules: +// - When start and stop time are the same, it's as if start was first +// - An event is considered inside the sample when it's >= sample start time +// but < sample stop time +// - if any part of the sample is supposed to be sent, we'll send the whole +// thing since we don't break it into smaller pieces +// - If we skip over a start or stop without doing it, we still signal the event +// and reset ourselves in case somebody's waiting for the event, and to make +// sure we notice that the event is past and should be forgotten +// Here are the 19 cases that have to be handled (x=start o=stop <-->=sample): +// +// 1. xo<--> start then stop +// 2. ox<--> stop then start +// 3. x start +// 4. o stop then start +// 5. x<-->o start +// 6. o<-->x stop +// 7. o start +// 8. x no change +// 9. start +// 10. stop then start +// 11. <-->xo no change +// 12. <-->ox no change +// 13. x<--> start +// 14. start +// 15. <-->x no change +// 16. o<--> stop +// 17. no change +// 18. <-->o no change +// 19. <--> no change + + +enum CBaseStreamControl::StreamControlState CBaseStreamControl::CheckSampleTimes +( const REFERENCE_TIME * pSampleStart, const REFERENCE_TIME * pSampleStop ) +{ + CAutoLock lck(&m_CritSec); + + ASSERT(!m_bIsFlushing); + ASSERT(pSampleStart && pSampleStop); + + // Don't ask me how I came up with the code below to handle all 19 cases + // - DannyMi + + if (m_tStopTime >= *pSampleStart) + { + if (m_tStartTime >= *pSampleStop) + return m_StreamState; // cases 8 11 12 15 17 18 19 + if (m_tStopTime < m_tStartTime) + ExecuteStop(); // case 10 + ExecuteStart(); // cases 3 5 7 9 13 14 + return m_StreamState; + } + + if (m_tStartTime >= *pSampleStop) + { + ExecuteStop(); // cases 6 16 + return m_StreamState; + } + + if (m_tStartTime <= m_tStopTime) + { + ExecuteStart(); + ExecuteStop(); + return m_StreamState; // case 1 + } + else + { + ExecuteStop(); + ExecuteStart(); + return m_StreamState; // cases 2 4 + } +} + + +enum CBaseStreamControl::StreamControlState CBaseStreamControl::CheckStreamState( IMediaSample * pSample ) +{ + + REFERENCE_TIME rtBufferStart, rtBufferStop; + const BOOL bNoBufferTimes = + pSample == NULL || + FAILED(pSample->GetTime(&rtBufferStart, &rtBufferStop)); + + StreamControlState state; + LONG lWait; + + do + { + // something has to break out of the blocking + if (m_bIsFlushing || m_FilterState == State_Stopped) + return STREAM_DISCARDING; + + if (bNoBufferTimes) { + // Can't do anything until we get a time stamp + state = m_StreamState; + break; + } else { + state = CheckSampleTimes( &rtBufferStart, &rtBufferStop ); + if (state == STREAM_FLOWING) + break; + + // we aren't supposed to send this, but we've been + // told to send one more than we were supposed to + // (and the stop isn't still pending and we're streaming) + if (m_bStopSendExtra && !m_bStopExtraSent && + m_tStopTime == MAX_TIME && + m_FilterState != State_Stopped) { + m_bStopExtraSent = TRUE; + DbgLog((LOG_TRACE,2,TEXT("%d sending an EXTRA frame"), + m_dwStopCookie)); + state = STREAM_FLOWING; + break; + } + } + + // We're in discarding mode + + // If we've no clock, discard as fast as we can + if (!m_pRefClock) { + break; + + // If we're paused, we can't discard in a timely manner because + // there's no such thing as stream times. We must block until + // we run or stop, or we'll end up throwing the whole stream away + // as quickly as possible + } else if (m_FilterState == State_Paused) { + lWait = INFINITE; + + } else { + // wait until it's time for the sample until we say "discard" + // ("discard in a timely fashion") + REFERENCE_TIME rtNow; + EXECUTE_ASSERT(SUCCEEDED(m_pRefClock->GetTime(&rtNow))); + rtNow -= m_tRunStart; // Into relative ref-time + lWait = LONG((rtBufferStart - rtNow)/10000); // 100ns -> ms + if (lWait < 10) break; // Not worth waiting - discard early + } + + } while(WaitForSingleObject(GetStreamEventHandle(), lWait) != WAIT_TIMEOUT); + + return state; +} + + +void CBaseStreamControl::NotifyFilterState( FILTER_STATE new_state, REFERENCE_TIME tStart ) +{ + CAutoLock lck(&m_CritSec); + + // or we will get confused + if (m_FilterState == new_state) + return; + + switch (new_state) + { + case State_Stopped: + + DbgLog((LOG_TRACE,2,TEXT("Filter is STOPPED"))); + + // execute any pending starts and stops in the right order, + // to make sure all notifications get sent, and we end up + // in the right state to begin next time (??? why not?) + + if (m_tStartTime != MAX_TIME && m_tStopTime == MAX_TIME) { + ExecuteStart(); + } else if (m_tStopTime != MAX_TIME && m_tStartTime == MAX_TIME) { + ExecuteStop(); + } else if (m_tStopTime != MAX_TIME && m_tStartTime != MAX_TIME) { + if (m_tStartTime <= m_tStopTime) { + ExecuteStart(); + ExecuteStop(); + } else { + ExecuteStop(); + ExecuteStart(); + } + } + // always start off flowing when the graph starts streaming + // unless told otherwise + m_StreamState = STREAM_FLOWING; + m_FilterState = new_state; + break; + + case State_Running: + + DbgLog((LOG_TRACE,2,TEXT("Filter is RUNNING"))); + + m_tRunStart = tStart; + // fall-through + + default: // case State_Paused: + m_FilterState = new_state; + } + // unblock! + m_StreamEvent.Set(); +} + + +void CBaseStreamControl::Flushing(BOOL bInProgress) +{ + CAutoLock lck(&m_CritSec); + m_bIsFlushing = bInProgress; + m_StreamEvent.Set(); +} diff --git a/plugins/GSdx/baseclasses/strmctl.h b/plugins/GSdx/baseclasses/strmctl.h new file mode 100644 index 0000000000..2a1475fe3a --- /dev/null +++ b/plugins/GSdx/baseclasses/strmctl.h @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// File: StrmCtl.h +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __strmctl_h__ +#define __strmctl_h__ + +class CBaseStreamControl : public IAMStreamControl +{ +public: + // Used by the implementation + enum StreamControlState + { STREAM_FLOWING = 0x1000, + STREAM_DISCARDING + }; + +private: + enum StreamControlState m_StreamState; // Current stream state + enum StreamControlState m_StreamStateOnStop; // State after next stop + // (i.e.Blocking or Discarding) + + REFERENCE_TIME m_tStartTime; // MAX_TIME implies none + REFERENCE_TIME m_tStopTime; // MAX_TIME implies none + DWORD m_dwStartCookie; // Cookie for notification to app + DWORD m_dwStopCookie; // Cookie for notification to app + volatile BOOL m_bIsFlushing; // No optimization pls! + volatile BOOL m_bStopSendExtra; // bSendExtra was set + volatile BOOL m_bStopExtraSent; // the extra one was sent + + CCritSec m_CritSec; // CritSec to guard above attributes + + // Event to fire when we can come + // out of blocking, or to come out of waiting + // to discard if we change our minds. + // + CAMEvent m_StreamEvent; + + // All of these methods execute immediately. Helpers for others. + // + void ExecuteStop(); + void ExecuteStart(); + void CancelStop(); + void CancelStart(); + + // Some things we need to be told by our owning filter + // Your pin must also expose IAMStreamControl when QI'd for it! + // + IReferenceClock * m_pRefClock; // Need it to set advises + // Filter must tell us via + // SetSyncSource + IMediaEventSink * m_pSink; // Event sink + // Filter must tell us after it + // creates it in JoinFilterGraph() + FILTER_STATE m_FilterState; // Just need it! + // Filter must tell us via + // NotifyFilterState + REFERENCE_TIME m_tRunStart; // Per the Run call to the filter + + // This guy will return one of the three StreamControlState's. Here's what + // the caller should do for each one: + // + // STREAM_FLOWING: Proceed as usual (render or pass the sample on) + // STREAM_DISCARDING: Calculate the time 'til *pSampleStop and wait + // that long for the event handle + // (GetStreamEventHandle()). If the wait + // expires, throw the sample away. If the event + // fires, call me back - I've changed my mind. + // + enum StreamControlState CheckSampleTimes( const REFERENCE_TIME * pSampleStart, + const REFERENCE_TIME * pSampleStop ); + +public: + // You don't have to tell us much when we're created, but there are other + // obligations that must be met. See SetSyncSource & NotifyFilterState + // below. + // + CBaseStreamControl(); + ~CBaseStreamControl(); + + // If you want this class to work properly, there are thing you need to + // (keep) telling it. Filters with pins that use this class + // should ensure that they pass through to this method any calls they + // receive on their SetSyncSource. + + // We need a clock to see what time it is. This is for the + // "discard in a timely fashion" logic. If we discard everything as + // quick as possible, a whole 60 minute file could get discarded in the + // first 10 seconds, and if somebody wants to turn streaming on at 30 + // minutes into the file, and they make the call more than a few seconds + // after the graph is run, it may be too late! + // So we hold every sample until it's time has gone, then we discard it. + // The filter should call this when it gets a SetSyncSource + // + void SetSyncSource( IReferenceClock * pRefClock ) + { + CAutoLock lck(&m_CritSec); + if (m_pRefClock) m_pRefClock->Release(); + m_pRefClock = pRefClock; + if (m_pRefClock) m_pRefClock->AddRef(); + } + + // Set event sink for notifications + // The filter should call this in its JoinFilterGraph after it creates the + // IMediaEventSink + // + void SetFilterGraph( IMediaEventSink *pSink ) { + m_pSink = pSink; + } + + // Since we schedule in stream time, we need the tStart and must track the + // state of our owning filter. + // The app should call this ever state change + // + void NotifyFilterState( FILTER_STATE new_state, REFERENCE_TIME tStart = 0 ); + + // Filter should call Flushing(TRUE) in BeginFlush, + // and Flushing(FALSE) in EndFlush. + // + void Flushing( BOOL bInProgress ); + + + // The two main methods of IAMStreamControl + + // Class adds default values suitable for immediate + // muting and unmuting of the stream. + + STDMETHODIMP StopAt( const REFERENCE_TIME * ptStop = NULL, + BOOL bSendExtra = FALSE, + DWORD dwCookie = 0 ); + STDMETHODIMP StartAt( const REFERENCE_TIME * ptStart = NULL, + DWORD dwCookie = 0 ); + STDMETHODIMP GetInfo( AM_STREAM_INFO *pInfo); + + // Helper function for pin's receive method. Call this with + // the sample and we'll tell you what to do with it. We'll do a + // WaitForSingleObject within this call if one is required. This is + // a "What should I do with this sample?" kind of call. We'll tell the + // caller to either flow it or discard it. + // If pSample is NULL we evaluate based on the current state + // settings + enum StreamControlState CheckStreamState( IMediaSample * pSample ); + +private: + // These don't require locking, but we are relying on the fact that + // m_StreamState can be retrieved with integrity, and is a snap shot that + // may have just been, or may be just about to be, changed. + HANDLE GetStreamEventHandle() const { return m_StreamEvent; } + enum StreamControlState GetStreamState() const { return m_StreamState; } + BOOL IsStreaming() const { return m_StreamState == STREAM_FLOWING; } +}; + +#endif diff --git a/plugins/GSdx/baseclasses/sysclock.cpp b/plugins/GSdx/baseclasses/sysclock.cpp new file mode 100644 index 0000000000..a3cb59e40b --- /dev/null +++ b/plugins/GSdx/baseclasses/sysclock.cpp @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// File: SysClock.cpp +// +// Desc: DirectShow base classes - implements a system clock based on +// IReferenceClock. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include + + +#ifdef FILTER_DLL + +/* List of class IDs and creator functions for the class factory. This + provides the link between the OLE entry point in the DLL and an object + being created. The class factory will call the static CreateInstance + function when it is asked to create a CLSID_SystemClock object */ + +CFactoryTemplate g_Templates[1] = { + {&CLSID_SystemClock, CSystemClock::CreateInstance} +}; + +int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]); +#endif + +/* This goes in the factory template table to create new instances */ +CUnknown * WINAPI CSystemClock::CreateInstance(LPUNKNOWN pUnk,HRESULT *phr) +{ + return new CSystemClock(NAME("System reference clock"),pUnk, phr); +} + + +CSystemClock::CSystemClock(TCHAR *pName,LPUNKNOWN pUnk,HRESULT *phr) : + CBaseReferenceClock(pName, pUnk, phr) +{ +} + +STDMETHODIMP CSystemClock::NonDelegatingQueryInterface( + REFIID riid, + void ** ppv) +{ + if (riid == IID_IPersist) + { + return GetInterface(static_cast(this), ppv); + } + else if (riid == IID_IAMClockAdjust) + { + return GetInterface(static_cast(this), ppv); + } + else + { + return CBaseReferenceClock::NonDelegatingQueryInterface(riid, ppv); + } +} + +/* Return the clock's clsid */ +STDMETHODIMP +CSystemClock::GetClassID(CLSID *pClsID) +{ + CheckPointer(pClsID,E_POINTER); + ValidateReadWritePtr(pClsID,sizeof(CLSID)); + *pClsID = CLSID_SystemClock; + return NOERROR; +} + + +STDMETHODIMP +CSystemClock::SetClockDelta(REFERENCE_TIME rtDelta) +{ + return SetTimeDelta(rtDelta); +} diff --git a/plugins/GSdx/baseclasses/sysclock.h b/plugins/GSdx/baseclasses/sysclock.h new file mode 100644 index 0000000000..53c8e4cefe --- /dev/null +++ b/plugins/GSdx/baseclasses/sysclock.h @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// File: SysClock.h +// +// Desc: DirectShow base classes - defines a system clock implementation of +// IReferenceClock. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __SYSTEMCLOCK__ +#define __SYSTEMCLOCK__ + +// +// Base clock. Uses timeGetTime ONLY +// Uses most of the code in the base reference clock. +// Provides GetTime +// + +class CSystemClock : public CBaseReferenceClock, public IAMClockAdjust, public IPersist +{ +public: + // We must be able to create an instance of ourselves + static CUnknown * WINAPI CreateInstance(LPUNKNOWN pUnk, HRESULT *phr); + CSystemClock(TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr); + + DECLARE_IUNKNOWN + + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,void ** ppv); + + // Yield up our class id so that we can be persisted + // Implement required Ipersist method + STDMETHODIMP GetClassID(CLSID *pClsID); + + // IAMClockAdjust methods + STDMETHODIMP SetClockDelta(REFERENCE_TIME rtDelta); +}; //CSystemClock + +#endif /* __SYSTEMCLOCK__ */ diff --git a/plugins/GSdx/baseclasses/transfrm.cpp b/plugins/GSdx/baseclasses/transfrm.cpp new file mode 100644 index 0000000000..2fc6e141aa --- /dev/null +++ b/plugins/GSdx/baseclasses/transfrm.cpp @@ -0,0 +1,1016 @@ +//------------------------------------------------------------------------------ +// File: Transfrm.cpp +// +// Desc: DirectShow base classes - implements class for simple transform +// filters such as video decompressors. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include "measure.h" + + +// ================================================================= +// Implements the CTransformFilter class +// ================================================================= + +CTransformFilter::CTransformFilter(TCHAR *pName, + LPUNKNOWN pUnk, + REFCLSID clsid) : + CBaseFilter(pName,pUnk,&m_csFilter, clsid), + m_pInput(NULL), + m_pOutput(NULL), + m_bEOSDelivered(FALSE), + m_bQualityChanged(FALSE), + m_bSampleSkipped(FALSE) +{ +#ifdef PERF + RegisterPerfId(); +#endif // PERF +} + +#ifdef UNICODE +CTransformFilter::CTransformFilter(char *pName, + LPUNKNOWN pUnk, + REFCLSID clsid) : + CBaseFilter(pName,pUnk,&m_csFilter, clsid), + m_pInput(NULL), + m_pOutput(NULL), + m_bEOSDelivered(FALSE), + m_bQualityChanged(FALSE), + m_bSampleSkipped(FALSE) +{ +#ifdef PERF + RegisterPerfId(); +#endif // PERF +} +#endif + +// destructor + +CTransformFilter::~CTransformFilter() +{ + // Delete the pins + + delete m_pInput; + delete m_pOutput; +} + + +// Transform place holder - should never be called +HRESULT CTransformFilter::Transform(IMediaSample * pIn, IMediaSample *pOut) +{ + UNREFERENCED_PARAMETER(pIn); + UNREFERENCED_PARAMETER(pOut); + DbgBreak("CTransformFilter::Transform() should never be called"); + return E_UNEXPECTED; +} + + +// return the number of pins we provide + +int CTransformFilter::GetPinCount() +{ + return 2; +} + + +// return a non-addrefed CBasePin * for the user to addref if he holds onto it +// for longer than his pointer to us. We create the pins dynamically when they +// are asked for rather than in the constructor. This is because we want to +// give the derived class an oppportunity to return different pin objects + +// We return the objects as and when they are needed. If either of these fails +// then we return NULL, the assumption being that the caller will realise the +// whole deal is off and destroy us - which in turn will delete everything. + +CBasePin * +CTransformFilter::GetPin(int n) +{ + HRESULT hr = S_OK; + + // Create an input pin if necessary + + if (m_pInput == NULL) { + + m_pInput = new CTransformInputPin(NAME("Transform input pin"), + this, // Owner filter + &hr, // Result code + L"XForm In"); // Pin name + + + // Can't fail + ASSERT(SUCCEEDED(hr)); + if (m_pInput == NULL) { + return NULL; + } + m_pOutput = (CTransformOutputPin *) + new CTransformOutputPin(NAME("Transform output pin"), + this, // Owner filter + &hr, // Result code + L"XForm Out"); // Pin name + + + // Can't fail + ASSERT(SUCCEEDED(hr)); + if (m_pOutput == NULL) { + delete m_pInput; + m_pInput = NULL; + } + } + + // Return the appropriate pin + + if (n == 0) { + return m_pInput; + } else + if (n == 1) { + return m_pOutput; + } else { + return NULL; + } +} + + +// +// FindPin +// +// If Id is In or Out then return the IPin* for that pin +// creating the pin if need be. Otherwise return NULL with an error. + +STDMETHODIMP CTransformFilter::FindPin(LPCWSTR Id, IPin **ppPin) +{ + CheckPointer(ppPin,E_POINTER); + ValidateReadWritePtr(ppPin,sizeof(IPin *)); + + if (0==lstrcmpW(Id,L"In")) { + *ppPin = GetPin(0); + } else if (0==lstrcmpW(Id,L"Out")) { + *ppPin = GetPin(1); + } else { + *ppPin = NULL; + return VFW_E_NOT_FOUND; + } + + HRESULT hr = NOERROR; + // AddRef() returned pointer - but GetPin could fail if memory is low. + if (*ppPin) { + (*ppPin)->AddRef(); + } else { + hr = E_OUTOFMEMORY; // probably. There's no pin anyway. + } + return hr; +} + + +// override these two functions if you want to inform something +// about entry to or exit from streaming state. + +HRESULT +CTransformFilter::StartStreaming() +{ + return NOERROR; +} + + +HRESULT +CTransformFilter::StopStreaming() +{ + return NOERROR; +} + + +// override this to grab extra interfaces on connection + +HRESULT +CTransformFilter::CheckConnect(PIN_DIRECTION dir,IPin *pPin) +{ + UNREFERENCED_PARAMETER(dir); + UNREFERENCED_PARAMETER(pPin); + return NOERROR; +} + + +// place holder to allow derived classes to release any extra interfaces + +HRESULT +CTransformFilter::BreakConnect(PIN_DIRECTION dir) +{ + UNREFERENCED_PARAMETER(dir); + return NOERROR; +} + + +// Let derived classes know about connection completion + +HRESULT +CTransformFilter::CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin) +{ + UNREFERENCED_PARAMETER(direction); + UNREFERENCED_PARAMETER(pReceivePin); + return NOERROR; +} + + +// override this to know when the media type is really set + +HRESULT +CTransformFilter::SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt) +{ + UNREFERENCED_PARAMETER(direction); + UNREFERENCED_PARAMETER(pmt); + return NOERROR; +} + + +// Set up our output sample +HRESULT +CTransformFilter::InitializeOutputSample(IMediaSample *pSample, IMediaSample **ppOutSample) +{ + IMediaSample *pOutSample; + + // default - times are the same + + AM_SAMPLE2_PROPERTIES * const pProps = m_pInput->SampleProps(); + DWORD dwFlags = m_bSampleSkipped ? AM_GBF_PREVFRAMESKIPPED : 0; + + // This will prevent the image renderer from switching us to DirectDraw + // when we can't do it without skipping frames because we're not on a + // keyframe. If it really has to switch us, it still will, but then we + // will have to wait for the next keyframe + if (!(pProps->dwSampleFlags & AM_SAMPLE_SPLICEPOINT)) { + dwFlags |= AM_GBF_NOTASYNCPOINT; + } + + ASSERT(m_pOutput->m_pAllocator != NULL); + HRESULT hr = m_pOutput->m_pAllocator->GetBuffer( + &pOutSample + , pProps->dwSampleFlags & AM_SAMPLE_TIMEVALID ? + &pProps->tStart : NULL + , pProps->dwSampleFlags & AM_SAMPLE_STOPVALID ? + &pProps->tStop : NULL + , dwFlags + ); + *ppOutSample = pOutSample; + if (FAILED(hr)) { + return hr; + } + + ASSERT(pOutSample); + IMediaSample2 *pOutSample2; + if (SUCCEEDED(pOutSample->QueryInterface(IID_IMediaSample2, + (void **)&pOutSample2))) { + /* Modify it */ + AM_SAMPLE2_PROPERTIES OutProps; + EXECUTE_ASSERT(SUCCEEDED(pOutSample2->GetProperties( + FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, tStart), (PBYTE)&OutProps) + )); + OutProps.dwTypeSpecificFlags = pProps->dwTypeSpecificFlags; + OutProps.dwSampleFlags = + (OutProps.dwSampleFlags & AM_SAMPLE_TYPECHANGED) | + (pProps->dwSampleFlags & ~AM_SAMPLE_TYPECHANGED); + OutProps.tStart = pProps->tStart; + OutProps.tStop = pProps->tStop; + OutProps.cbData = FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, dwStreamId); + hr = pOutSample2->SetProperties( + FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, dwStreamId), + (PBYTE)&OutProps + ); + if (pProps->dwSampleFlags & AM_SAMPLE_DATADISCONTINUITY) { + m_bSampleSkipped = FALSE; + } + pOutSample2->Release(); + } else { + if (pProps->dwSampleFlags & AM_SAMPLE_TIMEVALID) { + pOutSample->SetTime(&pProps->tStart, + &pProps->tStop); + } + if (pProps->dwSampleFlags & AM_SAMPLE_SPLICEPOINT) { + pOutSample->SetSyncPoint(TRUE); + } + if (pProps->dwSampleFlags & AM_SAMPLE_DATADISCONTINUITY) { + pOutSample->SetDiscontinuity(TRUE); + m_bSampleSkipped = FALSE; + } + // Copy the media times + + LONGLONG MediaStart, MediaEnd; + if (pSample->GetMediaTime(&MediaStart,&MediaEnd) == NOERROR) { + pOutSample->SetMediaTime(&MediaStart,&MediaEnd); + } + } + return S_OK; +} + +// override this to customize the transform process + +HRESULT +CTransformFilter::Receive(IMediaSample *pSample) +{ + /* Check for other streams and pass them on */ + AM_SAMPLE2_PROPERTIES * const pProps = m_pInput->SampleProps(); + if (pProps->dwStreamId != AM_STREAM_MEDIA) { + return m_pOutput->m_pInputPin->Receive(pSample); + } + HRESULT hr; + ASSERT(pSample); + IMediaSample * pOutSample; + + // If no output to deliver to then no point sending us data + + ASSERT (m_pOutput != NULL) ; + + // Set up the output sample + hr = InitializeOutputSample(pSample, &pOutSample); + + if (FAILED(hr)) { + return hr; + } + + // Start timing the transform (if PERF is defined) + MSR_START(m_idTransform); + + // have the derived class transform the data + + hr = Transform(pSample, pOutSample); + + // Stop the clock and log it (if PERF is defined) + MSR_STOP(m_idTransform); + + if (FAILED(hr)) { + DbgLog((LOG_TRACE,1,TEXT("Error from transform"))); + } else { + // the Transform() function can return S_FALSE to indicate that the + // sample should not be delivered; we only deliver the sample if it's + // really S_OK (same as NOERROR, of course.) + if (hr == NOERROR) { + hr = m_pOutput->m_pInputPin->Receive(pOutSample); + m_bSampleSkipped = FALSE; // last thing no longer dropped + } else { + // S_FALSE returned from Transform is a PRIVATE agreement + // We should return NOERROR from Receive() in this cause because returning S_FALSE + // from Receive() means that this is the end of the stream and no more data should + // be sent. + if (S_FALSE == hr) { + + // Release the sample before calling notify to avoid + // deadlocks if the sample holds a lock on the system + // such as DirectDraw buffers do + pOutSample->Release(); + m_bSampleSkipped = TRUE; + if (!m_bQualityChanged) { + NotifyEvent(EC_QUALITY_CHANGE,0,0); + m_bQualityChanged = TRUE; + } + return NOERROR; + } + } + } + + // release the output buffer. If the connected pin still needs it, + // it will have addrefed it itself. + pOutSample->Release(); + + return hr; +} + + +// Return S_FALSE to mean "pass the note on upstream" +// Return NOERROR (Same as S_OK) +// to mean "I've done something about it, don't pass it on" +HRESULT CTransformFilter::AlterQuality(Quality q) +{ + UNREFERENCED_PARAMETER(q); + return S_FALSE; +} + + +// EndOfStream received. Default behaviour is to deliver straight +// downstream, since we have no queued data. If you overrode Receive +// and have queue data, then you need to handle this and deliver EOS after +// all queued data is sent +HRESULT +CTransformFilter::EndOfStream(void) +{ + HRESULT hr = NOERROR; + if (m_pOutput != NULL) { + hr = m_pOutput->DeliverEndOfStream(); + } + + return hr; +} + + +// enter flush state. Receives already blocked +// must override this if you have queued data or a worker thread +HRESULT +CTransformFilter::BeginFlush(void) +{ + HRESULT hr = NOERROR; + if (m_pOutput != NULL) { + // block receives -- done by caller (CBaseInputPin::BeginFlush) + + // discard queued data -- we have no queued data + + // free anyone blocked on receive - not possible in this filter + + // call downstream + hr = m_pOutput->DeliverBeginFlush(); + } + return hr; +} + + +// leave flush state. must override this if you have queued data +// or a worker thread +HRESULT +CTransformFilter::EndFlush(void) +{ + // sync with pushing thread -- we have no worker thread + + // ensure no more data to go downstream -- we have no queued data + + // call EndFlush on downstream pins + ASSERT (m_pOutput != NULL); + return m_pOutput->DeliverEndFlush(); + + // caller (the input pin's method) will unblock Receives +} + + +// override these so that the derived filter can catch them + +STDMETHODIMP +CTransformFilter::Stop() +{ + CAutoLock lck1(&m_csFilter); + if (m_State == State_Stopped) { + return NOERROR; + } + + // Succeed the Stop if we are not completely connected + + ASSERT(m_pInput == NULL || m_pOutput != NULL); + if (m_pInput == NULL || m_pInput->IsConnected() == FALSE || + m_pOutput->IsConnected() == FALSE) { + m_State = State_Stopped; + m_bEOSDelivered = FALSE; + return NOERROR; + } + + ASSERT(m_pInput); + ASSERT(m_pOutput); + + // decommit the input pin before locking or we can deadlock + m_pInput->Inactive(); + + // synchronize with Receive calls + + CAutoLock lck2(&m_csReceive); + m_pOutput->Inactive(); + + // allow a class derived from CTransformFilter + // to know about starting and stopping streaming + + HRESULT hr = StopStreaming(); + if (SUCCEEDED(hr)) { + // complete the state transition + m_State = State_Stopped; + m_bEOSDelivered = FALSE; + } + return hr; +} + + +STDMETHODIMP +CTransformFilter::Pause() +{ + CAutoLock lck(&m_csFilter); + HRESULT hr = NOERROR; + + if (m_State == State_Paused) { + // (This space left deliberately blank) + } + + // If we have no input pin or it isn't yet connected then when we are + // asked to pause we deliver an end of stream to the downstream filter. + // This makes sure that it doesn't sit there forever waiting for + // samples which we cannot ever deliver without an input connection. + + else if (m_pInput == NULL || m_pInput->IsConnected() == FALSE) { + if (m_pOutput && m_bEOSDelivered == FALSE) { + m_pOutput->DeliverEndOfStream(); + m_bEOSDelivered = TRUE; + } + m_State = State_Paused; + } + + // We may have an input connection but no output connection + // However, if we have an input pin we do have an output pin + + else if (m_pOutput->IsConnected() == FALSE) { + m_State = State_Paused; + } + + else { + if (m_State == State_Stopped) { + // allow a class derived from CTransformFilter + // to know about starting and stopping streaming + CAutoLock lck2(&m_csReceive); + hr = StartStreaming(); + } + if (SUCCEEDED(hr)) { + hr = CBaseFilter::Pause(); + } + } + + m_bSampleSkipped = FALSE; + m_bQualityChanged = FALSE; + return hr; +} + +HRESULT +CTransformFilter::NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate) +{ + if (m_pOutput != NULL) { + return m_pOutput->DeliverNewSegment(tStart, tStop, dRate); + } + return S_OK; +} + +// Check streaming status +HRESULT +CTransformInputPin::CheckStreaming() +{ + ASSERT(m_pTransformFilter->m_pOutput != NULL); + if (!m_pTransformFilter->m_pOutput->IsConnected()) { + return VFW_E_NOT_CONNECTED; + } else { + // Shouldn't be able to get any data if we're not connected! + ASSERT(IsConnected()); + + // we're flushing + if (m_bFlushing) { + return S_FALSE; + } + // Don't process stuff in Stopped state + if (IsStopped()) { + return VFW_E_WRONG_STATE; + } + if (m_bRunTimeError) { + return VFW_E_RUNTIME_ERROR; + } + return S_OK; + } +} + + +// ================================================================= +// Implements the CTransformInputPin class +// ================================================================= + + +// constructor + +CTransformInputPin::CTransformInputPin( + TCHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName) + : CBaseInputPin(pObjectName, pTransformFilter, &pTransformFilter->m_csFilter, phr, pName) +{ + DbgLog((LOG_TRACE,2,TEXT("CTransformInputPin::CTransformInputPin"))); + m_pTransformFilter = pTransformFilter; +} + +#ifdef UNICODE +CTransformInputPin::CTransformInputPin( + CHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName) + : CBaseInputPin(pObjectName, pTransformFilter, &pTransformFilter->m_csFilter, phr, pName) +{ + DbgLog((LOG_TRACE,2,TEXT("CTransformInputPin::CTransformInputPin"))); + m_pTransformFilter = pTransformFilter; +} +#endif + +// provides derived filter a chance to grab extra interfaces + +HRESULT +CTransformInputPin::CheckConnect(IPin *pPin) +{ + HRESULT hr = m_pTransformFilter->CheckConnect(PINDIR_INPUT,pPin); + if (FAILED(hr)) { + return hr; + } + return CBaseInputPin::CheckConnect(pPin); +} + + +// provides derived filter a chance to release it's extra interfaces + +HRESULT +CTransformInputPin::BreakConnect() +{ + // Can't disconnect unless stopped + ASSERT(IsStopped()); + m_pTransformFilter->BreakConnect(PINDIR_INPUT); + return CBaseInputPin::BreakConnect(); +} + + +// Let derived class know when the input pin is connected + +HRESULT +CTransformInputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = m_pTransformFilter->CompleteConnect(PINDIR_INPUT,pReceivePin); + if (FAILED(hr)) { + return hr; + } + return CBaseInputPin::CompleteConnect(pReceivePin); +} + + +// check that we can support a given media type + +HRESULT +CTransformInputPin::CheckMediaType(const CMediaType* pmt) +{ + // Check the input type + + HRESULT hr = m_pTransformFilter->CheckInputType(pmt); + if (S_OK != hr) { + return hr; + } + + // if the output pin is still connected, then we have + // to check the transform not just the input format + + if ((m_pTransformFilter->m_pOutput != NULL) && + (m_pTransformFilter->m_pOutput->IsConnected())) { + return m_pTransformFilter->CheckTransform( + pmt, + &m_pTransformFilter->m_pOutput->CurrentMediaType()); + } else { + return hr; + } +} + + +// set the media type for this connection + +HRESULT +CTransformInputPin::SetMediaType(const CMediaType* mtIn) +{ + // Set the base class media type (should always succeed) + HRESULT hr = CBasePin::SetMediaType(mtIn); + if (FAILED(hr)) { + return hr; + } + + // check the transform can be done (should always succeed) + ASSERT(SUCCEEDED(m_pTransformFilter->CheckInputType(mtIn))); + + return m_pTransformFilter->SetMediaType(PINDIR_INPUT,mtIn); +} + + +// ================================================================= +// Implements IMemInputPin interface +// ================================================================= + + +// provide EndOfStream that passes straight downstream +// (there is no queued data) +STDMETHODIMP +CTransformInputPin::EndOfStream(void) +{ + CAutoLock lck(&m_pTransformFilter->m_csReceive); + HRESULT hr = CheckStreaming(); + if (S_OK == hr) { + hr = m_pTransformFilter->EndOfStream(); + } + return hr; +} + + +// enter flushing state. Call default handler to block Receives, then +// pass to overridable method in filter +STDMETHODIMP +CTransformInputPin::BeginFlush(void) +{ + CAutoLock lck(&m_pTransformFilter->m_csFilter); + // Are we actually doing anything? + ASSERT(m_pTransformFilter->m_pOutput != NULL); + if (!IsConnected() || + !m_pTransformFilter->m_pOutput->IsConnected()) { + return VFW_E_NOT_CONNECTED; + } + HRESULT hr = CBaseInputPin::BeginFlush(); + if (FAILED(hr)) { + return hr; + } + + return m_pTransformFilter->BeginFlush(); +} + + +// leave flushing state. +// Pass to overridable method in filter, then call base class +// to unblock receives (finally) +STDMETHODIMP +CTransformInputPin::EndFlush(void) +{ + CAutoLock lck(&m_pTransformFilter->m_csFilter); + // Are we actually doing anything? + ASSERT(m_pTransformFilter->m_pOutput != NULL); + if (!IsConnected() || + !m_pTransformFilter->m_pOutput->IsConnected()) { + return VFW_E_NOT_CONNECTED; + } + + HRESULT hr = m_pTransformFilter->EndFlush(); + if (FAILED(hr)) { + return hr; + } + + return CBaseInputPin::EndFlush(); +} + + +// here's the next block of data from the stream. +// AddRef it yourself if you need to hold it beyond the end +// of this call. + +HRESULT +CTransformInputPin::Receive(IMediaSample * pSample) +{ + HRESULT hr; + CAutoLock lck(&m_pTransformFilter->m_csReceive); + ASSERT(pSample); + + // check all is well with the base class + hr = CBaseInputPin::Receive(pSample); + if (S_OK == hr) { + hr = m_pTransformFilter->Receive(pSample); + } + return hr; +} + + + + +// override to pass downstream +STDMETHODIMP +CTransformInputPin::NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate) +{ + // Save the values in the pin + CBasePin::NewSegment(tStart, tStop, dRate); + return m_pTransformFilter->NewSegment(tStart, tStop, dRate); +} + + + + +// ================================================================= +// Implements the CTransformOutputPin class +// ================================================================= + + +// constructor + +CTransformOutputPin::CTransformOutputPin( + TCHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pPinName) + : CBaseOutputPin(pObjectName, pTransformFilter, &pTransformFilter->m_csFilter, phr, pPinName), + m_pPosition(NULL) +{ + DbgLog((LOG_TRACE,2,TEXT("CTransformOutputPin::CTransformOutputPin"))); + m_pTransformFilter = pTransformFilter; + +} + +#ifdef UNICODE +CTransformOutputPin::CTransformOutputPin( + CHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pPinName) + : CBaseOutputPin(pObjectName, pTransformFilter, &pTransformFilter->m_csFilter, phr, pPinName), + m_pPosition(NULL) +{ + DbgLog((LOG_TRACE,2,TEXT("CTransformOutputPin::CTransformOutputPin"))); + m_pTransformFilter = pTransformFilter; + +} +#endif + +// destructor + +CTransformOutputPin::~CTransformOutputPin() +{ + DbgLog((LOG_TRACE,2,TEXT("CTransformOutputPin::~CTransformOutputPin"))); + + if (m_pPosition) m_pPosition->Release(); +} + + +// overriden to expose IMediaPosition and IMediaSeeking control interfaces + +STDMETHODIMP +CTransformOutputPin::NonDelegatingQueryInterface(REFIID riid, void **ppv) +{ + CheckPointer(ppv,E_POINTER); + ValidateReadWritePtr(ppv,sizeof(PVOID)); + *ppv = NULL; + + if (riid == IID_IMediaPosition || riid == IID_IMediaSeeking) { + + // we should have an input pin by now + + ASSERT(m_pTransformFilter->m_pInput != NULL); + + if (m_pPosition == NULL) { + + HRESULT hr = CreatePosPassThru( + GetOwner(), + FALSE, + (IPin *)m_pTransformFilter->m_pInput, + &m_pPosition); + if (FAILED(hr)) { + return hr; + } + } + return m_pPosition->QueryInterface(riid, ppv); + } else { + return CBaseOutputPin::NonDelegatingQueryInterface(riid, ppv); + } +} + + +// provides derived filter a chance to grab extra interfaces + +HRESULT +CTransformOutputPin::CheckConnect(IPin *pPin) +{ + // we should have an input connection first + + ASSERT(m_pTransformFilter->m_pInput != NULL); + if ((m_pTransformFilter->m_pInput->IsConnected() == FALSE)) { + return E_UNEXPECTED; + } + + HRESULT hr = m_pTransformFilter->CheckConnect(PINDIR_OUTPUT,pPin); + if (FAILED(hr)) { + return hr; + } + return CBaseOutputPin::CheckConnect(pPin); +} + + +// provides derived filter a chance to release it's extra interfaces + +HRESULT +CTransformOutputPin::BreakConnect() +{ + // Can't disconnect unless stopped + ASSERT(IsStopped()); + m_pTransformFilter->BreakConnect(PINDIR_OUTPUT); + return CBaseOutputPin::BreakConnect(); +} + + +// Let derived class know when the output pin is connected + +HRESULT +CTransformOutputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = m_pTransformFilter->CompleteConnect(PINDIR_OUTPUT,pReceivePin); + if (FAILED(hr)) { + return hr; + } + return CBaseOutputPin::CompleteConnect(pReceivePin); +} + + +// check a given transform - must have selected input type first + +HRESULT +CTransformOutputPin::CheckMediaType(const CMediaType* pmtOut) +{ + // must have selected input first + ASSERT(m_pTransformFilter->m_pInput != NULL); + if ((m_pTransformFilter->m_pInput->IsConnected() == FALSE)) { + return E_INVALIDARG; + } + + return m_pTransformFilter->CheckTransform( + &m_pTransformFilter->m_pInput->CurrentMediaType(), + pmtOut); +} + + +// called after we have agreed a media type to actually set it in which case +// we run the CheckTransform function to get the output format type again + +HRESULT +CTransformOutputPin::SetMediaType(const CMediaType* pmtOut) +{ + HRESULT hr = NOERROR; + ASSERT(m_pTransformFilter->m_pInput != NULL); + + ASSERT(m_pTransformFilter->m_pInput->CurrentMediaType().IsValid()); + + // Set the base class media type (should always succeed) + hr = CBasePin::SetMediaType(pmtOut); + if (FAILED(hr)) { + return hr; + } + +#ifdef DEBUG + if (FAILED(m_pTransformFilter->CheckTransform(&m_pTransformFilter-> + m_pInput->CurrentMediaType(),pmtOut))) { + DbgLog((LOG_ERROR,0,TEXT("*** This filter is accepting an output media type"))); + DbgLog((LOG_ERROR,0,TEXT(" that it can't currently transform to. I hope"))); + DbgLog((LOG_ERROR,0,TEXT(" it's smart enough to reconnect its input."))); + } +#endif + + return m_pTransformFilter->SetMediaType(PINDIR_OUTPUT,pmtOut); +} + + +// pass the buffer size decision through to the main transform class + +HRESULT +CTransformOutputPin::DecideBufferSize( + IMemAllocator * pAllocator, + ALLOCATOR_PROPERTIES* pProp) +{ + return m_pTransformFilter->DecideBufferSize(pAllocator, pProp); +} + + + +// return a specific media type indexed by iPosition + +HRESULT +CTransformOutputPin::GetMediaType( + int iPosition, + CMediaType *pMediaType) +{ + ASSERT(m_pTransformFilter->m_pInput != NULL); + + // We don't have any media types if our input is not connected + + if (m_pTransformFilter->m_pInput->IsConnected()) { + return m_pTransformFilter->GetMediaType(iPosition,pMediaType); + } else { + return VFW_S_NO_MORE_ITEMS; + } +} + + +// Override this if you can do something constructive to act on the +// quality message. Consider passing it upstream as well + +// Pass the quality mesage on upstream. + +STDMETHODIMP +CTransformOutputPin::Notify(IBaseFilter * pSender, Quality q) +{ + UNREFERENCED_PARAMETER(pSender); + ValidateReadPtr(pSender,sizeof(IBaseFilter)); + + // First see if we want to handle this ourselves + HRESULT hr = m_pTransformFilter->AlterQuality(q); + if (hr!=S_FALSE) { + return hr; // either S_OK or a failure + } + + // S_FALSE means we pass the message on. + // Find the quality sink for our input pin and send it there + + ASSERT(m_pTransformFilter->m_pInput != NULL); + + return m_pTransformFilter->m_pInput->PassNotify(q); + +} // Notify + + +// the following removes a very large number of level 4 warnings from the microsoft +// compiler output, which are not useful at all in this case. +#pragma warning(disable:4514) diff --git a/plugins/GSdx/baseclasses/transfrm.h b/plugins/GSdx/baseclasses/transfrm.h new file mode 100644 index 0000000000..55a07dcab5 --- /dev/null +++ b/plugins/GSdx/baseclasses/transfrm.h @@ -0,0 +1,304 @@ +//------------------------------------------------------------------------------ +// File: Transfrm.h +// +// Desc: DirectShow base classes - defines classes from which simple +// transform codecs may be derived. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// It assumes the codec has one input and one output stream, and has no +// interest in memory management, interface negotiation or anything else. +// +// derive your class from this, and supply Transform and the media type/format +// negotiation functions. Implement that class, compile and link and +// you're done. + + +#ifndef __TRANSFRM__ +#define __TRANSFRM__ + +// ====================================================================== +// This is the com object that represents a simple transform filter. It +// supports IBaseFilter, IMediaFilter and two pins through nested interfaces +// ====================================================================== + +class CTransformFilter; + +// ================================================== +// Implements the input pin +// ================================================== + +class CTransformInputPin : public CBaseInputPin +{ + friend class CTransformFilter; + +protected: + CTransformFilter *m_pTransformFilter; + + +public: + + CTransformInputPin( + TCHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName); +#ifdef UNICODE + CTransformInputPin( + char *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName); +#endif + + STDMETHODIMP QueryId(LPWSTR * Id) + { + return AMGetWideString(L"In", Id); + } + + // Grab and release extra interfaces if required + + HRESULT CheckConnect(IPin *pPin); + HRESULT BreakConnect(); + HRESULT CompleteConnect(IPin *pReceivePin); + + // check that we can support this output type + HRESULT CheckMediaType(const CMediaType* mtIn); + + // set the connection media type + HRESULT SetMediaType(const CMediaType* mt); + + // --- IMemInputPin ----- + + // here's the next block of data from the stream. + // AddRef it yourself if you need to hold it beyond the end + // of this call. + STDMETHODIMP Receive(IMediaSample * pSample); + + // provide EndOfStream that passes straight downstream + // (there is no queued data) + STDMETHODIMP EndOfStream(void); + + // passes it to CTransformFilter::BeginFlush + STDMETHODIMP BeginFlush(void); + + // passes it to CTransformFilter::EndFlush + STDMETHODIMP EndFlush(void); + + STDMETHODIMP NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate); + + // Check if it's OK to process samples + virtual HRESULT CheckStreaming(); + + // Media type +public: + CMediaType& CurrentMediaType() { return m_mt; }; + +}; + +// ================================================== +// Implements the output pin +// ================================================== + +class CTransformOutputPin : public CBaseOutputPin +{ + friend class CTransformFilter; + +protected: + CTransformFilter *m_pTransformFilter; + +public: + + // implement IMediaPosition by passing upstream + IUnknown * m_pPosition; + + CTransformOutputPin( + TCHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName); +#ifdef UNICODE + CTransformOutputPin( + CHAR *pObjectName, + CTransformFilter *pTransformFilter, + HRESULT * phr, + LPCWSTR pName); +#endif + ~CTransformOutputPin(); + + // override to expose IMediaPosition + STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv); + + // --- CBaseOutputPin ------------ + + STDMETHODIMP QueryId(LPWSTR * Id) + { + return AMGetWideString(L"Out", Id); + } + + // Grab and release extra interfaces if required + + HRESULT CheckConnect(IPin *pPin); + HRESULT BreakConnect(); + HRESULT CompleteConnect(IPin *pReceivePin); + + // check that we can support this output type + HRESULT CheckMediaType(const CMediaType* mtOut); + + // set the connection media type + HRESULT SetMediaType(const CMediaType *pmt); + + // called from CBaseOutputPin during connection to ask for + // the count and size of buffers we need. + HRESULT DecideBufferSize( + IMemAllocator * pAlloc, + ALLOCATOR_PROPERTIES *pProp); + + // returns the preferred formats for a pin + HRESULT GetMediaType(int iPosition,CMediaType *pMediaType); + + // inherited from IQualityControl via CBasePin + STDMETHODIMP Notify(IBaseFilter * pSender, Quality q); + + // Media type +public: + CMediaType& CurrentMediaType() { return m_mt; }; +}; + + +class AM_NOVTABLE CTransformFilter : public CBaseFilter +{ + +public: + + // map getpin/getpincount for base enum of pins to owner + // override this to return more specialised pin objects + + virtual int GetPinCount(); + virtual CBasePin * GetPin(int n); + STDMETHODIMP FindPin(LPCWSTR Id, IPin **ppPin); + + // override state changes to allow derived transform filter + // to control streaming start/stop + STDMETHODIMP Stop(); + STDMETHODIMP Pause(); + +public: + + CTransformFilter(TCHAR *, LPUNKNOWN, REFCLSID clsid); +#ifdef UNICODE + CTransformFilter(CHAR *, LPUNKNOWN, REFCLSID clsid); +#endif + ~CTransformFilter(); + + // ================================================================= + // ----- override these bits --------------------------------------- + // ================================================================= + + // These must be supplied in a derived class + + virtual HRESULT Transform(IMediaSample * pIn, IMediaSample *pOut); + + // check if you can support mtIn + virtual HRESULT CheckInputType(const CMediaType* mtIn) PURE; + + // check if you can support the transform from this input to this output + virtual HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut) PURE; + + // this goes in the factory template table to create new instances + // static CCOMObject * CreateInstance(LPUNKNOWN, HRESULT *); + + // call the SetProperties function with appropriate arguments + virtual HRESULT DecideBufferSize( + IMemAllocator * pAllocator, + ALLOCATOR_PROPERTIES *pprop) PURE; + + // override to suggest OUTPUT pin media types + virtual HRESULT GetMediaType(int iPosition, CMediaType *pMediaType) PURE; + + + + // ================================================================= + // ----- Optional Override Methods ----------------------- + // ================================================================= + + // you can also override these if you want to know about streaming + virtual HRESULT StartStreaming(); + virtual HRESULT StopStreaming(); + + // override if you can do anything constructive with quality notifications + virtual HRESULT AlterQuality(Quality q); + + // override this to know when the media type is actually set + virtual HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt); + + // chance to grab extra interfaces on connection + virtual HRESULT CheckConnect(PIN_DIRECTION dir,IPin *pPin); + virtual HRESULT BreakConnect(PIN_DIRECTION dir); + virtual HRESULT CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin); + + // chance to customize the transform process + virtual HRESULT Receive(IMediaSample *pSample); + + // Standard setup for output sample + HRESULT InitializeOutputSample(IMediaSample *pSample, IMediaSample **ppOutSample); + + // if you override Receive, you may need to override these three too + virtual HRESULT EndOfStream(void); + virtual HRESULT BeginFlush(void); + virtual HRESULT EndFlush(void); + virtual HRESULT NewSegment( + REFERENCE_TIME tStart, + REFERENCE_TIME tStop, + double dRate); + +#ifdef PERF + // Override to register performance measurement with a less generic string + // You should do this to avoid confusion with other filters + virtual void RegisterPerfId() + {m_idTransform = MSR_REGISTER(TEXT("Transform"));} +#endif // PERF + + +// implementation details + +protected: + +#ifdef PERF + int m_idTransform; // performance measuring id +#endif + BOOL m_bEOSDelivered; // have we sent EndOfStream + BOOL m_bSampleSkipped; // Did we just skip a frame + BOOL m_bQualityChanged; // Have we degraded? + + // critical section protecting filter state. + + CCritSec m_csFilter; + + // critical section stopping state changes (ie Stop) while we're + // processing a sample. + // + // This critical section is held when processing + // events that occur on the receive thread - Receive() and EndOfStream(). + // + // If you want to hold both m_csReceive and m_csFilter then grab + // m_csFilter FIRST - like CTransformFilter::Stop() does. + + CCritSec m_csReceive; + + // these hold our input and output pins + + friend class CTransformInputPin; + friend class CTransformOutputPin; + CTransformInputPin *m_pInput; + CTransformOutputPin *m_pOutput; +}; + +#endif /* __TRANSFRM__ */ + + diff --git a/plugins/GSdx/baseclasses/transip.cpp b/plugins/GSdx/baseclasses/transip.cpp new file mode 100644 index 0000000000..43c72f20a8 --- /dev/null +++ b/plugins/GSdx/baseclasses/transip.cpp @@ -0,0 +1,966 @@ +//------------------------------------------------------------------------------ +// File: TransIP.cpp +// +// Desc: DirectShow base classes - implements class for simple Transform- +// In-Place filters such as audio. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// How allocators are decided. +// +// An in-place transform tries to do its work in someone else's buffers. +// It tries to persuade the filters on either side to use the same allocator +// (and for that matter the same media type). In desperation, if the downstream +// filter refuses to supply an allocator and the upstream filter offers only +// a read-only one then it will provide an allocator. +// if the upstream filter insists on a read-only allocator then the transform +// filter will (reluctantly) copy the data before transforming it. +// +// In order to pass an allocator through it needs to remember the one it got +// from the first connection to pass it on to the second one. +// +// It is good if we can avoid insisting on a particular order of connection +// (There is a precedent for insisting on the input +// being connected first. Insisting on the output being connected first is +// not allowed. That would break RenderFile.) +// +// The base pin classes (CBaseOutputPin and CBaseInputPin) both have a +// m_pAllocator member which is used in places like +// CBaseOutputPin::GetDeliveryBuffer and CBaseInputPin::Inactive. +// To avoid lots of extra overriding, we should keep these happy +// by using these pointers. +// +// When each pin is connected, it will set the corresponding m_pAllocator +// and will have a single ref-count on that allocator. +// +// Refcounts are acquired by GetAllocator calls which return AddReffed +// allocators and are released in one of: +// CBaseInputPin::Disconnect +// CBaseOutputPin::BreakConect +// In each case m_pAllocator is set to NULL after the release, so this +// is the last chance to ever release it. If there should ever be +// multiple refcounts associated with the same pointer, this had better +// be cleared up before that happens. To avoid such problems, we'll +// stick with one per pointer. + + + +// RECONNECTING and STATE CHANGES +// +// Each pin could be disconnected, connected with a read-only allocator, +// connected with an upstream read/write allocator, connected with an +// allocator from downstream or connected with its own allocator. +// Five states for each pin gives a data space of 25 states. +// +// Notation: +// +// R/W == read/write +// R-O == read-only +// +// +// +// 00 means an unconnected pin. +// <- means using a R/W allocator from the upstream filter +// <= means using a R-O allocator from an upstream filter +// || means using our own (R/W) allocator. +// -> means using a R/W allocator from a downstream filter +// (a R-O allocator from downstream is nonsense, it can't ever work). +// +// +// That makes 25 possible states. Some states are nonsense (two different +// allocators from the same place). These are just an artifact of the notation. +// <= <- Nonsense. +// <- <= Nonsense +// Some states are illegal (the output pin never accepts a R-O allocator): +// 00 <= !! Error !! +// <= <= !! Error !! +// || <= !! Error !! +// -> <= !! Error !! +// Three states appears to be inaccessible: +// -> || Inaccessible +// || -> Inaccessible +// || <- Inaccessible +// Some states only ever occur as intermediates with a pending reconnect which +// is guaranteed to finish in another state. +// -> 00 ?? unstable goes to || 00 +// 00 <- ?? unstable goes to 00 || +// -> <- ?? unstable goes to -> -> +// <- || ?? unstable goes to <- <- +// <- -> ?? unstable goes to <- <- +// And that leaves 11 possible resting states: +// 1 00 00 Nothing connected. +// 2 <- 00 Input pin connected. +// 3 <= 00 Input pin connected using R-O allocator. +// 4 || 00 Needs several state changes to get here. +// 5 00 || Output pin connected using our allocator +// 6 00 -> Downstream only connected +// 7 || || Undesirable but can be forced upon us. +// 8 <= || Copy forced. <= -> is preferable +// 9 <= -> OK - forced to copy. +// 10 <- <- Transform in place (ideal) +// 11 -> -> Transform in place (ideal) +// +// The object of the exercise is to ensure that we finish up in states +// 10 or 11 whenever possible. State 10 is only possible if the upstream +// filter has a R/W allocator (the AVI splitter notoriously +// doesn't) and state 11 is only possible if the downstream filter does +// offer an allocator. +// +// The transition table (entries marked * go via a reconnect) +// +// There are 8 possible transitions: +// A: Connect upstream to filter with R-O allocator that insists on using it. +// B: Connect upstream to filter with R-O allocator but chooses not to use it. +// C: Connect upstream to filter with R/W allocator and insists on using it. +// D: Connect upstream to filter with R/W allocator but chooses not to use it. +// E: Connect downstream to a filter that offers an allocator +// F: Connect downstream to a filter that does not offer an allocator +// G: disconnect upstream +// H: Disconnect downstream +// +// A B C D E F G H +// --------------------------------------------------------- +// 00 00 1 | 3 3 2 2 6 5 . . |1 00 00 +// <- 00 2 | . . . . *10/11 10 1 . |2 <- 00 +// <= 00 3 | . . . . *9/11 *7/8 1 . |3 <= 00 +// || 00 4 | . . . . *8 *7 1 . |4 || 00 +// 00 || 5 | 8 7 *10 7 . . . 1 |5 00 || +// 00 -> 6 | 9 11 *10 11 . . . 1 |6 00 -> +// || || 7 | . . . . . . 5 4 |7 || || +// <= || 8 | . . . . . . 5 3 |8 <= || +// <= -> 9 | . . . . . . 6 3 |9 <= -> +// <- <- 10| . . . . . . *5/6 2 |10 <- <- +// -> -> 11| . . . . . . 6 *2/3 |11 -> -> +// --------------------------------------------------------- +// A B C D E F G H +// +// All these states are accessible without requiring any filter to +// change its behaviour but not all transitions are accessible, for +// instance a transition from state 4 to anywhere other than +// state 8 requires that the upstream filter first offer a R-O allocator +// and then changes its mind and offer R/W. This is NOT allowable - it +// leads to things like the output pin getting a R/W allocator from +// upstream and then the input pin being told it can only have a R-O one. +// Note that you CAN change (say) the upstream filter for a different one, but +// only as a disconnect / connect, not as a Reconnect. (Exercise for +// the reader is to see how you get into state 4). +// +// The reconnection stuff goes as follows (some of the cases shown here as +// "no reconnect" may get one to finalise media type - an old story). +// If there is a reconnect where it says "no reconnect" here then the +// reconnection must not change the allocator choice. +// +// state 2: <- 00 transition E <- <- case C <- <- (no change) +// case D -> <- and then to -> -> +// +// state 2: <- 00 transition F <- <- (no reconnect) +// +// state 3: <= 00 transition E <= -> case A <= -> (no change) +// case B -> -> +// transition F <= || case A <= || (no change) +// case B || || +// +// state 4: || 00 transition E || || case B -> || and then all cases to -> -> +// F || || case B || || (no change) +// +// state 5: 00 || transition A <= || (no reconnect) +// B || || (no reconnect) +// C <- || all cases <- <- +// D || || (unfortunate, but upstream's choice) +// +// state 6: 00 -> transition A <= -> (no reconnect) +// B -> -> (no reconnect) +// C <- -> all cases <- <- +// D -> -> (no reconnect) +// +// state 10:<- <- transition G 00 <- case E 00 -> +// case F 00 || +// +// state 11:-> -> transition H -> 00 case A <= 00 (schizo) +// case B <= 00 +// case C <- 00 (schizo) +// case D <- 00 +// +// The Rules: +// To sort out media types: +// The input is reconnected +// if the input pin is connected and the output pin connects +// The output is reconnected +// If the output pin is connected +// and the input pin connects to a different media type +// +// To sort out allocators: +// The input is reconnected +// if the output disconnects and the input was using a downstream allocator +// The output pin calls SetAllocator to pass on a new allocator +// if the output is connected and +// if the input disconnects and the output was using an upstream allocator +// if the input acquires an allocator different from the output one +// and that new allocator is not R-O +// +// Data is copied (i.e. call getbuffer and copy the data before transforming it) +// if the two allocators are different. + + + +// CHAINS of filters: +// +// We sit between two filters (call them A and Z). We should finish up +// with the same allocator on both of our pins and that should be the +// same one that A and Z would have agreed on if we hadn't been in the +// way. Furthermore, it should not matter how many in-place transforms +// are in the way. Let B, C, D... be in-place transforms ("us"). +// Here's how it goes: +// +// 1. +// A connects to B. They agree on A's allocator. +// A-a->B +// +// 2. +// B connects to C. Same story. There is no point in a reconnect, but +// B will request an input reconnect anyway. +// A-a->B-a->C +// +// 3. +// C connects to Z. +// C insists on using A's allocator, but compromises by requesting a reconnect. +// of C's input. +// A-a->B-?->C-a->Z +// +// We now have pending reconnects on both A--->B and B--->C +// +// 4. +// The A--->B link is reconnected. +// A asks B for an allocator. B sees that it has a downstream connection so +// asks its downstream input pin i.e. C's input pin for an allocator. C sees +// that it too has a downstream connection so asks Z for an allocator. +// +// Even though Z's input pin is connected, it is being asked for an allocator. +// It could refuse, in which case the chain is done and will use A's allocator +// Alternatively, Z may supply one. A chooses either Z's or A's own one. +// B's input pin gets NotifyAllocator called to tell it the decision and it +// propagates this downstream by calling ReceiveAllocator on its output pin +// which calls NotifyAllocator on the next input pin downstream etc. +// If the choice is Z then it goes: +// A-z->B-a->C-a->Z +// A-z->B-z->C-a->Z +// A-z->B-z->C-z->Z +// +// And that's IT!! Any further (essentially spurious) reconnects peter out +// with no change in the chain. + +#include "streams.h" +#include "measure.h" +#include "transip.h" + + +// ================================================================= +// Implements the CTransInPlaceFilter class +// ================================================================= + +CTransInPlaceFilter::CTransInPlaceFilter + ( TCHAR *pName, + LPUNKNOWN pUnk, + REFCLSID clsid, + HRESULT *phr, + bool bModifiesData + ) + : CTransformFilter(pName, pUnk, clsid), + m_bModifiesData(bModifiesData) +{ +#ifdef PERF + RegisterPerfId(); +#endif // PERF + +} // constructor + +#ifdef UNICODE +CTransInPlaceFilter::CTransInPlaceFilter + ( CHAR *pName, + LPUNKNOWN pUnk, + REFCLSID clsid, + HRESULT *phr, + bool bModifiesData + ) + : CTransformFilter(pName, pUnk, clsid), + m_bModifiesData(bModifiesData) +{ +#ifdef PERF + RegisterPerfId(); +#endif // PERF + +} // constructor +#endif + +// return a non-addrefed CBasePin * for the user to addref if he holds onto it +// for longer than his pointer to us. We create the pins dynamically when they +// are asked for rather than in the constructor. This is because we want to +// give the derived class an oppportunity to return different pin objects + +// As soon as any pin is needed we create both (this is different from the +// usual transform filter) because enumerators, allocators etc are passed +// through from one pin to another and it becomes very painful if the other +// pin isn't there. If we fail to create either pin we ensure we fail both. + +CBasePin * +CTransInPlaceFilter::GetPin(int n) +{ + HRESULT hr = S_OK; + + // Create an input pin if not already done + + if (m_pInput == NULL) { + + m_pInput = new CTransInPlaceInputPin( NAME("TransInPlace input pin") + , this // Owner filter + , &hr // Result code + , L"Input" // Pin name + ); + + // Constructor for CTransInPlaceInputPin can't fail + ASSERT(SUCCEEDED(hr)); + } + + // Create an output pin if not already done + + if (m_pInput!=NULL && m_pOutput == NULL) { + + m_pOutput = new CTransInPlaceOutputPin( NAME("TransInPlace output pin") + , this // Owner filter + , &hr // Result code + , L"Output" // Pin name + ); + + // a failed return code should delete the object + + ASSERT(SUCCEEDED(hr)); + if (m_pOutput == NULL) { + delete m_pInput; + m_pInput = NULL; + } + } + + // Return the appropriate pin + + ASSERT (n>=0 && n<=1); + if (n == 0) { + return m_pInput; + } else if (n==1) { + return m_pOutput; + } else { + return NULL; + } + +} // GetPin + + + +// dir is the direction of our pin. +// pReceivePin is the pin we are connecting to. +HRESULT CTransInPlaceFilter::CompleteConnect(PIN_DIRECTION dir,IPin *pReceivePin) +{ + UNREFERENCED_PARAMETER(pReceivePin); + ASSERT(m_pInput); + ASSERT(m_pOutput); + + // if we are not part of a graph, then don't indirect the pointer + // this probably prevents use of the filter without a filtergraph + if (!m_pGraph) { + return VFW_E_NOT_IN_GRAPH; + } + + // Always reconnect the input to account for buffering changes + // + // Because we don't get to suggest a type on ReceiveConnection + // we need another way of making sure the right type gets used. + // + // One way would be to have our EnumMediaTypes return our output + // connection type first but more deterministic and simple is to + // call ReconnectEx passing the type we want to reconnect with + // via the base class ReconeectPin method. + + if (dir == PINDIR_OUTPUT) { + if( m_pInput->IsConnected() ) { + return ReconnectPin( m_pInput, &m_pOutput->CurrentMediaType() ); + } + return NOERROR; + } + + ASSERT(dir == PINDIR_INPUT); + + // Reconnect output if necessary + + if( m_pOutput->IsConnected() ) { + + if ( m_pInput->CurrentMediaType() + != m_pOutput->CurrentMediaType() + ) { + return ReconnectPin( m_pOutput, &m_pInput->CurrentMediaType() ); + } + } + return NOERROR; + +} // ComnpleteConnect + + +// +// DecideBufferSize +// +// Tell the output pin's allocator what size buffers we require. +// *pAlloc will be the allocator our output pin is using. +// + +HRESULT CTransInPlaceFilter::DecideBufferSize + ( IMemAllocator *pAlloc + , ALLOCATOR_PROPERTIES *pProperties + ) +{ + ALLOCATOR_PROPERTIES Request, Actual; + HRESULT hr; + + // If we are connected upstream, get his views + if (m_pInput->IsConnected()) { + // Get the input pin allocator, and get its size and count. + // we don't care about his alignment and prefix. + + hr = InputPin()->PeekAllocator()->GetProperties(&Request); + if (FAILED(hr)) { + // Input connected but with a secretive allocator - enough! + return hr; + } + } else { + // We're reduced to blind guessing. Let's guess one byte and if + // this isn't enough then when the other pin does get connected + // we can revise it. + ZeroMemory(&Request, sizeof(Request)); + Request.cBuffers = 1; + Request.cbBuffer = 1; + } + + + DbgLog((LOG_MEMORY,1,TEXT("Setting Allocator Requirements"))); + DbgLog((LOG_MEMORY,1,TEXT("Count %d, Size %d"), + Request.cBuffers, Request.cbBuffer)); + + // Pass the allocator requirements to our output side + // but do a little sanity checking first or we'll just hit + // asserts in the allocator. + + pProperties->cBuffers = Request.cBuffers; + pProperties->cbBuffer = Request.cbBuffer; + pProperties->cbAlign = Request.cbAlign; + if (pProperties->cBuffers<=0) {pProperties->cBuffers = 1; } + if (pProperties->cbBuffer<=0) {pProperties->cbBuffer = 1; } + hr = pAlloc->SetProperties(pProperties, &Actual); + + if (FAILED(hr)) { + return hr; + } + + DbgLog((LOG_MEMORY,1,TEXT("Obtained Allocator Requirements"))); + DbgLog((LOG_MEMORY,1,TEXT("Count %d, Size %d, Alignment %d"), + Actual.cBuffers, Actual.cbBuffer, Actual.cbAlign)); + + // Make sure we got the right alignment and at least the minimum required + + if ( (Request.cBuffers > Actual.cBuffers) + || (Request.cbBuffer > Actual.cbBuffer) + || (Request.cbAlign > Actual.cbAlign) + ) { + return E_FAIL; + } + return NOERROR; + +} // DecideBufferSize + +// +// Copy +// +// return a pointer to an identical copy of pSample +IMediaSample * CTransInPlaceFilter::Copy(IMediaSample *pSource) +{ + IMediaSample * pDest; + + HRESULT hr; + REFERENCE_TIME tStart, tStop; + const BOOL bTime = S_OK == pSource->GetTime( &tStart, &tStop); + + // this may block for an indeterminate amount of time + hr = OutputPin()->PeekAllocator()->GetBuffer( + &pDest + , bTime ? &tStart : NULL + , bTime ? &tStop : NULL + , m_bSampleSkipped ? AM_GBF_PREVFRAMESKIPPED : 0 + ); + + if (FAILED(hr)) { + return NULL; + } + + ASSERT(pDest); + IMediaSample2 *pSample2; + if (SUCCEEDED(pDest->QueryInterface(IID_IMediaSample2, (void **)&pSample2))) { + HRESULT hr = pSample2->SetProperties( + FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, pbBuffer), + (PBYTE)m_pInput->SampleProps()); + pSample2->Release(); + if (FAILED(hr)) { + pDest->Release(); + return NULL; + } + } else { + if (bTime) { + pDest->SetTime(&tStart, &tStop); + } + + if (S_OK == pSource->IsSyncPoint()) { + pDest->SetSyncPoint(TRUE); + } + if (S_OK == pSource->IsDiscontinuity() || m_bSampleSkipped) { + pDest->SetDiscontinuity(TRUE); + } + if (S_OK == pSource->IsPreroll()) { + pDest->SetPreroll(TRUE); + } + + // Copy the media type + AM_MEDIA_TYPE *pMediaType; + if (S_OK == pSource->GetMediaType(&pMediaType)) { + pDest->SetMediaType(pMediaType); + DeleteMediaType( pMediaType ); + } + + } + + m_bSampleSkipped = FALSE; + + // Copy the sample media times + REFERENCE_TIME TimeStart, TimeEnd; + if (pSource->GetMediaTime(&TimeStart,&TimeEnd) == NOERROR) { + pDest->SetMediaTime(&TimeStart,&TimeEnd); + } + + // Copy the actual data length and the actual data. + { + const long lDataLength = pSource->GetActualDataLength(); + pDest->SetActualDataLength(lDataLength); + + // Copy the sample data + { + BYTE *pSourceBuffer, *pDestBuffer; + long lSourceSize = pSource->GetSize(); + long lDestSize = pDest->GetSize(); + + ASSERT(lDestSize >= lSourceSize && lDestSize >= lDataLength); + + pSource->GetPointer(&pSourceBuffer); + pDest->GetPointer(&pDestBuffer); + ASSERT(lDestSize == 0 || pSourceBuffer != NULL && pDestBuffer != NULL); + + CopyMemory( (PVOID) pDestBuffer, (PVOID) pSourceBuffer, lDataLength ); + } + } + + return pDest; + +} // Copy + + +// override this to customize the transform process + +HRESULT +CTransInPlaceFilter::Receive(IMediaSample *pSample) +{ + /* Check for other streams and pass them on */ + AM_SAMPLE2_PROPERTIES * const pProps = m_pInput->SampleProps(); + if (pProps->dwStreamId != AM_STREAM_MEDIA) { + return m_pOutput->Deliver(pSample); + } + HRESULT hr; + + // Start timing the TransInPlace (if PERF is defined) + MSR_START(m_idTransInPlace); + + if (UsingDifferentAllocators()) { + + // We have to copy the data. + + pSample = Copy(pSample); + + if (pSample==NULL) { + MSR_STOP(m_idTransInPlace); + return E_UNEXPECTED; + } + } + + // have the derived class transform the data + hr = Transform(pSample); + + // Stop the clock and log it (if PERF is defined) + MSR_STOP(m_idTransInPlace); + + if (FAILED(hr)) { + DbgLog((LOG_TRACE, 1, TEXT("Error from TransInPlace"))); + if (UsingDifferentAllocators()) { + pSample->Release(); + } + return hr; + } + + // the Transform() function can return S_FALSE to indicate that the + // sample should not be delivered; we only deliver the sample if it's + // really S_OK (same as NOERROR, of course.) + if (hr == NOERROR) { + hr = m_pOutput->Deliver(pSample); + } else { + // But it would be an error to return this private workaround + // to the caller ... + if (S_FALSE == hr) { + // S_FALSE returned from Transform is a PRIVATE agreement + // We should return NOERROR from Receive() in this cause because + // returning S_FALSE from Receive() means that this is the end + // of the stream and no more data should be sent. + m_bSampleSkipped = TRUE; + if (!m_bQualityChanged) { + NotifyEvent(EC_QUALITY_CHANGE,0,0); + m_bQualityChanged = TRUE; + } + hr = NOERROR; + } + } + + // release the output buffer. If the connected pin still needs it, + // it will have addrefed it itself. + if (UsingDifferentAllocators()) { + pSample->Release(); + } + + return hr; + +} // Receive + + + +// ================================================================= +// Implements the CTransInPlaceInputPin class +// ================================================================= + + +// constructor + +CTransInPlaceInputPin::CTransInPlaceInputPin + ( TCHAR *pObjectName + , CTransInPlaceFilter *pFilter + , HRESULT *phr + , LPCWSTR pName + ) + : CTransformInputPin(pObjectName, + pFilter, + phr, + pName) + , m_bReadOnly(FALSE) + , m_pTIPFilter(pFilter) +{ + DbgLog((LOG_TRACE, 2 + , TEXT("CTransInPlaceInputPin::CTransInPlaceInputPin"))); + +} // constructor + + +// ================================================================= +// Implements IMemInputPin interface +// ================================================================= + + +// If the downstream filter has one then offer that (even if our own output +// pin is not using it yet. If the upstream filter chooses it then we will +// tell our output pin to ReceiveAllocator). +// Else if our output pin is using an allocator then offer that. +// ( This could mean offering the upstream filter his own allocator, +// it could mean offerring our own +// ) or it could mean offering the one from downstream +// Else fail to offer any allocator at all. + +STDMETHODIMP CTransInPlaceInputPin::GetAllocator(IMemAllocator ** ppAllocator) +{ + CheckPointer(ppAllocator,E_POINTER); + ValidateReadWritePtr(ppAllocator,sizeof(IMemAllocator *)); + CAutoLock cObjectLock(m_pLock); + + HRESULT hr; + + if ( m_pTIPFilter->m_pOutput->IsConnected() ) { + // Store the allocator we got + hr = m_pTIPFilter->OutputPin()->ConnectedIMemInputPin() + ->GetAllocator( ppAllocator ); + if (SUCCEEDED(hr)) { + m_pTIPFilter->OutputPin()->SetAllocator( *ppAllocator ); + } + } + else { + // Help upstream filter (eg TIP filter which is having to do a copy) + // by providing a temp allocator here - we'll never use + // this allocator because when our output is connected we'll + // reconnect this pin + hr = CTransformInputPin::GetAllocator( ppAllocator ); + } + return hr; + +} // GetAllocator + + + +/* Get told which allocator the upstream output pin is actually going to use */ + + +STDMETHODIMP +CTransInPlaceInputPin::NotifyAllocator( + IMemAllocator * pAllocator, + BOOL bReadOnly) +{ + HRESULT hr = S_OK; + CheckPointer(pAllocator,E_POINTER); + ValidateReadPtr(pAllocator,sizeof(IMemAllocator)); + + CAutoLock cObjectLock(m_pLock); + + m_bReadOnly = bReadOnly; + // If we modify data then don't accept the allocator if it's + // the same as the output pin's allocator + + // If our output is not connected just accept the allocator + // We're never going to use this allocator because when our + // output pin is connected we'll reconnect this pin + if (!m_pTIPFilter->OutputPin()->IsConnected()) { + return CTransformInputPin::NotifyAllocator(pAllocator, bReadOnly); + } + + // If the allocator is read-only and we're modifying data + // and the allocator is the same as the output pin's + // then reject + if (bReadOnly && m_pTIPFilter->m_bModifiesData) { + IMemAllocator *pOutputAllocator = + m_pTIPFilter->OutputPin()->PeekAllocator(); + + // Make sure we have an output allocator + if (pOutputAllocator == NULL) { + hr = m_pTIPFilter->OutputPin()->ConnectedIMemInputPin()-> + GetAllocator(&pOutputAllocator); + if(FAILED(hr)) { + hr = CreateMemoryAllocator(&pOutputAllocator); + } + if (SUCCEEDED(hr)) { + m_pTIPFilter->OutputPin()->SetAllocator(pOutputAllocator); + pOutputAllocator->Release(); + } + } + if (pAllocator == pOutputAllocator) { + hr = E_FAIL; + } else if(SUCCEEDED(hr)) { + // Must copy so set the allocator properties on the output + ALLOCATOR_PROPERTIES Props, Actual; + hr = pAllocator->GetProperties(&Props); + if (SUCCEEDED(hr)) { + hr = pOutputAllocator->SetProperties(&Props, &Actual); + } + if (SUCCEEDED(hr)) { + if ( (Props.cBuffers > Actual.cBuffers) + || (Props.cbBuffer > Actual.cbBuffer) + || (Props.cbAlign > Actual.cbAlign) + ) { + hr = E_FAIL; + } + } + + // Set the allocator on the output pin + if (SUCCEEDED(hr)) { + hr = m_pTIPFilter->OutputPin()->ConnectedIMemInputPin() + ->NotifyAllocator( pOutputAllocator, FALSE ); + } + } + } else { + hr = m_pTIPFilter->OutputPin()->ConnectedIMemInputPin() + ->NotifyAllocator( pAllocator, bReadOnly ); + if (SUCCEEDED(hr)) { + m_pTIPFilter->OutputPin()->SetAllocator( pAllocator ); + } + } + + if (SUCCEEDED(hr)) { + + // It's possible that the old and the new are the same thing. + // AddRef before release ensures that we don't unload it. + pAllocator->AddRef(); + + if( m_pAllocator != NULL ) + m_pAllocator->Release(); + + m_pAllocator = pAllocator; // We have an allocator for the input pin + } + + return hr; + +} // NotifyAllocator + + +// EnumMediaTypes +// - pass through to our downstream filter +STDMETHODIMP CTransInPlaceInputPin::EnumMediaTypes( IEnumMediaTypes **ppEnum ) +{ + // Can only pass through if connected + if( !m_pTIPFilter->m_pOutput->IsConnected() ) + return VFW_E_NOT_CONNECTED; + + return m_pTIPFilter->m_pOutput->GetConnected()->EnumMediaTypes( ppEnum ); + +} // EnumMediaTypes + + +// CheckMediaType +// - agree to anything if not connected, +// otherwise pass through to the downstream filter. +// This assumes that the filter does not change the media type. + +HRESULT CTransInPlaceInputPin::CheckMediaType(const CMediaType *pmt ) +{ + HRESULT hr = m_pTIPFilter->CheckInputType(pmt); + if (hr!=S_OK) return hr; + + if( m_pTIPFilter->m_pOutput->IsConnected() ) + return m_pTIPFilter->m_pOutput->GetConnected()->QueryAccept( pmt ); + else + return S_OK; + +} // CheckMediaType + + +// If upstream asks us what our requirements are, we will try to ask downstream +// if that doesn't work, we'll just take the defaults. +STDMETHODIMP +CTransInPlaceInputPin::GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps) +{ + + if( m_pTIPFilter->m_pOutput->IsConnected() ) + return m_pTIPFilter->OutputPin() + ->ConnectedIMemInputPin()->GetAllocatorRequirements( pProps ); + else + return E_NOTIMPL; + +} // GetAllocatorRequirements + + +// CTransInPlaceInputPin::CompleteConnect() calls CBaseInputPin::CompleteConnect() +// and then calls CTransInPlaceFilter::CompleteConnect(). It does this because +// CTransInPlaceFilter::CompleteConnect() can reconnect a pin and we do not +// want to reconnect a pin if CBaseInputPin::CompleteConnect() fails. +HRESULT +CTransInPlaceInputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = CBaseInputPin::CompleteConnect(pReceivePin); + if (FAILED(hr)) { + return hr; + } + + return m_pTransformFilter->CompleteConnect(PINDIR_INPUT,pReceivePin); +} // CompleteConnect + + +// ================================================================= +// Implements the CTransInPlaceOutputPin class +// ================================================================= + + +// constructor + +CTransInPlaceOutputPin::CTransInPlaceOutputPin( + TCHAR *pObjectName, + CTransInPlaceFilter *pFilter, + HRESULT * phr, + LPCWSTR pPinName) + : CTransformOutputPin( pObjectName + , pFilter + , phr + , pPinName), + m_pTIPFilter(pFilter) +{ + DbgLog(( LOG_TRACE, 2 + , TEXT("CTransInPlaceOutputPin::CTransInPlaceOutputPin"))); + +} // constructor + + +// EnumMediaTypes +// - pass through to our upstream filter +STDMETHODIMP CTransInPlaceOutputPin::EnumMediaTypes( IEnumMediaTypes **ppEnum ) +{ + // Can only pass through if connected. + if( ! m_pTIPFilter->m_pInput->IsConnected() ) + return VFW_E_NOT_CONNECTED; + + return m_pTIPFilter->m_pInput->GetConnected()->EnumMediaTypes( ppEnum ); + +} // EnumMediaTypes + + + +// CheckMediaType +// - agree to anything if not connected, +// otherwise pass through to the upstream filter. + +HRESULT CTransInPlaceOutputPin::CheckMediaType(const CMediaType *pmt ) +{ + // Don't accept any output pin type changes if we're copying + // between allocators - it's too late to change the input + // allocator size. + if (m_pTIPFilter->UsingDifferentAllocators() && !m_pFilter->IsStopped()) { + if (*pmt == m_mt) { + return S_OK; + } else { + return VFW_E_TYPE_NOT_ACCEPTED; + } + } + + // Assumes the type does not change. That's why we're calling + // CheckINPUTType here on the OUTPUT pin. + HRESULT hr = m_pTIPFilter->CheckInputType(pmt); + if (hr!=S_OK) return hr; + + if( m_pTIPFilter->m_pInput->IsConnected() ) + return m_pTIPFilter->m_pInput->GetConnected()->QueryAccept( pmt ); + else + return S_OK; + +} // CheckMediaType + + +/* Save the allocator pointer in the output pin +*/ +void +CTransInPlaceOutputPin::SetAllocator(IMemAllocator * pAllocator) +{ + pAllocator->AddRef(); + if (m_pAllocator) { + m_pAllocator->Release(); + } + m_pAllocator = pAllocator; +} // SetAllocator + + +// CTransInPlaceOutputPin::CompleteConnect() calls CBaseOutputPin::CompleteConnect() +// and then calls CTransInPlaceFilter::CompleteConnect(). It does this because +// CTransInPlaceFilter::CompleteConnect() can reconnect a pin and we do not want to +// reconnect a pin if CBaseOutputPin::CompleteConnect() fails. +// CBaseOutputPin::CompleteConnect() often fails when our output pin is being connected +// to the Video Mixing Renderer. +HRESULT +CTransInPlaceOutputPin::CompleteConnect(IPin *pReceivePin) +{ + HRESULT hr = CBaseOutputPin::CompleteConnect(pReceivePin); + if (FAILED(hr)) { + return hr; + } + + return m_pTransformFilter->CompleteConnect(PINDIR_OUTPUT,pReceivePin); +} // CompleteConnect diff --git a/plugins/GSdx/baseclasses/transip.h b/plugins/GSdx/baseclasses/transip.h new file mode 100644 index 0000000000..2c1e5114ac --- /dev/null +++ b/plugins/GSdx/baseclasses/transip.h @@ -0,0 +1,250 @@ +//------------------------------------------------------------------------------ +// File: TransIP.h +// +// Desc: DirectShow base classes - defines classes from which simple +// Transform-In-Place filters may be derived. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// +// The difference between this and Transfrm.h is that Transfrm copies the data. +// +// It assumes the filter has one input and one output stream, and has no +// interest in memory management, interface negotiation or anything else. +// +// Derive your class from this, and supply Transform and the media type/format +// negotiation functions. Implement that class, compile and link and +// you're done. + + +#ifndef __TRANSIP__ +#define __TRANSIP__ + +// ====================================================================== +// This is the com object that represents a simple transform filter. It +// supports IBaseFilter, IMediaFilter and two pins through nested interfaces +// ====================================================================== + +class CTransInPlaceFilter; + +// Several of the pin functions call filter functions to do the work, +// so you can often use the pin classes unaltered, just overriding the +// functions in CTransInPlaceFilter. If that's not enough and you want +// to derive your own pin class, override GetPin in the filter to supply +// your own pin classes to the filter. + +// ================================================== +// Implements the input pin +// ================================================== + +class CTransInPlaceInputPin : public CTransformInputPin +{ + +protected: + CTransInPlaceFilter * const m_pTIPFilter; // our filter + BOOL m_bReadOnly; // incoming stream is read only + +public: + + CTransInPlaceInputPin( + TCHAR *pObjectName, + CTransInPlaceFilter *pFilter, + HRESULT *phr, + LPCWSTR pName); + + // --- IMemInputPin ----- + + // Provide an enumerator for media types by getting one from downstream + STDMETHODIMP EnumMediaTypes( IEnumMediaTypes **ppEnum ); + + // Say whether media type is acceptable. + HRESULT CheckMediaType(const CMediaType* pmt); + + // Return our upstream allocator + STDMETHODIMP GetAllocator(IMemAllocator ** ppAllocator); + + // get told which allocator the upstream output pin is actually + // going to use. + STDMETHODIMP NotifyAllocator(IMemAllocator * pAllocator, + BOOL bReadOnly); + + // Allow the filter to see what allocator we have + // N.B. This does NOT AddRef + IMemAllocator * PeekAllocator() const + { return m_pAllocator; } + + // Pass this on downstream if it ever gets called. + STDMETHODIMP GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps); + + HRESULT CompleteConnect(IPin *pReceivePin); + + inline const BOOL ReadOnly() { return m_bReadOnly ; } + +}; // CTransInPlaceInputPin + +// ================================================== +// Implements the output pin +// ================================================== + +class CTransInPlaceOutputPin : public CTransformOutputPin +{ + +protected: + // m_pFilter points to our CBaseFilter + CTransInPlaceFilter * const m_pTIPFilter; + +public: + + CTransInPlaceOutputPin( + TCHAR *pObjectName, + CTransInPlaceFilter *pFilter, + HRESULT *phr, + LPCWSTR pName); + + + // --- CBaseOutputPin ------------ + + // negotiate the allocator and its buffer size/count + // Insists on using our own allocator. (Actually the one upstream of us). + // We don't override this - instead we just agree the default + // then let the upstream filter decide for itself on reconnect + // virtual HRESULT DecideAllocator(IMemInputPin * pPin, IMemAllocator ** pAlloc); + + // Provide a media type enumerator. Get it from upstream. + STDMETHODIMP EnumMediaTypes( IEnumMediaTypes **ppEnum ); + + // Say whether media type is acceptable. + HRESULT CheckMediaType(const CMediaType* pmt); + + // This just saves the allocator being used on the output pin + // Also called by input pin's GetAllocator() + void SetAllocator(IMemAllocator * pAllocator); + + IMemInputPin * ConnectedIMemInputPin() + { return m_pInputPin; } + + // Allow the filter to see what allocator we have + // N.B. This does NOT AddRef + IMemAllocator * PeekAllocator() const + { return m_pAllocator; } + + HRESULT CompleteConnect(IPin *pReceivePin); + +}; // CTransInPlaceOutputPin + + +class AM_NOVTABLE CTransInPlaceFilter : public CTransformFilter +{ + +public: + + // map getpin/getpincount for base enum of pins to owner + // override this to return more specialised pin objects + + virtual CBasePin *GetPin(int n); + +public: + + // Set bModifiesData == false if your derived filter does + // not modify the data samples (for instance it's just copying + // them somewhere else or looking at the timestamps). + + CTransInPlaceFilter(TCHAR *, LPUNKNOWN, REFCLSID clsid, HRESULT *, + bool bModifiesData = true); +#ifdef UNICODE + CTransInPlaceFilter(CHAR *, LPUNKNOWN, REFCLSID clsid, HRESULT *, + bool bModifiesData = true); +#endif + // The following are defined to avoid undefined pure virtuals. + // Even if they are never called, they will give linkage warnings/errors + + // We override EnumMediaTypes to bypass the transform class enumerator + // which would otherwise call this. + HRESULT GetMediaType(int iPosition, CMediaType *pMediaType) + { DbgBreak("CTransInPlaceFilter::GetMediaType should never be called"); + return E_UNEXPECTED; + } + + // This is called when we actually have to provide out own allocator. + HRESULT DecideBufferSize(IMemAllocator*, ALLOCATOR_PROPERTIES *); + + // The functions which call this in CTransform are overridden in this + // class to call CheckInputType with the assumption that the type + // does not change. In Debug builds some calls will be made and + // we just ensure that they do not assert. + HRESULT CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut) + { + return S_OK; + }; + + + // ================================================================= + // ----- You may want to override this ----------------------------- + // ================================================================= + + HRESULT CompleteConnect(PIN_DIRECTION dir,IPin *pReceivePin); + + // chance to customize the transform process + virtual HRESULT Receive(IMediaSample *pSample); + + // ================================================================= + // ----- You MUST override these ----------------------------------- + // ================================================================= + + virtual HRESULT Transform(IMediaSample *pSample) PURE; + + // this goes in the factory template table to create new instances + // static CCOMObject * CreateInstance(LPUNKNOWN, HRESULT *); + + +#ifdef PERF + // Override to register performance measurement with a less generic string + // You should do this to avoid confusion with other filters + virtual void RegisterPerfId() + {m_idTransInPlace = MSR_REGISTER(TEXT("TransInPlace"));} +#endif // PERF + + +// implementation details + +protected: + + IMediaSample * CTransInPlaceFilter::Copy(IMediaSample *pSource); + +#ifdef PERF + int m_idTransInPlace; // performance measuring id +#endif // PERF + bool m_bModifiesData; // Does this filter change the data? + + // these hold our input and output pins + + friend class CTransInPlaceInputPin; + friend class CTransInPlaceOutputPin; + + CTransInPlaceInputPin *InputPin() const + { + return (CTransInPlaceInputPin *)m_pInput; + }; + CTransInPlaceOutputPin *OutputPin() const + { + return (CTransInPlaceOutputPin *)m_pOutput; + }; + + // Helper to see if the input and output types match + BOOL TypesMatch() + { + return InputPin()->CurrentMediaType() == + OutputPin()->CurrentMediaType(); + } + + // Are the input and output allocators different? + BOOL UsingDifferentAllocators() const + { + return InputPin()->PeekAllocator() != OutputPin()->PeekAllocator(); + } +}; // CTransInPlaceFilter + +#endif /* __TRANSIP__ */ + diff --git a/plugins/GSdx/baseclasses/vtrans.cpp b/plugins/GSdx/baseclasses/vtrans.cpp new file mode 100644 index 0000000000..f93c200dcb --- /dev/null +++ b/plugins/GSdx/baseclasses/vtrans.cpp @@ -0,0 +1,468 @@ +//------------------------------------------------------------------------------ +// File: Vtrans.cpp +// +// Desc: DirectShow base classes. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include "measure.h" +// #include // now in precomp file streams.h + +CVideoTransformFilter::CVideoTransformFilter + ( TCHAR *pName, LPUNKNOWN pUnk, REFCLSID clsid) + : CTransformFilter(pName, pUnk, clsid) + , m_itrLate(0) + , m_nKeyFramePeriod(0) // No QM until we see at least 2 key frames + , m_nFramesSinceKeyFrame(0) + , m_bSkipping(FALSE) + , m_tDecodeStart(0) + , m_itrAvgDecode(300000) // 30mSec - probably allows skipping + , m_bQualityChanged(FALSE) +{ +#ifdef PERF + RegisterPerfId(); +#endif // PERF +} + + +CVideoTransformFilter::~CVideoTransformFilter() +{ + // nothing to do +} + + +// Reset our quality management state + +HRESULT CVideoTransformFilter::StartStreaming() +{ + m_itrLate = 0; + m_nKeyFramePeriod = 0; // No QM until we see at least 2 key frames + m_nFramesSinceKeyFrame = 0; + m_bSkipping = FALSE; + m_tDecodeStart = 0; + m_itrAvgDecode = 300000; // 30mSec - probably allows skipping + m_bQualityChanged = FALSE; + m_bSampleSkipped = FALSE; + return NOERROR; +} + + +// Overriden to reset quality management information + +HRESULT CVideoTransformFilter::EndFlush() +{ + { + // Synchronize + CAutoLock lck(&m_csReceive); + + // Reset our stats + // + // Note - we don't want to call derived classes here, + // we only want to reset our internal variables and this + // is a convenient way to do it + CVideoTransformFilter::StartStreaming(); + } + return CTransformFilter::EndFlush(); +} + + +HRESULT CVideoTransformFilter::AbortPlayback(HRESULT hr) +{ + NotifyEvent(EC_ERRORABORT, hr, 0); + m_pOutput->DeliverEndOfStream(); + return hr; +} + + +// Receive() +// +// Accept a sample from upstream, decide whether to process it +// or drop it. If we process it then get a buffer from the +// allocator of the downstream connection, transform it into the +// new buffer and deliver it to the downstream filter. +// If we decide not to process it then we do not get a buffer. + +// Remember that although this code will notice format changes coming into +// the input pin, it will NOT change its output format if that results +// in the filter needing to make a corresponding output format change. Your +// derived filter will have to take care of that. (eg. a palette change if +// the input and output is an 8 bit format). If the input sample is discarded +// and nothing is sent out for this Receive, please remember to put the format +// change on the first output sample that you actually do send. +// If your filter will produce the same output type even when the input type +// changes, then this base class code will do everything you need. + +HRESULT CVideoTransformFilter::Receive(IMediaSample *pSample) +{ + // If the next filter downstream is the video renderer, then it may + // be able to operate in DirectDraw mode which saves copying the data + // and gives higher performance. In that case the buffer which we + // get from GetDeliveryBuffer will be a DirectDraw buffer, and + // drawing into this buffer draws directly onto the display surface. + // This means that any waiting for the correct time to draw occurs + // during GetDeliveryBuffer, and that once the buffer is given to us + // the video renderer will count it in its statistics as a frame drawn. + // This means that any decision to drop the frame must be taken before + // calling GetDeliveryBuffer. + + ASSERT(CritCheckIn(&m_csReceive)); + AM_MEDIA_TYPE *pmtOut, *pmt; +#ifdef DEBUG + FOURCCMap fccOut; +#endif + HRESULT hr; + ASSERT(pSample); + IMediaSample * pOutSample; + + // If no output pin to deliver to then no point sending us data + ASSERT (m_pOutput != NULL) ; + + // The source filter may dynamically ask us to start transforming from a + // different media type than the one we're using now. If we don't, we'll + // draw garbage. (typically, this is a palette change in the movie, + // but could be something more sinister like the compression type changing, + // or even the video size changing) + +#define rcS1 ((VIDEOINFOHEADER *)(pmt->pbFormat))->rcSource +#define rcT1 ((VIDEOINFOHEADER *)(pmt->pbFormat))->rcTarget + + pSample->GetMediaType(&pmt); + if (pmt != NULL && pmt->pbFormat != NULL) { + + // spew some debug output + ASSERT(!IsEqualGUID(pmt->majortype, GUID_NULL)); +#ifdef DEBUG + fccOut.SetFOURCC(&pmt->subtype); + LONG lCompression = HEADER(pmt->pbFormat)->biCompression; + LONG lBitCount = HEADER(pmt->pbFormat)->biBitCount; + LONG lStride = (HEADER(pmt->pbFormat)->biWidth * lBitCount + 7) / 8; + lStride = (lStride + 3) & ~3; + DbgLog((LOG_TRACE,3,TEXT("*Changing input type on the fly to"))); + DbgLog((LOG_TRACE,3,TEXT("FourCC: %lx Compression: %lx BitCount: %ld"), + fccOut.GetFOURCC(), lCompression, lBitCount)); + DbgLog((LOG_TRACE,3,TEXT("biHeight: %ld rcDst: (%ld, %ld, %ld, %ld)"), + HEADER(pmt->pbFormat)->biHeight, + rcT1.left, rcT1.top, rcT1.right, rcT1.bottom)); + DbgLog((LOG_TRACE,3,TEXT("rcSrc: (%ld, %ld, %ld, %ld) Stride: %ld"), + rcS1.left, rcS1.top, rcS1.right, rcS1.bottom, + lStride)); +#endif + + // now switch to using the new format. I am assuming that the + // derived filter will do the right thing when its media type is + // switched and streaming is restarted. + + StopStreaming(); + m_pInput->CurrentMediaType() = *pmt; + DeleteMediaType(pmt); + // if this fails, playback will stop, so signal an error + hr = StartStreaming(); + if (FAILED(hr)) { + return AbortPlayback(hr); + } + } + + // Now that we have noticed any format changes on the input sample, it's + // OK to discard it. + + if (ShouldSkipFrame(pSample)) { + MSR_NOTE(m_idSkip); + m_bSampleSkipped = TRUE; + return NOERROR; + } + + // Set up the output sample + hr = InitializeOutputSample(pSample, &pOutSample); + + if (FAILED(hr)) { + return hr; + } + + m_bSampleSkipped = FALSE; + + // The renderer may ask us to on-the-fly to start transforming to a + // different format. If we don't obey it, we'll draw garbage + +#define rcS ((VIDEOINFOHEADER *)(pmtOut->pbFormat))->rcSource +#define rcT ((VIDEOINFOHEADER *)(pmtOut->pbFormat))->rcTarget + + pOutSample->GetMediaType(&pmtOut); + if (pmtOut != NULL && pmtOut->pbFormat != NULL) { + + // spew some debug output + ASSERT(!IsEqualGUID(pmtOut->majortype, GUID_NULL)); +#ifdef DEBUG + fccOut.SetFOURCC(&pmtOut->subtype); + LONG lCompression = HEADER(pmtOut->pbFormat)->biCompression; + LONG lBitCount = HEADER(pmtOut->pbFormat)->biBitCount; + LONG lStride = (HEADER(pmtOut->pbFormat)->biWidth * lBitCount + 7) / 8; + lStride = (lStride + 3) & ~3; + DbgLog((LOG_TRACE,3,TEXT("*Changing output type on the fly to"))); + DbgLog((LOG_TRACE,3,TEXT("FourCC: %lx Compression: %lx BitCount: %ld"), + fccOut.GetFOURCC(), lCompression, lBitCount)); + DbgLog((LOG_TRACE,3,TEXT("biHeight: %ld rcDst: (%ld, %ld, %ld, %ld)"), + HEADER(pmtOut->pbFormat)->biHeight, + rcT.left, rcT.top, rcT.right, rcT.bottom)); + DbgLog((LOG_TRACE,3,TEXT("rcSrc: (%ld, %ld, %ld, %ld) Stride: %ld"), + rcS.left, rcS.top, rcS.right, rcS.bottom, + lStride)); +#endif + + // now switch to using the new format. I am assuming that the + // derived filter will do the right thing when its media type is + // switched and streaming is restarted. + + StopStreaming(); + m_pOutput->CurrentMediaType() = *pmtOut; + DeleteMediaType(pmtOut); + hr = StartStreaming(); + + if (SUCCEEDED(hr)) { + // a new format, means a new empty buffer, so wait for a keyframe + // before passing anything on to the renderer. + // !!! a keyframe may never come, so give up after 30 frames + DbgLog((LOG_TRACE,3,TEXT("Output format change means we must wait for a keyframe"))); + m_nWaitForKey = 30; + + // if this fails, playback will stop, so signal an error + } else { + + // Must release the sample before calling AbortPlayback + // because we might be holding the win16 lock or + // ddraw lock + pOutSample->Release(); + AbortPlayback(hr); + return hr; + } + } + + // After a discontinuity, we need to wait for the next key frame + if (pSample->IsDiscontinuity() == S_OK) { + DbgLog((LOG_TRACE,3,TEXT("Non-key discontinuity - wait for keyframe"))); + m_nWaitForKey = 30; + } + + // Start timing the transform (and log it if PERF is defined) + + if (SUCCEEDED(hr)) { + m_tDecodeStart = timeGetTime(); + MSR_START(m_idTransform); + + // have the derived class transform the data + hr = Transform(pSample, pOutSample); + + // Stop the clock (and log it if PERF is defined) + MSR_STOP(m_idTransform); + m_tDecodeStart = timeGetTime()-m_tDecodeStart; + m_itrAvgDecode = m_tDecodeStart*(10000/16) + 15*(m_itrAvgDecode/16); + + // Maybe we're waiting for a keyframe still? + if (m_nWaitForKey) + m_nWaitForKey--; + if (m_nWaitForKey && pSample->IsSyncPoint() == S_OK) + m_nWaitForKey = FALSE; + + // if so, then we don't want to pass this on to the renderer + if (m_nWaitForKey && hr == NOERROR) { + DbgLog((LOG_TRACE,3,TEXT("still waiting for a keyframe"))); + hr = S_FALSE; + } + } + + if (FAILED(hr)) { + DbgLog((LOG_TRACE,1,TEXT("Error from video transform"))); + } else { + // the Transform() function can return S_FALSE to indicate that the + // sample should not be delivered; we only deliver the sample if it's + // really S_OK (same as NOERROR, of course.) + // Try not to return S_FALSE to a direct draw buffer (it's wasteful) + // Try to take the decision earlier - before you get it. + + if (hr == NOERROR) { + hr = m_pOutput->Deliver(pOutSample); + } else { + // S_FALSE returned from Transform is a PRIVATE agreement + // We should return NOERROR from Receive() in this case because returning S_FALSE + // from Receive() means that this is the end of the stream and no more data should + // be sent. + if (S_FALSE == hr) { + + // We must Release() the sample before doing anything + // like calling the filter graph because having the + // sample means we may have the DirectDraw lock + // (== win16 lock on some versions) + pOutSample->Release(); + m_bSampleSkipped = TRUE; + if (!m_bQualityChanged) { + m_bQualityChanged = TRUE; + NotifyEvent(EC_QUALITY_CHANGE,0,0); + } + return NOERROR; + } + } + } + + // release the output buffer. If the connected pin still needs it, + // it will have addrefed it itself. + pOutSample->Release(); + ASSERT(CritCheckIn(&m_csReceive)); + + return hr; +} + + + +BOOL CVideoTransformFilter::ShouldSkipFrame( IMediaSample * pIn) +{ + REFERENCE_TIME trStart, trStopAt; + HRESULT hr = pIn->GetTime(&trStart, &trStopAt); + + // Don't skip frames with no timestamps + if (hr != S_OK) + return FALSE; + + int itrFrame = (int)(trStopAt - trStart); // frame duration + + if(S_OK==pIn->IsSyncPoint()) { + MSR_INTEGER(m_idFrameType, 1); + if ( m_nKeyFramePeriod < m_nFramesSinceKeyFrame ) { + // record the max + m_nKeyFramePeriod = m_nFramesSinceKeyFrame; + } + m_nFramesSinceKeyFrame = 0; + m_bSkipping = FALSE; + } else { + MSR_INTEGER(m_idFrameType, 2); + if ( m_nFramesSinceKeyFrame>m_nKeyFramePeriod + && m_nKeyFramePeriod>0 + ) { + // We haven't seen the key frame yet, but we were clearly being + // overoptimistic about how frequent they are. + m_nKeyFramePeriod = m_nFramesSinceKeyFrame; + } + } + + + // Whatever we might otherwise decide, + // if we are taking only a small fraction of the required frame time to decode + // then any quality problems are actually coming from somewhere else. + // Could be a net problem at the source for instance. In this case there's + // no point in us skipping frames here. + if (m_itrAvgDecode*4>itrFrame) { + + // Don't skip unless we are at least a whole frame late. + // (We would skip B frames if more than 1/2 frame late, but they're safe). + if ( m_itrLate > itrFrame ) { + + // Don't skip unless the anticipated key frame would be no more than + // 1 frame early. If the renderer has not been waiting (we *guess* + // it hasn't because we're late) then it will allow frames to be + // played early by up to a frame. + + // Let T = Stream time from now to anticipated next key frame + // = (frame duration) * (KeyFramePeriod - FramesSinceKeyFrame) + // So we skip if T - Late < one frame i.e. + // (duration) * (freq - FramesSince) - Late < duration + // or (duration) * (freq - FramesSince - 1) < Late + + // We don't dare skip until we have seen some key frames and have + // some idea how often they occur and they are reasonably frequent. + if (m_nKeyFramePeriod>0) { + // It would be crazy - but we could have a stream with key frames + // a very long way apart - and if they are further than about + // 3.5 minutes apart then we could get arithmetic overflow in + // reference time units. Therefore we switch to mSec at this point + int it = (itrFrame/10000) + * (m_nKeyFramePeriod-m_nFramesSinceKeyFrame - 1); + MSR_INTEGER(m_idTimeTillKey, it); + + // For debug - might want to see the details - dump them as scratch pad +#ifdef VTRANSPERF + MSR_INTEGER(0, itrFrame); + MSR_INTEGER(0, m_nFramesSinceKeyFrame); + MSR_INTEGER(0, m_nKeyFramePeriod); +#endif + if (m_itrLate/10000 > it) { + m_bSkipping = TRUE; + // Now we are committed. Once we start skipping, we + // cannot stop until we hit a key frame. + } else { +#ifdef VTRANSPERF + MSR_INTEGER(0, 777770); // not near enough to next key +#endif + } + } else { +#ifdef VTRANSPERF + MSR_INTEGER(0, 777771); // Next key not predictable +#endif + } + } else { +#ifdef VTRANSPERF + MSR_INTEGER(0, 777772); // Less than one frame late + MSR_INTEGER(0, m_itrLate); + MSR_INTEGER(0, itrFrame); +#endif + } + } else { +#ifdef VTRANSPERF + MSR_INTEGER(0, 777773); // Decode time short - not not worth skipping + MSR_INTEGER(0, m_itrAvgDecode); + MSR_INTEGER(0, itrFrame); +#endif + } + + ++m_nFramesSinceKeyFrame; + + if (m_bSkipping) { + // We will count down the lateness as we skip each frame. + // We re-assess each frame. The key frame might not arrive when expected. + // We reset m_itrLate if we get a new Quality message, but actually that's + // not likely because we're not sending frames on to the Renderer. In + // fact if we DID get another one it would mean that there's a long + // pipe between us and the renderer and we might need an altogether + // better strategy to avoid hunting! + m_itrLate = m_itrLate - itrFrame; + } + + MSR_INTEGER(m_idLate, (int)m_itrLate/10000 ); // Note how late we think we are + if (m_bSkipping) { + if (!m_bQualityChanged) { + m_bQualityChanged = TRUE; + NotifyEvent(EC_QUALITY_CHANGE,0,0); + } + } + return m_bSkipping; +} + + +HRESULT CVideoTransformFilter::AlterQuality(Quality q) +{ + // to reduce the amount of 64 bit arithmetic, m_itrLate is an int. + // +, -, >, == etc are not too bad, but * and / are painful. + if (m_itrLate>300000000) { + // Avoid overflow and silliness - more than 30 secs late is already silly + m_itrLate = 300000000; + } else { + m_itrLate = (int)q.Late; + } + // We ignore the other fields + + // We're actually not very good at handling this. In non-direct draw mode + // most of the time can be spent in the renderer which can skip any frame. + // In that case we'd rather the renderer handled things. + // Nevertheless we will keep an eye on it and if we really start getting + // a very long way behind then we will actually skip - but we'll still tell + // the renderer (or whoever is downstream) that they should handle quality. + + return E_FAIL; // Tell the renderer to do his thing. + +} + + + +// This will avoid several hundred useless warnings if compiled -W4 by MS VC++ v4 +#pragma warning(disable:4514) + diff --git a/plugins/GSdx/baseclasses/vtrans.h b/plugins/GSdx/baseclasses/vtrans.h new file mode 100644 index 0000000000..a9c1d6bde1 --- /dev/null +++ b/plugins/GSdx/baseclasses/vtrans.h @@ -0,0 +1,143 @@ +//------------------------------------------------------------------------------ +// File: VTrans.h +// +// Desc: DirectShow base classes - defines a video transform class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// This class is derived from CTransformFilter, but is specialised to handle +// the requirements of video quality control by frame dropping. +// This is a non-in-place transform, (i.e. it copies the data) such as a decoder. + +class CVideoTransformFilter : public CTransformFilter +{ + public: + + CVideoTransformFilter(TCHAR *, LPUNKNOWN, REFCLSID clsid); + ~CVideoTransformFilter(); + HRESULT EndFlush(); + + // ================================================================= + // ----- override these bits --------------------------------------- + // ================================================================= + // The following methods are in CTransformFilter which is inherited. + // They are mentioned here for completeness + // + // These MUST be supplied in a derived class + // + // NOTE: + // virtual HRESULT Transform(IMediaSample * pIn, IMediaSample *pOut); + // virtual HRESULT CheckInputType(const CMediaType* mtIn) PURE; + // virtual HRESULT CheckTransform + // (const CMediaType* mtIn, const CMediaType* mtOut) PURE; + // static CCOMObject * CreateInstance(LPUNKNOWN, HRESULT *); + // virtual HRESULT DecideBufferSize + // (IMemAllocator * pAllocator, ALLOCATOR_PROPERTIES *pprop) PURE; + // virtual HRESULT GetMediaType(int iPosition, CMediaType *pMediaType) PURE; + // + // These MAY also be overridden + // + // virtual HRESULT StopStreaming(); + // virtual HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt); + // virtual HRESULT CheckConnect(PIN_DIRECTION dir,IPin *pPin); + // virtual HRESULT BreakConnect(PIN_DIRECTION dir); + // virtual HRESULT CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin); + // virtual HRESULT EndOfStream(void); + // virtual HRESULT BeginFlush(void); + // virtual HRESULT EndFlush(void); + // virtual HRESULT NewSegment + // (REFERENCE_TIME tStart,REFERENCE_TIME tStop,double dRate); +#ifdef PERF + + // If you override this - ensure that you register all these ids + // as well as any of your own, + virtual void RegisterPerfId() { + m_idSkip = MSR_REGISTER(TEXT("Video Transform Skip frame")); + m_idFrameType = MSR_REGISTER(TEXT("Video transform frame type")); + m_idLate = MSR_REGISTER(TEXT("Video Transform Lateness")); + m_idTimeTillKey = MSR_REGISTER(TEXT("Video Transform Estd. time to next key")); + CTransformFilter::RegisterPerfId(); + } +#endif + + protected: + + // =========== QUALITY MANAGEMENT IMPLEMENTATION ======================== + // Frames are assumed to come in three types: + // Type 1: an AVI key frame or an MPEG I frame. + // This frame can be decoded with no history. + // Dropping this frame means that no further frame can be decoded + // until the next type 1 frame. + // Type 1 frames are sync points. + // Type 2: an AVI non-key frame or an MPEG P frame. + // This frame cannot be decoded unless the previous type 1 frame was + // decoded and all type 2 frames since have been decoded. + // Dropping this frame means that no further frame can be decoded + // until the next type 1 frame. + // Type 3: An MPEG B frame. + // This frame cannot be decoded unless the previous type 1 or 2 frame + // has been decoded AND the subsequent type 1 or 2 frame has also + // been decoded. (This requires decoding the frames out of sequence). + // Dropping this frame affects no other frames. This implementation + // does not allow for these. All non-sync-point frames are treated + // as being type 2. + // + // The spacing of frames of type 1 in a file is not guaranteed. There MUST + // be a type 1 frame at (well, near) the start of the file in order to start + // decoding at all. After that there could be one every half second or so, + // there could be one at the start of each scene (aka "cut", "shot") or + // there could be no more at all. + // If there is only a single type 1 frame then NO FRAMES CAN BE DROPPED + // without losing all the rest of the movie. There is no way to tell whether + // this is the case, so we find that we are in the gambling business. + // To try to improve the odds, we record the greatest interval between type 1s + // that we have seen and we bet on things being no worse than this in the + // future. + + // You can tell if it's a type 1 frame by calling IsSyncPoint(). + // there is no architected way to test for a type 3, so you should override + // the quality management here if you have B-frames. + + int m_nKeyFramePeriod; // the largest observed interval between type 1 frames + // 1 means every frame is type 1, 2 means every other. + + int m_nFramesSinceKeyFrame; // Used to count frames since the last type 1. + // becomes the new m_nKeyFramePeriod if greater. + + BOOL m_bSkipping; // we are skipping to the next type 1 frame + +#ifdef PERF + int m_idFrameType; // MSR id Frame type. 1=Key, 2="non-key" + int m_idSkip; // MSR id skipping + int m_idLate; // MSR id lateness + int m_idTimeTillKey; // MSR id for guessed time till next key frame. +#endif + + virtual HRESULT StartStreaming(); + + HRESULT AbortPlayback(HRESULT hr); // if something bad happens + + HRESULT Receive(IMediaSample *pSample); + + HRESULT AlterQuality(Quality q); + + BOOL ShouldSkipFrame(IMediaSample * pIn); + + int m_itrLate; // lateness from last Quality message + // (this overflows at 214 secs late). + int m_tDecodeStart; // timeGetTime when decode started. + int m_itrAvgDecode; // Average decode time in reference units. + + BOOL m_bNoSkip; // debug - no skipping. + + // We send an EC_QUALITY_CHANGE notification to the app if we have to degrade. + // We send one when we start degrading, not one for every frame, this means + // we track whether we've sent one yet. + BOOL m_bQualityChanged; + + // When non-zero, don't pass anything to renderer until next keyframe + // If there are few keys, give up and eventually draw something + int m_nWaitForKey; +}; diff --git a/plugins/GSdx/baseclasses/wxdebug.cpp b/plugins/GSdx/baseclasses/wxdebug.cpp new file mode 100644 index 0000000000..3cb0b42ce7 --- /dev/null +++ b/plugins/GSdx/baseclasses/wxdebug.cpp @@ -0,0 +1,1418 @@ +//------------------------------------------------------------------------------ +// File: WXDebug.cpp +// +// Desc: DirectShow base classes - implements ActiveX system debugging +// facilities. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" +#include +#include + +#ifdef DEBUG +#ifdef UNICODE +#ifndef _UNICODE +#define _UNICODE +#endif // _UNICODE +#endif // UNICODE +#endif // DEBUG + +#ifdef DEBUG + +// The Win32 wsprintf() function writes a maximum of 1024 characters to it's output buffer. +// See the documentation for wsprintf()'s lpOut parameter for more information. +const INT iDEBUGINFO = 1024; // Used to format strings + +/* For every module and executable we store a debugging level for each of + the five categories (eg LOG_ERROR and LOG_TIMING). This makes it easy + to isolate and debug individual modules without seeing everybody elses + spurious debug output. The keys are stored in the registry under the + HKEY_LOCAL_MACHINE\SOFTWARE\Debug\\ key values + NOTE these must be in the same order as their enumeration definition */ + +TCHAR *pKeyNames[] = { + TEXT("TIMING"), // Timing and performance measurements + TEXT("TRACE"), // General step point call tracing + TEXT("MEMORY"), // Memory and object allocation/destruction + TEXT("LOCKING"), // Locking/unlocking of critical sections + TEXT("ERROR"), // Debug error notification + TEXT("CUSTOM1"), + TEXT("CUSTOM2"), + TEXT("CUSTOM3"), + TEXT("CUSTOM4"), + TEXT("CUSTOM5") + }; + +const TCHAR CAutoTrace::_szEntering[] = TEXT("->: %s"); +const TCHAR CAutoTrace::_szLeaving[] = TEXT("<-: %s"); + +const INT iMAXLEVELS = NUMELMS(pKeyNames); // Maximum debug categories + +HINSTANCE m_hInst; // Module instance handle +TCHAR m_ModuleName[iDEBUGINFO]; // Cut down module name +DWORD m_Levels[iMAXLEVELS]; // Debug level per category +CRITICAL_SECTION m_CSDebug; // Controls access to list +DWORD m_dwNextCookie; // Next active object ID +ObjectDesc *pListHead = NULL; // First active object +DWORD m_dwObjectCount; // Active object count +BOOL m_bInit = FALSE; // Have we been initialised +HANDLE m_hOutput = INVALID_HANDLE_VALUE; // Optional output written here +DWORD dwWaitTimeout = INFINITE; // Default timeout value +DWORD dwTimeOffset; // Time of first DbgLog call +bool g_fUseKASSERT = false; // don't create messagebox +bool g_fDbgInDllEntryPoint = false; +bool g_fAutoRefreshLevels = false; + +const TCHAR *pBaseKey = TEXT("SOFTWARE\\Debug"); +const TCHAR *pGlobalKey = TEXT("GLOBAL"); +static CHAR *pUnknownName = "UNKNOWN"; + +TCHAR *TimeoutName = TEXT("TIMEOUT"); + +/* This sets the instance handle that the debug library uses to find + the module's file name from the Win32 GetModuleFileName function */ + +void WINAPI DbgInitialise(HINSTANCE hInst) +{ + InitializeCriticalSection(&m_CSDebug); + m_bInit = TRUE; + + m_hInst = hInst; + DbgInitModuleName(); + if (GetProfileInt(m_ModuleName, TEXT("BreakOnLoad"), 0)) + DebugBreak(); + DbgInitModuleSettings(false); + DbgInitGlobalSettings(true); + dwTimeOffset = timeGetTime(); +} + + +/* This is called to clear up any resources the debug library uses - at the + moment we delete our critical section and the object list. The values we + retrieve from the registry are all done during initialisation but we don't + go looking for update notifications while we are running, if the values + are changed then the application has to be restarted to pick them up */ + +void WINAPI DbgTerminate() +{ + if (m_hOutput != INVALID_HANDLE_VALUE) { + EXECUTE_ASSERT(CloseHandle(m_hOutput)); + m_hOutput = INVALID_HANDLE_VALUE; + } + DeleteCriticalSection(&m_CSDebug); + m_bInit = FALSE; +} + + +/* This is called by DbgInitLogLevels to read the debug settings + for each logging category for this module from the registry */ + +void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax) +{ + LONG lReturn; // Create key return value + LONG lKeyPos; // Current key category + DWORD dwKeySize; // Size of the key value + DWORD dwKeyType; // Receives it's type + DWORD dwKeyValue; // This fields value + + /* Try and read a value for each key position in turn */ + for (lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) { + + dwKeySize = sizeof(DWORD); + lReturn = RegQueryValueEx( + hKey, // Handle to an open key + pKeyNames[lKeyPos], // Subkey name derivation + NULL, // Reserved field + &dwKeyType, // Returns the field type + (LPBYTE) &dwKeyValue, // Returns the field's value + &dwKeySize ); // Number of bytes transferred + + /* If either the key was not available or it was not a DWORD value + then we ensure only the high priority debug logging is output + but we try and update the field to a zero filled DWORD value */ + + if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) { + + dwKeyValue = 0; + lReturn = RegSetValueEx( + hKey, // Handle of an open key + pKeyNames[lKeyPos], // Address of subkey name + (DWORD) 0, // Reserved field + REG_DWORD, // Type of the key field + (PBYTE) &dwKeyValue, // Value for the field + sizeof(DWORD)); // Size of the field buffer + + if (lReturn != ERROR_SUCCESS) { + DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos])); + dwKeyValue = 0; + } + } + if(fTakeMax) + { + m_Levels[lKeyPos] = max(dwKeyValue,m_Levels[lKeyPos]); + } + else + { + if((m_Levels[lKeyPos] & LOG_FORCIBLY_SET) == 0) { + m_Levels[lKeyPos] = dwKeyValue; + } + } + } + + /* Read the timeout value for catching hangs */ + dwKeySize = sizeof(DWORD); + lReturn = RegQueryValueEx( + hKey, // Handle to an open key + TimeoutName, // Subkey name derivation + NULL, // Reserved field + &dwKeyType, // Returns the field type + (LPBYTE) &dwWaitTimeout, // Returns the field's value + &dwKeySize ); // Number of bytes transferred + + /* If either the key was not available or it was not a DWORD value + then we ensure only the high priority debug logging is output + but we try and update the field to a zero filled DWORD value */ + + if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) { + + dwWaitTimeout = INFINITE; + lReturn = RegSetValueEx( + hKey, // Handle of an open key + TimeoutName, // Address of subkey name + (DWORD) 0, // Reserved field + REG_DWORD, // Type of the key field + (PBYTE) &dwWaitTimeout, // Value for the field + sizeof(DWORD)); // Size of the field buffer + + if (lReturn != ERROR_SUCCESS) { + DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),TimeoutName)); + dwWaitTimeout = INFINITE; + } + } +} + +void WINAPI DbgOutString(LPCTSTR psz) +{ + if (m_hOutput != INVALID_HANDLE_VALUE) { + UINT cb = lstrlen(psz); + DWORD dw; +#ifdef UNICODE + CHAR szDest[2048]; + WideCharToMultiByte(CP_ACP, 0, psz, -1, szDest, NUMELMS(szDest), 0, 0); + WriteFile (m_hOutput, szDest, cb, &dw, NULL); +#else + WriteFile (m_hOutput, psz, cb, &dw, NULL); +#endif + } else { + OutputDebugString (psz); + } +} + +/* Called by DbgInitGlobalSettings to setup alternate logging destinations + */ + +void WINAPI DbgInitLogTo ( + HKEY hKey) +{ + LONG lReturn; + DWORD dwKeyType; + DWORD dwKeySize; + TCHAR szFile[MAX_PATH] = {0}; + static const TCHAR cszKey[] = TEXT("LogToFile"); + + dwKeySize = MAX_PATH; + lReturn = RegQueryValueEx( + hKey, // Handle to an open key + cszKey, // Subkey name derivation + NULL, // Reserved field + &dwKeyType, // Returns the field type + (LPBYTE) szFile, // Returns the field's value + &dwKeySize); // Number of bytes transferred + + // create an empty key if it does not already exist + // + if (lReturn != ERROR_SUCCESS || dwKeyType != REG_SZ) + { + dwKeySize = sizeof(TCHAR); + lReturn = RegSetValueEx( + hKey, // Handle of an open key + cszKey, // Address of subkey name + (DWORD) 0, // Reserved field + REG_SZ, // Type of the key field + (PBYTE)szFile, // Value for the field + dwKeySize); // Size of the field buffer + } + + // if an output-to was specified. try to open it. + // + if (m_hOutput != INVALID_HANDLE_VALUE) { + EXECUTE_ASSERT(CloseHandle (m_hOutput)); + m_hOutput = INVALID_HANDLE_VALUE; + } + if (szFile[0] != 0) + { + if (!lstrcmpi(szFile, TEXT("Console"))) { + m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE); + if (m_hOutput == INVALID_HANDLE_VALUE) { + AllocConsole (); + m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE); + } + SetConsoleTitle (TEXT("ActiveX Debug Output")); + } else if (szFile[0] && + lstrcmpi(szFile, TEXT("Debug")) && + lstrcmpi(szFile, TEXT("Debugger")) && + lstrcmpi(szFile, TEXT("Deb"))) + { + m_hOutput = CreateFile(szFile, GENERIC_WRITE, + FILE_SHARE_READ, + NULL, OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (INVALID_HANDLE_VALUE != m_hOutput) + { + static const TCHAR cszBar[] = TEXT("\r\n\r\n=====DbgInitialize()=====\r\n\r\n"); + SetFilePointer (m_hOutput, 0, NULL, FILE_END); + DbgOutString (cszBar); + } + } + } +} + + + +/* This is called by DbgInitLogLevels to read the global debug settings for + each logging category for this module from the registry. Normally each + module has it's own values set for it's different debug categories but + setting the global SOFTWARE\Debug\Global applies them to ALL modules */ + +void WINAPI DbgInitGlobalSettings(bool fTakeMax) +{ + LONG lReturn; // Create key return value + TCHAR szInfo[iDEBUGINFO]; // Constructs key names + HKEY hGlobalKey; // Global override key + + /* Construct the global base key name */ + (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%s\\%s"),pBaseKey,pGlobalKey); + + /* Create or open the key for this module */ + lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key + szInfo, // Address of subkey name + (DWORD) 0, // Reserved value + NULL, // Address of class name + (DWORD) 0, // Special options flags + KEY_ALL_ACCESS, // Desired security access + NULL, // Key security descriptor + &hGlobalKey, // Opened handle buffer + NULL); // What really happened + + if (lReturn != ERROR_SUCCESS) { + DbgLog((LOG_ERROR,0,TEXT("Could not access GLOBAL module key"))); + return; + } + + DbgInitKeyLevels(hGlobalKey, fTakeMax); + RegCloseKey(hGlobalKey); +} + + +/* This sets the debugging log levels for the different categories. We start + by opening (or creating if not already available) the SOFTWARE\Debug key + that all these settings live under. We then look at the global values + set under SOFTWARE\Debug\Global which apply on top of the individual + module settings. We then load the individual module registry settings */ + +void WINAPI DbgInitModuleSettings(bool fTakeMax) +{ + LONG lReturn; // Create key return value + TCHAR szInfo[iDEBUGINFO]; // Constructs key names + HKEY hModuleKey; // Module key handle + + /* Construct the base key name */ + (void)StringCchPrintf(szInfo,NUMELMS(szInfo), TEXT("%s\\%s"),pBaseKey,m_ModuleName); + + /* Create or open the key for this module */ + lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key + szInfo, // Address of subkey name + (DWORD) 0, // Reserved value + NULL, // Address of class name + (DWORD) 0, // Special options flags + KEY_ALL_ACCESS, // Desired security access + NULL, // Key security descriptor + &hModuleKey, // Opened handle buffer + NULL); // What really happened + + if (lReturn != ERROR_SUCCESS) { + DbgLog((LOG_ERROR,0,TEXT("Could not access module key"))); + return; + } + + DbgInitLogTo(hModuleKey); + DbgInitKeyLevels(hModuleKey, fTakeMax); + RegCloseKey(hModuleKey); +} + + +/* Initialise the module file name */ + +void WINAPI DbgInitModuleName() +{ + TCHAR FullName[iDEBUGINFO]; // Load the full path and module name + TCHAR *pName; // Searches from the end for a backslash + + GetModuleFileName(m_hInst,FullName,iDEBUGINFO); + pName = _tcsrchr(FullName,'\\'); + if (pName == NULL) { + pName = FullName; + } else { + pName++; + } + (void)StringCchCopy(m_ModuleName,NUMELMS(m_ModuleName), pName); +} + +struct MsgBoxMsg +{ + HWND hwnd; + TCHAR *szTitle; + TCHAR *szMessage; + DWORD dwFlags; + INT iResult; +}; + +// +// create a thread to call MessageBox(). calling MessageBox() on +// random threads at bad times can confuse the host (eg IE). +// +DWORD WINAPI MsgBoxThread( + LPVOID lpParameter // thread data + ) +{ + MsgBoxMsg *pmsg = (MsgBoxMsg *)lpParameter; + pmsg->iResult = MessageBox( + pmsg->hwnd, + pmsg->szTitle, + pmsg->szMessage, + pmsg->dwFlags); + + return 0; +} + +INT MessageBoxOtherThread( + HWND hwnd, + TCHAR *szTitle, + TCHAR *szMessage, + DWORD dwFlags) +{ + if(g_fDbgInDllEntryPoint) + { + // can't wait on another thread because we have the loader + // lock held in the dll entry point. + return MessageBox(hwnd, szTitle, szMessage, dwFlags); + } + else + { + MsgBoxMsg msg = {hwnd, szTitle, szMessage, dwFlags, 0}; + DWORD dwid; + HANDLE hThread = CreateThread( + 0, // security + 0, // stack size + MsgBoxThread, + (void *)&msg, // arg + 0, // flags + &dwid); + if(hThread) + { + WaitForSingleObject(hThread, INFINITE); + CloseHandle(hThread); + return msg.iResult; + } + + // break into debugger on failure. + return IDCANCEL; + } +} + +/* Displays a message box if the condition evaluated to FALSE */ + +void WINAPI DbgAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine) +{ + if(g_fUseKASSERT) + { + DbgKernelAssert(pCondition, pFileName, iLine); + } + else + { + + TCHAR szInfo[iDEBUGINFO]; + + (void)StringCchPrintf(szInfo, NUMELMS(szInfo), TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"), + pCondition, iLine, pFileName); + + INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"), + MB_SYSTEMMODAL | + MB_ICONHAND | + MB_YESNOCANCEL | + MB_SETFOREGROUND); + switch (MsgId) + { + case IDNO: /* Kill the application */ + + FatalAppExit(FALSE, TEXT("Application terminated")); + break; + + case IDCANCEL: /* Break into the debugger */ + + DebugBreak(); + break; + + case IDYES: /* Ignore assertion continue execution */ + break; + } + } +} + +/* Displays a message box at a break point */ + +void WINAPI DbgBreakPoint(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine) +{ + if(g_fUseKASSERT) + { + DbgKernelAssert(pCondition, pFileName, iLine); + } + else + { + TCHAR szInfo[iDEBUGINFO]; + + (void)StringCchPrintf(szInfo, NUMELMS(szInfo), TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"), + pCondition, iLine, pFileName); + + INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"), + MB_SYSTEMMODAL | + MB_ICONHAND | + MB_YESNOCANCEL | + MB_SETFOREGROUND); + switch (MsgId) + { + case IDNO: /* Kill the application */ + + FatalAppExit(FALSE, TEXT("Application terminated")); + break; + + case IDCANCEL: /* Break into the debugger */ + + DebugBreak(); + break; + + case IDYES: /* Ignore break point continue execution */ + break; + } + } +} + +void WINAPI DbgBreakPoint(const TCHAR *pFileName,INT iLine,const TCHAR* szFormatString,...) +{ + // A debug break point message can have at most 2000 characters if + // ANSI or UNICODE characters are being used. A debug break point message + // can have between 1000 and 2000 double byte characters in it. If a + // particular message needs more characters, then the value of this constant + // should be increased. + const DWORD MAX_BREAK_POINT_MESSAGE_SIZE = 2000; + + TCHAR szBreakPointMessage[MAX_BREAK_POINT_MESSAGE_SIZE]; + + const DWORD MAX_CHARS_IN_BREAK_POINT_MESSAGE = sizeof(szBreakPointMessage) / sizeof(TCHAR); + + va_list va; + va_start( va, szFormatString ); + + HRESULT hr = StringCchVPrintf( szBreakPointMessage, MAX_CHARS_IN_BREAK_POINT_MESSAGE, szFormatString, va ); + + va_end(va); + + if( S_OK != hr ) { + DbgBreak( "ERROR in DbgBreakPoint(). The variable length debug message could not be displayed because _vsnprintf() failed." ); + return; + } + + ::DbgBreakPoint( szBreakPointMessage, pFileName, iLine ); +} + + +/* When we initialised the library we stored in the m_Levels array the current + debug output level for this module for each of the five categories. When + some debug logging is sent to us it can be sent with a combination of the + categories (if it is applicable to many for example) in which case we map + the type's categories into their current debug levels and see if any of + them can be accepted. The function looks at each bit position in turn from + the input type field and then compares it's debug level with the modules. + + A level of 0 means that output is always sent to the debugger. This is + due to producing output if the input level is <= m_Levels. +*/ + + +BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level) +{ + if(g_fAutoRefreshLevels) + { + // re-read the registry every second. We cannot use RegNotify() to + // notice registry changes because it's not available on win9x. + static DWORD g_dwLastRefresh = 0; + DWORD dwTime = timeGetTime(); + if(dwTime - g_dwLastRefresh > 1000) { + g_dwLastRefresh = dwTime; + + // there's a race condition: multiple threads could update the + // values. plus read and write not synchronized. no harm + // though. + DbgInitModuleSettings(false); + } + } + + + DWORD Mask = 0x01; + + // If no valid bits are set return FALSE + if ((Type & ((1<m_szName = szObjectName; + pObject->m_wszName = wszObjectName; + pObject->m_dwCookie = ++m_dwNextCookie; + pObject->m_pNext = pListHead; + + pListHead = pObject; + m_dwObjectCount++; + + DWORD ObjectCookie = pObject->m_dwCookie; + ASSERT(ObjectCookie); + + if(wszObjectName) { + DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%ls) %d Active"), + pObject->m_dwCookie, wszObjectName, m_dwObjectCount)); + } else { + DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%hs) %d Active"), + pObject->m_dwCookie, szObjectName, m_dwObjectCount)); + } + + LeaveCriticalSection(&m_CSDebug); + return ObjectCookie; +} + + +/* This is called by the CBaseObject destructor when an object is about to be + destroyed, we are passed the cookie we returned during construction that + identifies this object. We scan the object list for a matching cookie and + remove the object if successful. We also update the active object count */ + +BOOL WINAPI DbgRegisterObjectDestruction(DWORD dwCookie) +{ + /* Grab the list critical section */ + EnterCriticalSection(&m_CSDebug); + + ObjectDesc *pObject = pListHead; + ObjectDesc *pPrevious = NULL; + + /* Scan the object list looking for a cookie match */ + + while (pObject) { + if (pObject->m_dwCookie == dwCookie) { + break; + } + pPrevious = pObject; + pObject = pObject->m_pNext; + } + + if (pObject == NULL) { + DbgBreak("Apparently destroying a bogus object"); + LeaveCriticalSection(&m_CSDebug); + return FALSE; + } + + /* Is the object at the head of the list */ + + if (pPrevious == NULL) { + pListHead = pObject->m_pNext; + } else { + pPrevious->m_pNext = pObject->m_pNext; + } + + /* Delete the object and update the housekeeping information */ + + m_dwObjectCount--; + + if(pObject->m_wszName) { + DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%ls) %d Active"), + pObject->m_dwCookie, pObject->m_wszName, m_dwObjectCount)); + } else { + DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%hs) %d Active"), + pObject->m_dwCookie, pObject->m_szName, m_dwObjectCount)); + } + + delete pObject; + LeaveCriticalSection(&m_CSDebug); + return TRUE; +} + + +/* This runs through the active object list displaying their details */ + +void WINAPI DbgDumpObjectRegister() +{ + TCHAR szInfo[iDEBUGINFO]; + + /* Grab the list critical section */ + + EnterCriticalSection(&m_CSDebug); + ObjectDesc *pObject = pListHead; + + /* Scan the object list displaying the name and cookie */ + + DbgLog((LOG_MEMORY,2,TEXT(""))); + DbgLog((LOG_MEMORY,2,TEXT(" ID Object Description"))); + DbgLog((LOG_MEMORY,2,TEXT(""))); + + while (pObject) { + if(pObject->m_wszName) { + #ifdef UNICODE + LPCTSTR FORMAT_STRING = TEXT("%5d (%8x) %30s"); + #else + LPCTSTR FORMAT_STRING = TEXT("%5d (%8x) %30S"); + #endif + + (void)StringCchPrintf(szInfo,NUMELMS(szInfo), FORMAT_STRING, pObject->m_dwCookie, &pObject, pObject->m_wszName); + + } else { + #ifdef UNICODE + LPCTSTR FORMAT_STRING = TEXT("%5d (%8x) %30S"); + #else + LPCTSTR FORMAT_STRING = TEXT("%5d (%8x) %30s"); + #endif + + (void)StringCchPrintf(szInfo,NUMELMS(szInfo),FORMAT_STRING,pObject->m_dwCookie, &pObject, pObject->m_szName); + } + DbgLog((LOG_MEMORY,2,szInfo)); + pObject = pObject->m_pNext; + } + + (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("Total object count %5d"),m_dwObjectCount); + DbgLog((LOG_MEMORY,2,TEXT(""))); + DbgLog((LOG_MEMORY,1,szInfo)); + LeaveCriticalSection(&m_CSDebug); +} + +/* Debug infinite wait stuff */ +DWORD WINAPI DbgWaitForSingleObject(HANDLE h) +{ + DWORD dwWaitResult; + do { + dwWaitResult = WaitForSingleObject(h, dwWaitTimeout); + ASSERT(dwWaitResult == WAIT_OBJECT_0); + } while (dwWaitResult == WAIT_TIMEOUT); + return dwWaitResult; +} +DWORD WINAPI DbgWaitForMultipleObjects(DWORD nCount, + CONST HANDLE *lpHandles, + BOOL bWaitAll) +{ + DWORD dwWaitResult; + do { + dwWaitResult = WaitForMultipleObjects(nCount, + lpHandles, + bWaitAll, + dwWaitTimeout); + ASSERT((DWORD)(dwWaitResult - WAIT_OBJECT_0) < MAXIMUM_WAIT_OBJECTS); + } while (dwWaitResult == WAIT_TIMEOUT); + return dwWaitResult; +} + +void WINAPI DbgSetWaitTimeout(DWORD dwTimeout) +{ + dwWaitTimeout = dwTimeout; +} + +#endif /* DEBUG */ + +#ifdef _OBJBASE_H_ + + /* Stuff for printing out our GUID names */ + + GUID_STRING_ENTRY g_GuidNames[] = { + #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ + { #name, { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } }, + #include + }; + + CGuidNameList GuidNames; + int g_cGuidNames = sizeof(g_GuidNames) / sizeof(g_GuidNames[0]); + + char *CGuidNameList::operator [] (const GUID &guid) + { + for (int i = 0; i < g_cGuidNames; i++) { + if (g_GuidNames[i].guid == guid) { + return g_GuidNames[i].szName; + } + } + if (guid == GUID_NULL) { + return "GUID_NULL"; + } + + // !!! add something to print FOURCC guids? + + // shouldn't this print the hex CLSID? + return "Unknown GUID Name"; + } + +#endif /* _OBJBASE_H_ */ + +/* CDisp class - display our data types */ + +// clashes with REFERENCE_TIME +CDisp::CDisp(LONGLONG ll, int Format) +{ + // note: this could be combined with CDisp(LONGLONG) by + // introducing a default format of CDISP_REFTIME + LARGE_INTEGER li; + li.QuadPart = ll; + switch (Format) { + case CDISP_DEC: + { + TCHAR temp[20]; + int pos=20; + temp[--pos] = 0; + int digit; + // always output at least one digit + do { + // Get the rightmost digit - we only need the low word + digit = li.LowPart % 10; + li.QuadPart /= 10; + temp[--pos] = (TCHAR) digit+L'0'; + } while (li.QuadPart); + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%s"), temp+pos); + break; + } + case CDISP_HEX: + default: + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("0x%X%8.8X"), li.HighPart, li.LowPart); + } +}; + +CDisp::CDisp(REFCLSID clsid) +{ + WCHAR strClass[CHARS_IN_GUID+1]; + StringFromGUID2(clsid, strClass, sizeof(strClass) / sizeof(strClass[0])); + ASSERT(sizeof(m_String)/sizeof(m_String[0]) >= CHARS_IN_GUID+1); + #ifdef UNICODE + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%s"), strClass); + #else + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%S"), strClass); + #endif +}; + +#ifdef __STREAMS__ +/* Display stuff */ +CDisp::CDisp(CRefTime llTime) +{ + LPTSTR lpsz = m_String; + size_t len = NUMELMS(m_String); + LONGLONG llDiv; + if (llTime < 0) { + llTime = -llTime; + (void)StringCchPrintf(lpsz, len, TEXT("-")); + size_t t = lstrlen(lpsz); + lpsz += t; + len -= t; + } + llDiv = (LONGLONG)24 * 3600 * 10000000; + if (llTime >= llDiv) { + (void)StringCchPrintf(lpsz, len, TEXT("%d days "), (LONG)(llTime / llDiv)); + size_t t = lstrlen(lpsz); + lpsz += t; + len -= t; + llTime = llTime % llDiv; + } + llDiv = (LONGLONG)3600 * 10000000; + if (llTime >= llDiv) { + (void)StringCchPrintf(lpsz, len, TEXT("%d hrs "), (LONG)(llTime / llDiv)); + size_t t = lstrlen(lpsz); + lpsz += t; + len -= t; + llTime = llTime % llDiv; + } + llDiv = (LONGLONG)60 * 10000000; + if (llTime >= llDiv) { + (void)StringCchPrintf(lpsz, len, TEXT("%d mins "), (LONG)(llTime / llDiv)); + size_t t = lstrlen(lpsz); + lpsz += t; + len -= t; + llTime = llTime % llDiv; + } + (void)StringCchPrintf(lpsz, len, TEXT("%d.%3.3d sec"), + (LONG)llTime / 10000000, + (LONG)((llTime % 10000000) / 10000)); +}; + +#endif // __STREAMS__ + + +/* Display pin */ +CDisp::CDisp(IPin *pPin) +{ + PIN_INFO pi; + TCHAR str[MAX_PIN_NAME]; + CLSID clsid; + + if (pPin) { + pPin->QueryPinInfo(&pi); + pi.pFilter->GetClassID(&clsid); + QueryPinInfoReleaseFilter(pi); + #ifndef UNICODE + WideCharToMultiByte(GetACP(), 0, pi.achName, lstrlenW(pi.achName) + 1, + str, MAX_PIN_NAME, NULL, NULL); + #else + (void)StringCchCopy(str, NUMELMS(str), pi.achName); + #endif + } else { + (void)StringCchCopy(str, NUMELMS(str), TEXT("NULL IPin")); + } + + size_t len = lstrlen(str)+64; + m_pString = (TCHAR*) new TCHAR[len]; + if (!m_pString) { + return; + } + + #ifdef UNICODE + LPCTSTR FORMAT_STRING = TEXT("%S(%s)"); + #else + LPCTSTR FORMAT_STRING = TEXT("%s(%s)"); + #endif + + (void)StringCchPrintf(m_pString, len, FORMAT_STRING, GuidNames[clsid], str); +} + +/* Display filter or pin */ +CDisp::CDisp(IUnknown *pUnk) +{ + IBaseFilter *pf; + HRESULT hr = pUnk->QueryInterface(IID_IBaseFilter, (void **)&pf); + if(SUCCEEDED(hr)) + { + FILTER_INFO fi; + hr = pf->QueryFilterInfo(&fi); + if(SUCCEEDED(hr)) + { + QueryFilterInfoReleaseGraph(fi); + + size_t len = lstrlenW(fi.achName) + 1; + m_pString = new TCHAR[len]; + if(m_pString) + { + #ifdef UNICODE + LPCTSTR FORMAT_STRING = TEXT("%s"); + #else + LPCTSTR FORMAT_STRING = TEXT("%S"); + #endif + + (void)StringCchPrintf(m_pString, len, FORMAT_STRING, fi.achName); + } + } + + pf->Release(); + + return; + } + + IPin *pp; + hr = pUnk->QueryInterface(IID_IPin, (void **)&pp); + if(SUCCEEDED(hr)) + { + CDisp::CDisp(pp); + pp->Release(); + return; + } +} + + +CDisp::~CDisp() +{ +} + +CDispBasic::~CDispBasic() +{ + if (m_pString != m_String) { + delete [] m_pString; + } +} + +CDisp::CDisp(double d) +{ +#ifdef DEBUG + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%.16g"), d); +#else + (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%d.%03d"), (int) d, (int) ((d - (int) d) * 1000)); +#endif +} + + +/* If built for debug this will display the media type details. We convert the + major and subtypes into strings and also ask the base classes for a string + description of the subtype, so MEDIASUBTYPE_RGB565 becomes RGB 565 16 bit + We also display the fields in the BITMAPINFOHEADER structure, this should + succeed as we do not accept input types unless the format is big enough */ + +#ifdef DEBUG +void WINAPI DisplayType(LPTSTR label, const AM_MEDIA_TYPE *pmtIn, DWORD dwLevel) +{ + + /* Dump the GUID types and a short description */ + + DbgLog((LOG_TRACE,dwLevel,TEXT(""))); + DbgLog((LOG_TRACE,dwLevel,TEXT("%s M type %hs S type %hs"), label, + GuidNames[pmtIn->majortype], + GuidNames[pmtIn->subtype])); + DbgLog((LOG_TRACE,dwLevel,TEXT("Subtype description %s"),GetSubtypeName(&pmtIn->subtype))); + + /* Dump the generic media types */ + + if (pmtIn->bTemporalCompression) { + DbgLog((LOG_TRACE,dwLevel,TEXT("Temporally compressed"))); + } else { + DbgLog((LOG_TRACE,dwLevel,TEXT("Not temporally compressed"))); + } + + if (pmtIn->bFixedSizeSamples) { + DbgLog((LOG_TRACE,dwLevel,TEXT("Sample size %d"),pmtIn->lSampleSize)); + } else { + DbgLog((LOG_TRACE,dwLevel,TEXT("Variable size samples"))); + } + + if (pmtIn->formattype == FORMAT_VideoInfo) { + /* Dump the contents of the BITMAPINFOHEADER structure */ + BITMAPINFOHEADER *pbmi = HEADER(pmtIn->pbFormat); + VIDEOINFOHEADER *pVideoInfo = (VIDEOINFOHEADER *)pmtIn->pbFormat; + + DbgLog((LOG_TRACE,dwLevel,TEXT("Source rectangle (Left %d Top %d Right %d Bottom %d)"), + pVideoInfo->rcSource.left, + pVideoInfo->rcSource.top, + pVideoInfo->rcSource.right, + pVideoInfo->rcSource.bottom)); + + DbgLog((LOG_TRACE,dwLevel,TEXT("Target rectangle (Left %d Top %d Right %d Bottom %d)"), + pVideoInfo->rcTarget.left, + pVideoInfo->rcTarget.top, + pVideoInfo->rcTarget.right, + pVideoInfo->rcTarget.bottom)); + + DbgLog((LOG_TRACE,dwLevel,TEXT("Size of BITMAPINFO structure %d"),pbmi->biSize)); + if (pbmi->biCompression < 256) { + DbgLog((LOG_TRACE,dwLevel,TEXT("%dx%dx%d bit (%d)"), + pbmi->biWidth, pbmi->biHeight, + pbmi->biBitCount, pbmi->biCompression)); + } else { + DbgLog((LOG_TRACE,dwLevel,TEXT("%dx%dx%d bit '%4.4hs'"), + pbmi->biWidth, pbmi->biHeight, + pbmi->biBitCount, &pbmi->biCompression)); + } + + DbgLog((LOG_TRACE,dwLevel,TEXT("Image size %d"),pbmi->biSizeImage)); + DbgLog((LOG_TRACE,dwLevel,TEXT("Planes %d"),pbmi->biPlanes)); + DbgLog((LOG_TRACE,dwLevel,TEXT("X Pels per metre %d"),pbmi->biXPelsPerMeter)); + DbgLog((LOG_TRACE,dwLevel,TEXT("Y Pels per metre %d"),pbmi->biYPelsPerMeter)); + DbgLog((LOG_TRACE,dwLevel,TEXT("Colours used %d"),pbmi->biClrUsed)); + + } else if (pmtIn->majortype == MEDIATYPE_Audio) { + DbgLog((LOG_TRACE,dwLevel,TEXT(" Format type %hs"), + GuidNames[pmtIn->formattype])); + DbgLog((LOG_TRACE,dwLevel,TEXT(" Subtype %hs"), + GuidNames[pmtIn->subtype])); + + if ((pmtIn->subtype != MEDIASUBTYPE_MPEG1Packet) + && (pmtIn->cbFormat >= sizeof(PCMWAVEFORMAT))) + { + /* Dump the contents of the WAVEFORMATEX type-specific format structure */ + + WAVEFORMATEX *pwfx = (WAVEFORMATEX *) pmtIn->pbFormat; + DbgLog((LOG_TRACE,dwLevel,TEXT("wFormatTag %u"), pwfx->wFormatTag)); + DbgLog((LOG_TRACE,dwLevel,TEXT("nChannels %u"), pwfx->nChannels)); + DbgLog((LOG_TRACE,dwLevel,TEXT("nSamplesPerSec %lu"), pwfx->nSamplesPerSec)); + DbgLog((LOG_TRACE,dwLevel,TEXT("nAvgBytesPerSec %lu"), pwfx->nAvgBytesPerSec)); + DbgLog((LOG_TRACE,dwLevel,TEXT("nBlockAlign %u"), pwfx->nBlockAlign)); + DbgLog((LOG_TRACE,dwLevel,TEXT("wBitsPerSample %u"), pwfx->wBitsPerSample)); + + /* PCM uses a WAVEFORMAT and does not have the extra size field */ + + if (pmtIn->cbFormat >= sizeof(WAVEFORMATEX)) { + DbgLog((LOG_TRACE,dwLevel,TEXT("cbSize %u"), pwfx->cbSize)); + } + } else { + } + + } else { + DbgLog((LOG_TRACE,dwLevel,TEXT(" Format type %hs"), + GuidNames[pmtIn->formattype])); + // !!!! should add code to dump wave format, others + } +} + + +void WINAPI DumpGraph(IFilterGraph *pGraph, DWORD dwLevel) +{ + if( !pGraph ) + { + return; + } + + IEnumFilters *pFilters; + + DbgLog((LOG_TRACE,dwLevel,TEXT("DumpGraph [%x]"), pGraph)); + + if (FAILED(pGraph->EnumFilters(&pFilters))) { + DbgLog((LOG_TRACE,dwLevel,TEXT("EnumFilters failed!"))); + } + + IBaseFilter *pFilter; + ULONG n; + while (pFilters->Next(1, &pFilter, &n) == S_OK) { + FILTER_INFO info; + + if (FAILED(pFilter->QueryFilterInfo(&info))) { + DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%x] -- failed QueryFilterInfo"), pFilter)); + } else { + QueryFilterInfoReleaseGraph(info); + + // !!! should QueryVendorInfo here! + + DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%x] '%ls'"), pFilter, info.achName)); + + IEnumPins *pins; + + if (FAILED(pFilter->EnumPins(&pins))) { + DbgLog((LOG_TRACE,dwLevel,TEXT("EnumPins failed!"))); + } else { + + IPin *pPin; + while (pins->Next(1, &pPin, &n) == S_OK) { + PIN_INFO info; + + if (FAILED(pPin->QueryPinInfo(&info))) { + DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%x] -- failed QueryPinInfo"), pPin)); + } else { + QueryPinInfoReleaseFilter(info); + + IPin *pPinConnected = NULL; + + HRESULT hr = pPin->ConnectedTo(&pPinConnected); + + if (pPinConnected) { + DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%x] '%ls' [%sput]") + TEXT(" Connected to pin [%x]"), + pPin, info.achName, + info.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out"), + pPinConnected)); + + pPinConnected->Release(); + + // perhaps we should really dump the type both ways as a sanity + // check? + if (info.dir == PINDIR_OUTPUT) { + AM_MEDIA_TYPE mt; + + hr = pPin->ConnectionMediaType(&mt); + + if (SUCCEEDED(hr)) { + DisplayType(TEXT("Connection type"), &mt); + + FreeMediaType(mt); + } + } + } else { + DbgLog((LOG_TRACE,dwLevel, + TEXT(" Pin [%x] '%ls' [%sput]"), + pPin, info.achName, + info.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out"))); + + } + } + + pPin->Release(); + + } + + pins->Release(); + } + + } + + pFilter->Release(); + } + + pFilters->Release(); + +} + +#endif + diff --git a/plugins/GSdx/baseclasses/wxdebug.h b/plugins/GSdx/baseclasses/wxdebug.h new file mode 100644 index 0000000000..6a8b1ff0a4 --- /dev/null +++ b/plugins/GSdx/baseclasses/wxdebug.h @@ -0,0 +1,393 @@ +//------------------------------------------------------------------------------ +// File: WXDebug.h +// +// Desc: DirectShow base classes - provides debugging facilities. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __WXDEBUG__ +#define __WXDEBUG__ + +// This library provides fairly straight forward debugging functionality, this +// is split into two main sections. The first is assertion handling, there are +// three types of assertions provided here. The most commonly used one is the +// ASSERT(condition) macro which will pop up a message box including the file +// and line number if the condition evaluates to FALSE. Then there is the +// EXECUTE_ASSERT macro which is the same as ASSERT except the condition will +// still be executed in NON debug builds. The final type of assertion is the +// KASSERT macro which is more suitable for pure (perhaps kernel) filters as +// the condition is printed onto the debugger rather than in a message box. +// +// The other part of the debug module facilties is general purpose logging. +// This is accessed by calling DbgLog(). The function takes a type and level +// field which define the type of informational string you are presenting and +// it's relative importance. The type field can be a combination (one or more) +// of LOG_TIMING, LOG_TRACE, LOG_MEMORY, LOG_LOCKING and LOG_ERROR. The level +// is a DWORD value where zero defines highest important. Use of zero as the +// debug logging level is to be encouraged ONLY for major errors or events as +// they will ALWAYS be displayed on the debugger. Other debug output has it's +// level matched against the current debug output level stored in the registry +// for this module and if less than the current setting it will be displayed. +// +// Each module or executable has it's own debug output level for each of the +// five types. These are read in when the DbgInitialise function is called +// for DLLs linking to STRMBASE.LIB this is done automatically when the DLL +// is loaded, executables must call it explicitely with the module instance +// handle given to them through the WINMAIN entry point. An executable must +// also call DbgTerminate when they have finished to clean up the resources +// the debug library uses, once again this is done automatically for DLLs + +// These are the five different categories of logging information + +enum { LOG_TIMING = 0x01, // Timing and performance measurements + LOG_TRACE = 0x02, // General step point call tracing + LOG_MEMORY = 0x04, // Memory and object allocation/destruction + LOG_LOCKING = 0x08, // Locking/unlocking of critical sections + LOG_ERROR = 0x10, // Debug error notification + LOG_CUSTOM1 = 0x20, + LOG_CUSTOM2 = 0x40, + LOG_CUSTOM3 = 0x80, + LOG_CUSTOM4 = 0x100, + LOG_CUSTOM5 = 0x200, +}; + +#define LOG_FORCIBLY_SET 0x80000000 + +enum { CDISP_HEX = 0x01, + CDISP_DEC = 0x02}; + +// For each object created derived from CBaseObject (in debug builds) we +// create a descriptor that holds it's name (statically allocated memory) +// and a cookie we assign it. We keep a list of all the active objects +// we have registered so that we can dump a list of remaining objects + +typedef struct tag_ObjectDesc { + const CHAR *m_szName; + const WCHAR *m_wszName; + DWORD m_dwCookie; + tag_ObjectDesc *m_pNext; +} ObjectDesc; + +#define DLLIMPORT __declspec(dllimport) +#define DLLEXPORT __declspec(dllexport) + +#ifdef DEBUG + + #define NAME(x) TEXT(x) + + // These are used internally by the debug library (PRIVATE) + + void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax); + void WINAPI DbgInitGlobalSettings(bool fTakeMax); + void WINAPI DbgInitModuleSettings(bool fTakeMax); + void WINAPI DbgInitModuleName(); + DWORD WINAPI DbgRegisterObjectCreation( + const CHAR *szObjectName, const WCHAR *wszObjectName); + + BOOL WINAPI DbgRegisterObjectDestruction(DWORD dwCookie); + + // These are the PUBLIC entry points + + BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level); + void WINAPI DbgSetModuleLevel(DWORD Type,DWORD Level); + void WINAPI DbgSetAutoRefreshLevels(bool fAuto); + + // Initialise the library with the module handle + + void WINAPI DbgInitialise(HINSTANCE hInst); + void WINAPI DbgTerminate(); + + void WINAPI DbgDumpObjectRegister(); + + // Display error and logging to the user + + void WINAPI DbgAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine); + void WINAPI DbgBreakPoint(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine); + void WINAPI DbgBreakPoint(const TCHAR *pFileName,INT iLine,const TCHAR* szFormatString,...); + + void WINAPI DbgKernelAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine); + void WINAPI DbgLogInfo(DWORD Type,DWORD Level,const TCHAR *pFormat,...); +#ifdef UNICODE + void WINAPI DbgLogInfo(DWORD Type,DWORD Level,const CHAR *pFormat,...); + void WINAPI DbgAssert(const CHAR *pCondition,const CHAR *pFileName,INT iLine); + void WINAPI DbgBreakPoint(const CHAR *pCondition,const CHAR *pFileName,INT iLine); + void WINAPI DbgKernelAssert(const CHAR *pCondition,const CHAR *pFileName,INT iLine); +#endif + void WINAPI DbgOutString(LPCTSTR psz); + + // Debug infinite wait stuff + DWORD WINAPI DbgWaitForSingleObject(HANDLE h); + DWORD WINAPI DbgWaitForMultipleObjects(DWORD nCount, + CONST HANDLE *lpHandles, + BOOL bWaitAll); + void WINAPI DbgSetWaitTimeout(DWORD dwTimeout); + +#ifdef __strmif_h__ + // Display a media type: Terse at level 2, verbose at level 5 + void WINAPI DisplayType(LPTSTR label, const AM_MEDIA_TYPE *pmtIn, DWORD dwLevel = 5); + + // Dump lots of information about a filter graph + void WINAPI DumpGraph(IFilterGraph *pGraph, DWORD dwLevel); +#endif + + #define KASSERT(_x_) if (!(_x_)) \ + DbgKernelAssert(TEXT(#_x_),TEXT(__FILE__),__LINE__) + + // Break on the debugger without putting up a message box + // message goes to debugger instead + + #define KDbgBreak(_x_) \ + DbgKernelAssert(TEXT(#_x_),TEXT(__FILE__),__LINE__) + + // We chose a common name for our ASSERT macro, MFC also uses this name + // So long as the implementation evaluates the condition and handles it + // then we will be ok. Rather than override the behaviour expected we + // will leave whatever first defines ASSERT as the handler (i.e. MFC) + #ifndef ASSERT + #define ASSERT(_x_) if (!(_x_)) \ + DbgAssert(TEXT(#_x_),TEXT(__FILE__),__LINE__) + #endif + + #define DbgAssertAligned( _ptr_, _alignment_ ) ASSERT( ((DWORD_PTR) (_ptr_)) % (_alignment_) == 0) + + // Put up a message box informing the user of a halt + // condition in the program + + #define DbgBreak(_x_) \ + DbgBreakPoint(TEXT(#_x_),TEXT(__FILE__),__LINE__) + + #define EXECUTE_ASSERT(_x_) ASSERT(_x_) + #define DbgLog(_x_) DbgLogInfo _x_ + // MFC style trace macros + + #define NOTE(_x_) DbgLog((LOG_TRACE,5,TEXT(_x_))) + #define NOTE1(_x_,a) DbgLog((LOG_TRACE,5,TEXT(_x_),a)) + #define NOTE2(_x_,a,b) DbgLog((LOG_TRACE,5,TEXT(_x_),a,b)) + #define NOTE3(_x_,a,b,c) DbgLog((LOG_TRACE,5,TEXT(_x_),a,b,c)) + #define NOTE4(_x_,a,b,c,d) DbgLog((LOG_TRACE,5,TEXT(_x_),a,b,c,d)) + #define NOTE5(_x_,a,b,c,d,e) DbgLog((LOG_TRACE,5,TEXT(_x_),a,b,c,d,e)) + +#else + + // Retail builds make public debug functions inert - WARNING the source + // files do not define or build any of the entry points in debug builds + // (public entry points compile to nothing) so if you go trying to call + // any of the private entry points in your source they won't compile + + #define NAME(_x_) ((TCHAR *) NULL) + + #define DbgInitialise(hInst) + #define DbgTerminate() + #define DbgLog(_x_) 0 + #define DbgOutString(psz) + #define DbgAssertAligned( _ptr_, _alignment_ ) 0 + + #define DbgRegisterObjectCreation(pObjectName) + #define DbgRegisterObjectDestruction(dwCookie) + #define DbgDumpObjectRegister() + + #define DbgCheckModuleLevel(Type,Level) + #define DbgSetModuleLevel(Type,Level) + #define DbgSetAutoRefreshLevels(fAuto) + + #define DbgWaitForSingleObject(h) WaitForSingleObject(h, INFINITE) + #define DbgWaitForMultipleObjects(nCount, lpHandles, bWaitAll) \ + WaitForMultipleObjects(nCount, lpHandles, bWaitAll, INFINITE) + #define DbgSetWaitTimeout(dwTimeout) + + #define KDbgBreak(_x_) + #define DbgBreak(_x_) + + #define KASSERT(_x_) ((void)0) + #ifndef ASSERT + #define ASSERT(_x_) ((void)0) + #endif + #define EXECUTE_ASSERT(_x_) ((void)(_x_)) + + // MFC style trace macros + + #define NOTE(_x_) ((void)0) + #define NOTE1(_x_,a) ((void)0) + #define NOTE2(_x_,a,b) ((void)0) + #define NOTE3(_x_,a,b,c) ((void)0) + #define NOTE4(_x_,a,b,c,d) ((void)0) + #define NOTE5(_x_,a,b,c,d,e) ((void)0) + + #define DisplayType(label, pmtIn) ((void)0) + #define DumpGraph(pGraph, label) ((void)0) +#endif + + +// Checks a pointer which should be non NULL - can be used as follows. + +#define CheckPointer(p,ret) {if((p)==NULL) return (ret);} + +// HRESULT Foo(VOID *pBar) +// { +// CheckPointer(pBar,E_INVALIDARG) +// } +// +// Or if the function returns a boolean +// +// BOOL Foo(VOID *pBar) +// { +// CheckPointer(pBar,FALSE) +// } + +// These validate pointers when symbol VFWROBUST is defined +// This will normally be defined in debug not retail builds + +#ifdef DEBUG + #define VFWROBUST +#endif + +#ifdef VFWROBUST + + #define ValidateReadPtr(p,cb) \ + {if(IsBadReadPtr((PVOID)p,cb) == TRUE) \ + DbgBreak("Invalid read pointer");} + + #define ValidateWritePtr(p,cb) \ + {if(IsBadWritePtr((PVOID)p,cb) == TRUE) \ + DbgBreak("Invalid write pointer");} + + #define ValidateReadWritePtr(p,cb) \ + {ValidateReadPtr(p,cb) ValidateWritePtr(p,cb)} + + #define ValidateStringPtr(p) \ + {if(IsBadStringPtr((LPCTSTR)p,INFINITE) == TRUE) \ + DbgBreak("Invalid string pointer");} + + #define ValidateStringPtrA(p) \ + {if(IsBadStringPtrA((LPCSTR)p,INFINITE) == TRUE) \ + DbgBreak("Invalid ANSI string pointer");} + + #define ValidateStringPtrW(p) \ + {if(IsBadStringPtrW((LPCWSTR)p,INFINITE) == TRUE) \ + DbgBreak("Invalid UNICODE string pointer");} + +#else + #define ValidateReadPtr(p,cb) 0 + #define ValidateWritePtr(p,cb) 0 + #define ValidateReadWritePtr(p,cb) 0 + #define ValidateStringPtr(p) 0 + #define ValidateStringPtrA(p) 0 + #define ValidateStringPtrW(p) 0 +#endif + + +#ifdef _OBJBASE_H_ + + // Outputting GUID names. If you want to include the name + // associated with a GUID (eg CLSID_...) then + // + // GuidNames[yourGUID] + // + // Returns the name defined in uuids.h as a string + + typedef struct { + CHAR *szName; + GUID guid; + } GUID_STRING_ENTRY; + + class CGuidNameList { + public: + CHAR *operator [] (const GUID& guid); + }; + + extern CGuidNameList GuidNames; + +#endif + +#ifndef REMIND + // REMIND macro - generates warning as reminder to complete coding + // (eg) usage: + // + // #pragma message (REMIND("Add automation support")) + + + #define QUOTE(x) #x + #define QQUOTE(y) QUOTE(y) + #define REMIND(str) __FILE__ "(" QQUOTE(__LINE__) ") : " str +#endif + +// Method to display objects in a useful format +// +// eg If you want to display a LONGLONG ll in a debug string do (eg) +// +// DbgLog((LOG_TRACE, n, TEXT("Value is %s"), (LPCTSTR)CDisp(ll, CDISP_HEX))); + + +class CDispBasic +{ +public: + CDispBasic() { m_pString = m_String; }; + ~CDispBasic(); +protected: + TCHAR* m_pString; // normally points to m_String... unless too much data + TCHAR m_String[50]; +}; +class CDisp : public CDispBasic +{ +public: + CDisp(LONGLONG ll, int Format = CDISP_HEX); // Display a LONGLONG in CDISP_HEX or CDISP_DEC form + CDisp(REFCLSID clsid); // Display a GUID + CDisp(double d); // Display a floating point number +#ifdef __strmif_h__ +#ifdef __STREAMS__ + CDisp(CRefTime t); // Display a Reference Time +#endif + CDisp(IPin *pPin); // Display a pin as {filter clsid}(pin name) + CDisp(IUnknown *pUnk); // Display a filter or pin +#endif // __strmif_h__ + ~CDisp(); + + // Implement cast to (LPCTSTR) as parameter to logger + operator LPCTSTR() + { + return (LPCTSTR)m_pString; + }; +}; + + +#if defined(DEBUG) +class CAutoTrace +{ +private: + const TCHAR* _szBlkName; + const int _level; + static const TCHAR _szEntering[]; + static const TCHAR _szLeaving[]; +public: + CAutoTrace(const TCHAR* szBlkName, const int level = 15) + : _szBlkName(szBlkName), _level(level) + {DbgLog((LOG_TRACE, _level, _szEntering, _szBlkName));} + + ~CAutoTrace() + {DbgLog((LOG_TRACE, _level, _szLeaving, _szBlkName));} +}; + +#if defined (__FUNCTION__) + +#define AMTRACEFN() CAutoTrace __trace(TEXT(__FUNCTION__)) +#define AMTRACE(_x_) CAutoTrace __trace(TEXT(__FUNCTION__)) + +#else + +#define AMTRACE(_x_) CAutoTrace __trace _x_ +#define AMTRACEFN() + +#endif + +#else + +#define AMTRACE(_x_) +#define AMTRACEFN() + +#endif + +#endif // __WXDEBUG__ + + diff --git a/plugins/GSdx/baseclasses/wxlist.cpp b/plugins/GSdx/baseclasses/wxlist.cpp new file mode 100644 index 0000000000..27c3a5b8c5 --- /dev/null +++ b/plugins/GSdx/baseclasses/wxlist.cpp @@ -0,0 +1,885 @@ +//------------------------------------------------------------------------------ +// File: WXList.cpp +// +// Desc: DirectShow base classes - implements a non-MFC based generic list +// template class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +/* A generic list of pointers to objects. + Objectives: avoid using MFC libraries in ndm kernel mode and + provide a really useful list type. + + The class is thread safe in that separate threads may add and + delete items in the list concurrently although the application + must ensure that constructor and destructor access is suitably + synchronised. + + The list name must not conflict with MFC classes as an + application may use both + + The nodes form a doubly linked, NULL terminated chain with an anchor + block (the list object per se) holding pointers to the first and last + nodes and a count of the nodes. + There is a node cache to reduce the allocation and freeing overhead. + It optionally (determined at construction time) has an Event which is + set whenever the list becomes non-empty and reset whenever it becomes + empty. + It optionally (determined at construction time) has a Critical Section + which is entered during the important part of each operation. (About + all you can do outside it is some parameter checking). + + The node cache is a repository of nodes that are NOT in the list to speed + up storage allocation. Each list has its own cache to reduce locking and + serialising. The list accesses are serialised anyway for a given list - a + common cache would mean that we would have to separately serialise access + of all lists within the cache. Because the cache only stores nodes that are + not in the list, releasing the cache does not release any list nodes. This + means that list nodes can be copied or rechained from one list to another + without danger of creating a dangling reference if the original cache goes + away. + + Questionable design decisions: + 1. Retaining the warts for compatibility + 2. Keeping an element count -i.e. counting whenever we do anything + instead of only when we want the count. + 3. Making the chain pointers NULL terminated. If the list object + itself looks just like a node and the list is kept as a ring then + it reduces the number of special cases. All inserts look the same. +*/ + + +#include "streams.h" + +/* set cursor to the position of each element of list in turn */ +#define INTERNALTRAVERSELIST(list, cursor) \ +for ( cursor = (list).GetHeadPositionI() \ + ; cursor!=NULL \ + ; cursor = (list).Next(cursor) \ + ) + + +/* set cursor to the position of each element of list in turn + in reverse order +*/ +#define INTERNALREVERSETRAVERSELIST(list, cursor) \ +for ( cursor = (list).GetTailPositionI() \ + ; cursor!=NULL \ + ; cursor = (list).Prev(cursor) \ + ) + +/* Constructor calls a separate initialisation function that + creates a node cache, optionally creates a lock object + and optionally creates a signaling object. + + By default we create a locking object, a DEFAULTCACHE sized + cache but no event object so the list cannot be used in calls + to WaitForSingleObject +*/ +CBaseList::CBaseList(TCHAR *pName, // Descriptive list name + INT iItems) : // Node cache size +#ifdef DEBUG + CBaseObject(pName), +#endif + m_pFirst(NULL), + m_pLast(NULL), + m_Count(0), + m_Cache(iItems) +{ +} // constructor + +CBaseList::CBaseList(TCHAR *pName) : // Descriptive list name +#ifdef DEBUG + CBaseObject(pName), +#endif + m_pFirst(NULL), + m_pLast(NULL), + m_Count(0), + m_Cache(DEFAULTCACHE) +{ +} // constructor + +#ifdef UNICODE +CBaseList::CBaseList(CHAR *pName, // Descriptive list name + INT iItems) : // Node cache size +#ifdef DEBUG + CBaseObject(pName), +#endif + m_pFirst(NULL), + m_pLast(NULL), + m_Count(0), + m_Cache(iItems) +{ +} // constructor + +CBaseList::CBaseList(CHAR *pName) : // Descriptive list name +#ifdef DEBUG + CBaseObject(pName), +#endif + m_pFirst(NULL), + m_pLast(NULL), + m_Count(0), + m_Cache(DEFAULTCACHE) +{ +} // constructor + +#endif + +/* The destructor enumerates all the node objects in the list and + in the cache deleting each in turn. We do not do any processing + on the objects that the list holds (i.e. points to) so if they + represent interfaces for example the creator of the list should + ensure that each of them is released before deleting us +*/ +CBaseList::~CBaseList() +{ + /* Delete all our list nodes */ + + RemoveAll(); + +} // destructor + +/* Remove all the nodes from the list but don't do anything + with the objects that each node looks after (this is the + responsibility of the creator). + Aa a last act we reset the signalling event + (if available) to indicate to clients that the list + does not have any entries in it. +*/ +void CBaseList::RemoveAll() +{ + /* Free up all the CNode objects NOTE we don't bother putting the + deleted nodes into the cache as this method is only really called + in serious times of change such as when we are being deleted at + which point the cache will be deleted anway */ + + CNode *pn = m_pFirst; + while (pn) { + CNode *op = pn; + pn = pn->Next(); + delete op; + } + + /* Reset the object count and the list pointers */ + + m_Count = 0; + m_pFirst = m_pLast = NULL; + +} // RemoveAll + + + +/* Return a position enumerator for the entire list. + A position enumerator is a pointer to a node object cast to a + transparent type so all we do is return the head/tail node + pointer in the list. + WARNING because the position is a pointer to a node there is + an implicit assumption for users a the list class that after + deleting an object from the list that any other position + enumerators that you have may be invalid (since the node + may be gone). +*/ +WXLIST_POSITION CBaseList::GetHeadPositionI() const +{ + return (WXLIST_POSITION) m_pFirst; +} // GetHeadPosition + + + +WXLIST_POSITION CBaseList::GetTailPositionI() const +{ + return (WXLIST_POSITION) m_pLast; +} // GetTailPosition + + + +/* Get the number of objects in the list, + Get the lock before accessing the count. + Locking may not be entirely necessary but it has the side effect + of making sure that all operations are complete before we get it. + So for example if a list is being added to this list then that + will have completed in full before we continue rather than seeing + an intermediate albeit valid state +*/ +int CBaseList::GetCountI() const +{ + return m_Count; +} // GetCount + + + +/* Return the object at rp, update rp to the next object from + the list or NULL if you have moved over the last object. + You may still call this function once we return NULL but + we will continue to return a NULL position value +*/ +void *CBaseList::GetNextI(WXLIST_POSITION& rp) const +{ + /* have we reached the end of the list */ + + if (rp == NULL) { + return NULL; + } + + /* Lock the object before continuing */ + + void *pObject; + + /* Copy the original position then step on */ + + CNode *pn = (CNode *) rp; + ASSERT(pn != NULL); + rp = (WXLIST_POSITION) pn->Next(); + + /* Get the object at the original position from the list */ + + pObject = pn->GetData(); + // ASSERT(pObject != NULL); // NULL pointers in the list are allowed. + return pObject; +} //GetNext + + + +/* Return the object at p. + Asking for the object at NULL ASSERTs then returns NULL + The object is NOT locked. The list is not being changed + in any way. If another thread is busy deleting the object + then locking would only result in a change from one bad + behaviour to another. +*/ +void *CBaseList::GetI(WXLIST_POSITION p) const +{ + if (p == NULL) { + return NULL; + } + + CNode * pn = (CNode *) p; + void *pObject = pn->GetData(); + // ASSERT(pObject != NULL); // NULL pointers in the list are allowed. + return pObject; +} //Get + + + +/* Return the first position in the list which holds the given pointer. + Return NULL if it's not found. +*/ +WXLIST_POSITION CBaseList::FindI( void * pObj) const +{ + WXLIST_POSITION pn; + INTERNALTRAVERSELIST(*this, pn){ + if (GetI(pn)==pObj) { + return pn; + } + } + return NULL; +} // Find + + + +/* Remove the first node in the list (deletes the pointer to its object + from the list, does not free the object itself). + Return the pointer to its object or NULL if empty +*/ +void *CBaseList::RemoveHeadI() +{ + /* All we do is get the head position and ask for that to be deleted. + We could special case this since some of the code path checking + in Remove() is redundant as we know there is no previous + node for example but it seems to gain little over the + added complexity + */ + + return RemoveI((WXLIST_POSITION)m_pFirst); +} // RemoveHead + + + +/* Remove the last node in the list (deletes the pointer to its object + from the list, does not free the object itself). + Return the pointer to its object or NULL if empty +*/ +void *CBaseList::RemoveTailI() +{ + /* All we do is get the tail position and ask for that to be deleted. + We could special case this since some of the code path checking + in Remove() is redundant as we know there is no previous + node for example but it seems to gain little over the + added complexity + */ + + return RemoveI((WXLIST_POSITION)m_pLast); +} // RemoveTail + + + +/* Remove the pointer to the object in this position from the list. + Deal with all the chain pointers + Return a pointer to the object removed from the list. + The node object that is freed as a result + of this operation is added to the node cache where + it can be used again. + Remove(NULL) is a harmless no-op - but probably is a wart. +*/ +void *CBaseList::RemoveI(WXLIST_POSITION pos) +{ + /* Lock the critical section before continuing */ + + // ASSERT (pos!=NULL); // Removing NULL is to be harmless! + if (pos==NULL) return NULL; + + + CNode *pCurrent = (CNode *) pos; + ASSERT(pCurrent != NULL); + + /* Update the previous node */ + + CNode *pNode = pCurrent->Prev(); + if (pNode == NULL) { + m_pFirst = pCurrent->Next(); + } else { + pNode->SetNext(pCurrent->Next()); + } + + /* Update the following node */ + + pNode = pCurrent->Next(); + if (pNode == NULL) { + m_pLast = pCurrent->Prev(); + } else { + pNode->SetPrev(pCurrent->Prev()); + } + + /* Get the object this node was looking after */ + + void *pObject = pCurrent->GetData(); + + // ASSERT(pObject != NULL); // NULL pointers in the list are allowed. + + /* Try and add the node object to the cache - + a NULL return code from the cache means we ran out of room. + The cache size is fixed by a constructor argument when the + list is created and defaults to DEFAULTCACHE. + This means that the cache will have room for this many + node objects. So if you have a list of media samples + and you know there will never be more than five active at + any given time of them for example then override the default + constructor + */ + + m_Cache.AddToCache(pCurrent); + + /* If the list is empty then reset the list event */ + + --m_Count; + ASSERT(m_Count >= 0); + return pObject; +} // Remove + + + +/* Add this object to the tail end of our list + Return the new tail position. +*/ + +WXLIST_POSITION CBaseList::AddTailI(void *pObject) +{ + /* Lock the critical section before continuing */ + + CNode *pNode; + // ASSERT(pObject); // NULL pointers in the list are allowed. + + /* If there is a node objects in the cache then use + that otherwise we will have to create a new one */ + + pNode = (CNode *) m_Cache.RemoveFromCache(); + if (pNode == NULL) { + pNode = new CNode; + } + + /* Check we have a valid object */ + + if (pNode == NULL) { + return NULL; + } + + /* Initialise all the CNode object + just in case it came from the cache + */ + + pNode->SetData(pObject); + pNode->SetNext(NULL); + pNode->SetPrev(m_pLast); + + if (m_pLast == NULL) { + m_pFirst = pNode; + } else { + m_pLast->SetNext(pNode); + } + + /* Set the new last node pointer and also increment the number + of list entries, the critical section is unlocked when we + exit the function + */ + + m_pLast = pNode; + ++m_Count; + + return (WXLIST_POSITION) pNode; +} // AddTail(object) + + + +/* Add this object to the head end of our list + Return the new head position. +*/ +WXLIST_POSITION CBaseList::AddHeadI(void *pObject) +{ + CNode *pNode; + // ASSERT(pObject); // NULL pointers in the list are allowed. + + /* If there is a node objects in the cache then use + that otherwise we will have to create a new one */ + + pNode = (CNode *) m_Cache.RemoveFromCache(); + if (pNode == NULL) { + pNode = new CNode; + } + + /* Check we have a valid object */ + + if (pNode == NULL) { + return NULL; + } + + /* Initialise all the CNode object + just in case it came from the cache + */ + + pNode->SetData(pObject); + + /* chain it in (set four pointers) */ + pNode->SetPrev(NULL); + pNode->SetNext(m_pFirst); + + if (m_pFirst == NULL) { + m_pLast = pNode; + } else { + m_pFirst->SetPrev(pNode); + } + m_pFirst = pNode; + + ++m_Count; + + return (WXLIST_POSITION) pNode; +} // AddHead(object) + + + +/* Add all the elements in *pList to the tail of this list. + Return TRUE if it all worked, FALSE if it didn't. + If it fails some elements may have been added. +*/ +BOOL CBaseList::AddTail(CBaseList *pList) +{ + /* lock the object before starting then enumerate + each entry in the source list and add them one by one to + our list (while still holding the object lock) + Lock the other list too. + */ + WXLIST_POSITION pos = pList->GetHeadPositionI(); + + while (pos) { + if (NULL == AddTailI(pList->GetNextI(pos))) { + return FALSE; + } + } + return TRUE; +} // AddTail(list) + + + +/* Add all the elements in *pList to the head of this list. + Return TRUE if it all worked, FALSE if it didn't. + If it fails some elements may have been added. +*/ +BOOL CBaseList::AddHead(CBaseList *pList) +{ + /* lock the object before starting then enumerate + each entry in the source list and add them one by one to + our list (while still holding the object lock) + Lock the other list too. + + To avoid reversing the list, traverse it backwards. + */ + + WXLIST_POSITION pos; + + INTERNALREVERSETRAVERSELIST(*pList, pos) { + if (NULL== AddHeadI(pList->GetI(pos))){ + return FALSE; + } + } + return TRUE; +} // AddHead(list) + + + +/* Add the object after position p + p is still valid after the operation. + AddAfter(NULL,x) adds x to the start - same as AddHead + Return the position of the new object, NULL if it failed +*/ +WXLIST_POSITION CBaseList::AddAfterI(WXLIST_POSITION pos, void * pObj) +{ + if (pos==NULL) + return AddHeadI(pObj); + + /* As someone else might be furkling with the list - + Lock the critical section before continuing + */ + CNode *pAfter = (CNode *) pos; + ASSERT(pAfter != NULL); + if (pAfter==m_pLast) + return AddTailI(pObj); + + /* set pnode to point to a new node, preferably from the cache */ + + CNode *pNode = (CNode *) m_Cache.RemoveFromCache(); + if (pNode == NULL) { + pNode = new CNode; + } + + /* Check we have a valid object */ + + if (pNode == NULL) { + return NULL; + } + + /* Initialise all the CNode object + just in case it came from the cache + */ + + pNode->SetData(pObj); + + /* It is to be added to the middle of the list - there is a before + and after node. Chain it after pAfter, before pBefore. + */ + CNode * pBefore = pAfter->Next(); + ASSERT(pBefore != NULL); + + /* chain it in (set four pointers) */ + pNode->SetPrev(pAfter); + pNode->SetNext(pBefore); + pBefore->SetPrev(pNode); + pAfter->SetNext(pNode); + + ++m_Count; + + return (WXLIST_POSITION) pNode; + +} // AddAfter(object) + + + +BOOL CBaseList::AddAfter(WXLIST_POSITION p, CBaseList *pList) +{ + WXLIST_POSITION pos; + INTERNALTRAVERSELIST(*pList, pos) { + /* p follows along the elements being added */ + p = AddAfterI(p, pList->GetI(pos)); + if (p==NULL) return FALSE; + } + return TRUE; +} // AddAfter(list) + + + +/* Mirror images: + Add the element or list after position p. + p is still valid after the operation. + AddBefore(NULL,x) adds x to the end - same as AddTail +*/ +WXLIST_POSITION CBaseList::AddBeforeI(WXLIST_POSITION pos, void * pObj) +{ + if (pos==NULL) + return AddTailI(pObj); + + /* set pnode to point to a new node, preferably from the cache */ + + CNode *pBefore = (CNode *) pos; + ASSERT(pBefore != NULL); + if (pBefore==m_pFirst) + return AddHeadI(pObj); + + CNode * pNode = (CNode *) m_Cache.RemoveFromCache(); + if (pNode == NULL) { + pNode = new CNode; + } + + /* Check we have a valid object */ + + if (pNode == NULL) { + return NULL; + } + + /* Initialise all the CNode object + just in case it came from the cache + */ + + pNode->SetData(pObj); + + /* It is to be added to the middle of the list - there is a before + and after node. Chain it after pAfter, before pBefore. + */ + + CNode * pAfter = pBefore->Prev(); + ASSERT(pAfter != NULL); + + /* chain it in (set four pointers) */ + pNode->SetPrev(pAfter); + pNode->SetNext(pBefore); + pBefore->SetPrev(pNode); + pAfter->SetNext(pNode); + + ++m_Count; + + return (WXLIST_POSITION) pNode; + +} // Addbefore(object) + + + +BOOL CBaseList::AddBefore(WXLIST_POSITION p, CBaseList *pList) +{ + WXLIST_POSITION pos; + INTERNALREVERSETRAVERSELIST(*pList, pos) { + /* p follows along the elements being added */ + p = AddBeforeI(p, pList->GetI(pos)); + if (p==NULL) return FALSE; + } + return TRUE; +} // AddBefore(list) + + + +/* Split *this after position p in *this + Retain as *this the tail portion of the original *this + Add the head portion to the tail end of *pList + Return TRUE if it all worked, FALSE if it didn't. + + e.g. + foo->MoveToTail(foo->GetHeadPosition(), bar); + moves one element from the head of foo to the tail of bar + foo->MoveToTail(NULL, bar); + is a no-op + foo->MoveToTail(foo->GetTailPosition, bar); + concatenates foo onto the end of bar and empties foo. + + A better, except excessively long name might be + MoveElementsFromHeadThroughPositionToOtherTail +*/ +BOOL CBaseList::MoveToTail + (WXLIST_POSITION pos, CBaseList *pList) +{ + /* Algorithm: + Note that the elements (including their order) in the concatenation + of *pList to the head of *this is invariant. + 1. Count elements to be moved + 2. Join *pList onto the head of this to make one long chain + 3. Set first/Last pointers in *this and *pList + 4. Break the chain at the new place + 5. Adjust counts + 6. Set/Reset any events + */ + + if (pos==NULL) return TRUE; // no-op. Eliminates special cases later. + + + /* Make cMove the number of nodes to move */ + CNode * p = (CNode *)pos; + int cMove = 0; // number of nodes to move + while(p!=NULL) { + p = p->Prev(); + ++cMove; + } + + + /* Join the two chains together */ + if (pList->m_pLast!=NULL) + pList->m_pLast->SetNext(m_pFirst); + if (m_pFirst!=NULL) + m_pFirst->SetPrev(pList->m_pLast); + + + /* set first and last pointers */ + p = (CNode *)pos; + + if (pList->m_pFirst==NULL) + pList->m_pFirst = m_pFirst; + m_pFirst = p->Next(); + if (m_pFirst==NULL) + m_pLast = NULL; + pList->m_pLast = p; + + + /* Break the chain after p to create the new pieces */ + if (m_pFirst!=NULL) + m_pFirst->SetPrev(NULL); + p->SetNext(NULL); + + + /* Adjust the counts */ + m_Count -= cMove; + pList->m_Count += cMove; + + return TRUE; + +} // MoveToTail + + + +/* Mirror image of MoveToTail: + Split *this before position p in *this. + Retain in *this the head portion of the original *this + Add the tail portion to the start (i.e. head) of *pList + Return TRUE if it all worked, FALSE if it didn't. + + e.g. + foo->MoveToHead(foo->GetTailPosition(), bar); + moves one element from the tail of foo to the head of bar + foo->MoveToHead(NULL, bar); + is a no-op + foo->MoveToHead(foo->GetHeadPosition, bar); + concatenates foo onto the start of bar and empties foo. +*/ +BOOL CBaseList::MoveToHead + (WXLIST_POSITION pos, CBaseList *pList) +{ + + /* See the comments on the algorithm in MoveToTail */ + + if (pos==NULL) return TRUE; // no-op. Eliminates special cases later. + + /* Make cMove the number of nodes to move */ + CNode * p = (CNode *)pos; + int cMove = 0; // number of nodes to move + while(p!=NULL) { + p = p->Next(); + ++cMove; + } + + + /* Join the two chains together */ + if (pList->m_pFirst!=NULL) + pList->m_pFirst->SetPrev(m_pLast); + if (m_pLast!=NULL) + m_pLast->SetNext(pList->m_pFirst); + + + /* set first and last pointers */ + p = (CNode *)pos; + + + if (pList->m_pLast==NULL) + pList->m_pLast = m_pLast; + + m_pLast = p->Prev(); + if (m_pLast==NULL) + m_pFirst = NULL; + pList->m_pFirst = p; + + + /* Break the chain after p to create the new pieces */ + if (m_pLast!=NULL) + m_pLast->SetNext(NULL); + p->SetPrev(NULL); + + + /* Adjust the counts */ + m_Count -= cMove; + pList->m_Count += cMove; + + return TRUE; + +} // MoveToHead + + + +/* Reverse the order of the [pointers to] objects in *this +*/ +void CBaseList::Reverse() +{ + /* algorithm: + The obvious booby trap is that you flip pointers around and lose + addressability to the node that you are going to process next. + The easy way to avoid this is do do one chain at a time. + + Run along the forward chain, + For each node, set the reverse pointer to the one ahead of us. + The reverse chain is now a copy of the old forward chain, including + the NULL termination. + + Run along the reverse chain (i.e. old forward chain again) + For each node set the forward pointer of the node ahead to point back + to the one we're standing on. + The first node needs special treatment, + it's new forward pointer is NULL. + Finally set the First/Last pointers + + */ + CNode * p; + + // Yes we COULD use a traverse, but it would look funny! + p = m_pFirst; + while (p!=NULL) { + CNode * q; + q = p->Next(); + p->SetNext(p->Prev()); + p->SetPrev(q); + p = q; + } + + p = m_pFirst; + m_pFirst = m_pLast; + m_pLast = p; + + +#if 0 // old version + + if (m_pFirst==NULL) return; // empty list + if (m_pFirst->Next()==NULL) return; // single node list + + + /* run along forward chain */ + for ( p = m_pFirst + ; p!=NULL + ; p = p->Next() + ){ + p->SetPrev(p->Next()); + } + + + /* special case first element */ + m_pFirst->SetNext(NULL); // fix the old first element + + + /* run along new reverse chain i.e. old forward chain again */ + for ( p = m_pFirst // start at the old first element + ; p->Prev()!=NULL // while there's a node still to be set + ; p = p->Prev() // work in the same direction as before + ){ + p->Prev()->SetNext(p); + } + + + /* fix forward and reverse pointers + - the triple XOR swap would work but all the casts look hideous */ + p = m_pFirst; + m_pFirst = m_pLast; + m_pLast = p; +#endif + +} // Reverse diff --git a/plugins/GSdx/baseclasses/wxlist.h b/plugins/GSdx/baseclasses/wxlist.h new file mode 100644 index 0000000000..3c268c3258 --- /dev/null +++ b/plugins/GSdx/baseclasses/wxlist.h @@ -0,0 +1,543 @@ +//------------------------------------------------------------------------------ +// File: WXList.h +// +// Desc: DirectShow base classes - defines a non-MFC generic template list +// class. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +/* A generic list of pointers to objects. + No storage management or copying is done on the objects pointed to. + Objectives: avoid using MFC libraries in ndm kernel mode and + provide a really useful list type. + + The class is thread safe in that separate threads may add and + delete items in the list concurrently although the application + must ensure that constructor and destructor access is suitably + synchronised. An application can cause deadlock with operations + which use two lists by simultaneously calling + list1->Operation(list2) and list2->Operation(list1). So don't! + + The names must not conflict with MFC classes as an application + may use both. + */ + +#ifndef __WXLIST__ +#define __WXLIST__ + + /* A POSITION represents (in some fashion that's opaque) a cursor + on the list that can be set to identify any element. NULL is + a valid value and several operations regard NULL as the position + "one step off the end of the list". (In an n element list there + are n+1 places to insert and NULL is that "n+1-th" value). + The POSITION of an element in the list is only invalidated if + that element is deleted. Move operations may mean that what + was a valid POSITION in one list is now a valid POSITION in + a different list. + + Some operations which at first sight are illegal are allowed as + harmless no-ops. For instance RemoveHead is legal on an empty + list and it returns NULL. This allows an atomic way to test if + there is an element there, and if so, get it. The two operations + AddTail and RemoveHead thus implement a MONITOR (See Hoare's paper). + + Single element operations return POSITIONs, non-NULL means it worked. + whole list operations return a BOOL. TRUE means it all worked. + + This definition is the same as the POSITION type for MFCs, so we must + avoid defining it twice. + */ +struct __WXLIST_POSITION { int unused; }; +typedef __WXLIST_POSITION* WXLIST_POSITION; + +const int DEFAULTCACHE = 10; /* Default node object cache size */ + +/* A class representing one node in a list. + Each node knows a pointer to it's adjacent nodes and also a pointer + to the object that it looks after. + All of these pointers can be retrieved or set through member functions. +*/ +class CBaseList +#ifdef DEBUG + : public CBaseObject +#endif +{ + /* Making these classes inherit from CBaseObject does nothing + functionally but it allows us to check there are no memory + leaks in debug builds. + */ + +public: + +#ifdef DEBUG + class CNode : public CBaseObject { +#else + class CNode { +#endif + + CNode *m_pPrev; /* Previous node in the list */ + CNode *m_pNext; /* Next node in the list */ + void *m_pObject; /* Pointer to the object */ + + public: + + /* Constructor - initialise the object's pointers */ + CNode() +#ifdef DEBUG + : CBaseObject(NAME("List node")) +#endif + { + }; + + + /* Return the previous node before this one */ + CNode *Prev() const { return m_pPrev; }; + + + /* Return the next node after this one */ + CNode *Next() const { return m_pNext; }; + + + /* Set the previous node before this one */ + void SetPrev(CNode *p) { m_pPrev = p; }; + + + /* Set the next node after this one */ + void SetNext(CNode *p) { m_pNext = p; }; + + + /* Get the pointer to the object for this node */ + void *GetData() const { return m_pObject; }; + + + /* Set the pointer to the object for this node */ + void SetData(void *p) { m_pObject = p; }; + }; + + class CNodeCache + { + public: + CNodeCache(INT iCacheSize) : m_iCacheSize(iCacheSize), + m_pHead(NULL), + m_iUsed(0) + {}; + ~CNodeCache() { + CNode *pNode = m_pHead; + while (pNode) { + CNode *pCurrent = pNode; + pNode = pNode->Next(); + delete pCurrent; + } + }; + void AddToCache(CNode *pNode) + { + if (m_iUsed < m_iCacheSize) { + pNode->SetNext(m_pHead); + m_pHead = pNode; + m_iUsed++; + } else { + delete pNode; + } + }; + CNode *RemoveFromCache() + { + CNode *pNode = m_pHead; + if (pNode != NULL) { + m_pHead = pNode->Next(); + m_iUsed--; + ASSERT(m_iUsed >= 0); + } else { + ASSERT(m_iUsed == 0); + } + return pNode; + }; + private: + INT m_iCacheSize; + INT m_iUsed; + CNode *m_pHead; + }; + +protected: + + CNode* m_pFirst; /* Pointer to first node in the list */ + CNode* m_pLast; /* Pointer to the last node in the list */ + LONG m_Count; /* Number of nodes currently in the list */ + +private: + + CNodeCache m_Cache; /* Cache of unused node pointers */ + +private: + + /* These override the default copy constructor and assignment + operator for all list classes. They are in the private class + declaration section so that anybody trying to pass a list + object by value will generate a compile time error of + "cannot access the private member function". If these were + not here then the compiler will create default constructors + and assignment operators which when executed first take a + copy of all member variables and then during destruction + delete them all. This must not be done for any heap + allocated data. + */ + CBaseList(const CBaseList &refList); + CBaseList &operator=(const CBaseList &refList); + +public: + + CBaseList(TCHAR *pName, + INT iItems); + + CBaseList(TCHAR *pName); +#ifdef UNICODE + CBaseList(CHAR *pName, + INT iItems); + + CBaseList(CHAR *pName); +#endif + ~CBaseList(); + + /* Remove all the nodes from *this i.e. make the list empty */ + void RemoveAll(); + + + /* Return a cursor which identifies the first element of *this */ + WXLIST_POSITION GetHeadPositionI() const; + + + /* Return a cursor which identifies the last element of *this */ + WXLIST_POSITION GetTailPositionI() const; + + + /* Return the number of objects in *this */ + int GetCountI() const; + +protected: + /* Return the pointer to the object at rp, + Update rp to the next node in *this + but make it NULL if it was at the end of *this. + This is a wart retained for backwards compatibility. + GetPrev is not implemented. + Use Next, Prev and Get separately. + */ + void *GetNextI(WXLIST_POSITION& rp) const; + + + /* Return a pointer to the object at p + Asking for the object at NULL will return NULL harmlessly. + */ + void *GetI(WXLIST_POSITION p) const; + +public: + /* return the next / prev position in *this + return NULL when going past the end/start. + Next(NULL) is same as GetHeadPosition() + Prev(NULL) is same as GetTailPosition() + An n element list therefore behaves like a n+1 element + cycle with NULL at the start/end. + + !!WARNING!! - This handling of NULL is DIFFERENT from GetNext. + + Some reasons are: + 1. For a list of n items there are n+1 positions to insert + These are conveniently encoded as the n POSITIONs and NULL. + 2. If you are keeping a list sorted (fairly common) and you + search forward for an element to insert before and don't + find it you finish up with NULL as the element before which + to insert. You then want that NULL to be a valid WXLIST_POSITION + so that you can insert before it and you want that insertion + point to mean the (n+1)-th one that doesn't have a WXLIST_POSITION. + (symmetrically if you are working backwards through the list). + 3. It simplifies the algebra which the methods generate. + e.g. AddBefore(p,x) is identical to AddAfter(Prev(p),x) + in ALL cases. All the other arguments probably are reflections + of the algebraic point. + */ + WXLIST_POSITION Next(WXLIST_POSITION pos) const + { + if (pos == NULL) { + return (WXLIST_POSITION) m_pFirst; + } + CNode *pn = (CNode *) pos; + return (WXLIST_POSITION) pn->Next(); + } //Next + + // See Next + WXLIST_POSITION Prev(WXLIST_POSITION pos) const + { + if (pos == NULL) { + return (WXLIST_POSITION) m_pLast; + } + CNode *pn = (CNode *) pos; + return (WXLIST_POSITION) pn->Prev(); + } //Prev + + + /* Return the first position in *this which holds the given + pointer. Return NULL if the pointer was not not found. + */ +protected: + WXLIST_POSITION FindI( void * pObj) const; + + /* Remove the first node in *this (deletes the pointer to its + object from the list, does not free the object itself). + Return the pointer to its object. + If *this was already empty it will harmlessly return NULL. + */ + void *RemoveHeadI(); + + + /* Remove the last node in *this (deletes the pointer to its + object from the list, does not free the object itself). + Return the pointer to its object. + If *this was already empty it will harmlessly return NULL. + */ + void *RemoveTailI(); + + + /* Remove the node identified by p from the list (deletes the pointer + to its object from the list, does not free the object itself). + Asking to Remove the object at NULL will harmlessly return NULL. + Return the pointer to the object removed. + */ + void *RemoveI(WXLIST_POSITION p); + + /* Add single object *pObj to become a new last element of the list. + Return the new tail position, NULL if it fails. + If you are adding a COM objects, you might want AddRef it first. + Other existing POSITIONs in *this are still valid + */ + WXLIST_POSITION AddTailI(void * pObj); +public: + + + /* Add all the elements in *pList to the tail of *this. + This duplicates all the nodes in *pList (i.e. duplicates + all its pointers to objects). It does not duplicate the objects. + If you are adding a list of pointers to a COM object into the list + it's a good idea to AddRef them all it when you AddTail it. + Return TRUE if it all worked, FALSE if it didn't. + If it fails some elements may have been added. + Existing POSITIONs in *this are still valid + + If you actually want to MOVE the elements, use MoveToTail instead. + */ + BOOL AddTail(CBaseList *pList); + + + /* Mirror images of AddHead: */ + + /* Add single object to become a new first element of the list. + Return the new head position, NULL if it fails. + Existing POSITIONs in *this are still valid + */ +protected: + WXLIST_POSITION AddHeadI(void * pObj); +public: + + /* Add all the elements in *pList to the head of *this. + Same warnings apply as for AddTail. + Return TRUE if it all worked, FALSE if it didn't. + If it fails some of the objects may have been added. + + If you actually want to MOVE the elements, use MoveToHead instead. + */ + BOOL AddHead(CBaseList *pList); + + + /* Add the object *pObj to *this after position p in *this. + AddAfter(NULL,x) adds x to the start - equivalent to AddHead + Return the position of the object added, NULL if it failed. + Existing POSITIONs in *this are undisturbed, including p. + */ +protected: + WXLIST_POSITION AddAfterI(WXLIST_POSITION p, void * pObj); +public: + + /* Add the list *pList to *this after position p in *this + AddAfter(NULL,x) adds x to the start - equivalent to AddHead + Return TRUE if it all worked, FALSE if it didn't. + If it fails, some of the objects may be added + Existing POSITIONs in *this are undisturbed, including p. + */ + BOOL AddAfter(WXLIST_POSITION p, CBaseList *pList); + + + /* Mirror images: + Add the object *pObj to this-List after position p in *this. + AddBefore(NULL,x) adds x to the end - equivalent to AddTail + Return the position of the new object, NULL if it fails + Existing POSITIONs in *this are undisturbed, including p. + */ + protected: + WXLIST_POSITION AddBeforeI(WXLIST_POSITION p, void * pObj); + public: + + /* Add the list *pList to *this before position p in *this + AddAfter(NULL,x) adds x to the start - equivalent to AddHead + Return TRUE if it all worked, FALSE if it didn't. + If it fails, some of the objects may be added + Existing POSITIONs in *this are undisturbed, including p. + */ + BOOL AddBefore(WXLIST_POSITION p, CBaseList *pList); + + + /* Note that AddAfter(p,x) is equivalent to AddBefore(Next(p),x) + even in cases where p is NULL or Next(p) is NULL. + Similarly for mirror images etc. + This may make it easier to argue about programs. + */ + + + + /* The following operations do not copy any elements. + They move existing blocks of elements around by switching pointers. + They are fairly efficient for long lists as for short lists. + (Alas, the Count slows things down). + + They split the list into two parts. + One part remains as the original list, the other part + is appended to the second list. There are eight possible + variations: + Split the list {after/before} a given element + keep the {head/tail} portion in the original list + append the rest to the {head/tail} of the new list. + + Since After is strictly equivalent to Before Next + we are not in serious need of the Before/After variants. + That leaves only four. + + If you are processing a list left to right and dumping + the bits that you have processed into another list as + you go, the Tail/Tail variant gives the most natural result. + If you are processing in reverse order, Head/Head is best. + + By using NULL positions and empty lists judiciously either + of the other two can be built up in two operations. + + The definition of NULL (see Next/Prev etc) means that + degenerate cases include + "move all elements to new list" + "Split a list into two lists" + "Concatenate two lists" + (and quite a few no-ops) + + !!WARNING!! The type checking won't buy you much if you get list + positions muddled up - e.g. use a WXLIST_POSITION that's in a different + list and see what a mess you get! + */ + + /* Split *this after position p in *this + Retain as *this the tail portion of the original *this + Add the head portion to the tail end of *pList + Return TRUE if it all worked, FALSE if it didn't. + + e.g. + foo->MoveToTail(foo->GetHeadPosition(), bar); + moves one element from the head of foo to the tail of bar + foo->MoveToTail(NULL, bar); + is a no-op, returns NULL + foo->MoveToTail(foo->GetTailPosition, bar); + concatenates foo onto the end of bar and empties foo. + + A better, except excessively long name might be + MoveElementsFromHeadThroughPositionToOtherTail + */ + BOOL MoveToTail(WXLIST_POSITION pos, CBaseList *pList); + + + /* Mirror image: + Split *this before position p in *this. + Retain in *this the head portion of the original *this + Add the tail portion to the start (i.e. head) of *pList + + e.g. + foo->MoveToHead(foo->GetTailPosition(), bar); + moves one element from the tail of foo to the head of bar + foo->MoveToHead(NULL, bar); + is a no-op, returns NULL + foo->MoveToHead(foo->GetHeadPosition, bar); + concatenates foo onto the start of bar and empties foo. + */ + BOOL MoveToHead(WXLIST_POSITION pos, CBaseList *pList); + + + /* Reverse the order of the [pointers to] objects in *this + */ + void Reverse(); + + + /* set cursor to the position of each element of list in turn */ + #define TRAVERSELIST(list, cursor) \ + for ( cursor = (list).GetHeadPosition() \ + ; cursor!=NULL \ + ; cursor = (list).Next(cursor) \ + ) + + + /* set cursor to the position of each element of list in turn + in reverse order + */ + #define REVERSETRAVERSELIST(list, cursor) \ + for ( cursor = (list).GetTailPosition() \ + ; cursor!=NULL \ + ; cursor = (list).Prev(cursor) \ + ) + +}; // end of class declaration + +template class CGenericList : public CBaseList +{ +public: + CGenericList(TCHAR *pName, + INT iItems, + BOOL bLock = TRUE, + BOOL bAlert = FALSE) : + CBaseList(pName, iItems) { + UNREFERENCED_PARAMETER(bAlert); + UNREFERENCED_PARAMETER(bLock); + }; + CGenericList(TCHAR *pName) : + CBaseList(pName) { + }; + + WXLIST_POSITION GetHeadPosition() const { return (WXLIST_POSITION)m_pFirst; } + WXLIST_POSITION GetTailPosition() const { return (WXLIST_POSITION)m_pLast; } + int GetCount() const { return m_Count; } + + OBJECT *GetNext(WXLIST_POSITION& rp) const { return (OBJECT *) GetNextI(rp); } + + OBJECT *Get(WXLIST_POSITION p) const { return (OBJECT *) GetI(p); } + OBJECT *GetHead() const { return Get(GetHeadPosition()); } + + OBJECT *RemoveHead() { return (OBJECT *) RemoveHeadI(); } + + OBJECT *RemoveTail() { return (OBJECT *) RemoveTailI(); } + + OBJECT *Remove(WXLIST_POSITION p) { return (OBJECT *) RemoveI(p); } + WXLIST_POSITION AddBefore(WXLIST_POSITION p, OBJECT * pObj) { return AddBeforeI(p, pObj); } + WXLIST_POSITION AddAfter(WXLIST_POSITION p, OBJECT * pObj) { return AddAfterI(p, pObj); } + WXLIST_POSITION AddHead(OBJECT * pObj) { return AddHeadI(pObj); } + WXLIST_POSITION AddTail(OBJECT * pObj) { return AddTailI(pObj); } + BOOL AddTail(CGenericList *pList) + { return CBaseList::AddTail((CBaseList *) pList); } + BOOL AddHead(CGenericList *pList) + { return CBaseList::AddHead((CBaseList *) pList); } + BOOL AddAfter(WXLIST_POSITION p, CGenericList *pList) + { return CBaseList::AddAfter(p, (CBaseList *) pList); }; + BOOL AddBefore(WXLIST_POSITION p, CGenericList *pList) + { return CBaseList::AddBefore(p, (CBaseList *) pList); }; + WXLIST_POSITION Find( OBJECT * pObj) const { return FindI(pObj); } +}; // end of class declaration + + + +/* These define the standard list types */ + +typedef CGenericList CBaseObjectList; +typedef CGenericList CBaseInterfaceList; + +#endif /* __WXLIST__ */ + diff --git a/plugins/GSdx/baseclasses/wxutil.cpp b/plugins/GSdx/baseclasses/wxutil.cpp new file mode 100644 index 0000000000..8947f6dcec --- /dev/null +++ b/plugins/GSdx/baseclasses/wxutil.cpp @@ -0,0 +1,1243 @@ +//------------------------------------------------------------------------------ +// File: WXUtil.cpp +// +// Desc: DirectShow base classes - implements helper classes for building +// multimedia filters. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#include "streams.h" + +// +// Declare function from largeint.h we need so that PPC can build +// + +// +// Enlarged integer divide - 64-bits / 32-bits > 32-bits +// + +#ifndef _X86_ + +#define LLtoU64(x) (*(unsigned __int64*)(void*)(&(x))) + +__inline +ULONG +WINAPI +EnlargedUnsignedDivide ( + IN ULARGE_INTEGER Dividend, + IN ULONG Divisor, + IN PULONG Remainder + ) +{ + // return remainder if necessary + if (Remainder != NULL) + *Remainder = (ULONG)(LLtoU64(Dividend) % Divisor); + return (ULONG)(LLtoU64(Dividend) / Divisor); +} + +#else +__inline +ULONG +WINAPI +EnlargedUnsignedDivide ( + IN ULARGE_INTEGER Dividend, + IN ULONG Divisor, + IN PULONG Remainder + ) +{ + ULONG ulResult; + _asm { + mov eax,Dividend.LowPart + mov edx,Dividend.HighPart + mov ecx,Remainder + div Divisor + or ecx,ecx + jz short label + mov [ecx],edx +label: + mov ulResult,eax + } + return ulResult; +} +#endif + +// --- CAMEvent ----------------------- +CAMEvent::CAMEvent(BOOL fManualReset) +{ + m_hEvent = CreateEvent(NULL, fManualReset, FALSE, NULL); +} + +CAMEvent::~CAMEvent() +{ + if (m_hEvent) { + EXECUTE_ASSERT(CloseHandle(m_hEvent)); + } +} + + +// --- CAMMsgEvent ----------------------- +// One routine. The rest is handled in CAMEvent + +BOOL CAMMsgEvent::WaitMsg(DWORD dwTimeout) +{ + // wait for the event to be signalled, or for the + // timeout (in MS) to expire. allow SENT messages + // to be processed while we wait + DWORD dwWait; + DWORD dwStartTime; + + // set the waiting period. + DWORD dwWaitTime = dwTimeout; + + // the timeout will eventually run down as we iterate + // processing messages. grab the start time so that + // we can calculate elapsed times. + if (dwWaitTime != INFINITE) { + dwStartTime = timeGetTime(); + } + + do { + dwWait = MsgWaitForMultipleObjects(1,&m_hEvent,FALSE, dwWaitTime, QS_SENDMESSAGE); + if (dwWait == WAIT_OBJECT_0 + 1) { + MSG Message; + PeekMessage(&Message,NULL,0,0,PM_NOREMOVE); + + // If we have an explicit length of time to wait calculate + // the next wake up point - which might be now. + // If dwTimeout is INFINITE, it stays INFINITE + if (dwWaitTime != INFINITE) { + + DWORD dwElapsed = timeGetTime()-dwStartTime; + + dwWaitTime = + (dwElapsed >= dwTimeout) + ? 0 // wake up with WAIT_TIMEOUT + : dwTimeout-dwElapsed; + } + } + } while (dwWait == WAIT_OBJECT_0 + 1); + + // return TRUE if we woke on the event handle, + // FALSE if we timed out. + return (dwWait == WAIT_OBJECT_0); +} + +// --- CAMThread ---------------------- + + +CAMThread::CAMThread() + : m_EventSend(TRUE) // must be manual-reset for CheckRequest() +{ + m_hThread = NULL; +} + +CAMThread::~CAMThread() { + Close(); +} + + +// when the thread starts, it calls this function. We unwrap the 'this' +//pointer and call ThreadProc. +DWORD WINAPI +CAMThread::InitialThreadProc(LPVOID pv) +{ + HRESULT hrCoInit = CAMThread::CoInitializeHelper(); + if(FAILED(hrCoInit)) { + DbgLog((LOG_ERROR, 1, TEXT("CoInitializeEx failed."))); + } + + CAMThread * pThread = (CAMThread *) pv; + + HRESULT hr = pThread->ThreadProc(); + + if(SUCCEEDED(hrCoInit)) { + CoUninitialize(); + } + + return hr; +} + +BOOL +CAMThread::Create() +{ + DWORD threadid; + + CAutoLock lock(&m_AccessLock); + + if (ThreadExists()) { + return FALSE; + } + + m_hThread = CreateThread( + NULL, + 0, + CAMThread::InitialThreadProc, + this, + 0, + &threadid); + + if (!m_hThread) { + return FALSE; + } + + return TRUE; +} + +DWORD +CAMThread::CallWorker(DWORD dwParam) +{ + // lock access to the worker thread for scope of this object + CAutoLock lock(&m_AccessLock); + + if (!ThreadExists()) { + return (DWORD) E_FAIL; + } + + // set the parameter + m_dwParam = dwParam; + + // signal the worker thread + m_EventSend.Set(); + + // wait for the completion to be signalled + m_EventComplete.Wait(); + + // done - this is the thread's return value + return m_dwReturnVal; +} + +// Wait for a request from the client +DWORD +CAMThread::GetRequest() +{ + m_EventSend.Wait(); + return m_dwParam; +} + +// is there a request? +BOOL +CAMThread::CheckRequest(DWORD * pParam) +{ + if (!m_EventSend.Check()) { + return FALSE; + } else { + if (pParam) { + *pParam = m_dwParam; + } + return TRUE; + } +} + +// reply to the request +void +CAMThread::Reply(DWORD dw) +{ + m_dwReturnVal = dw; + + // The request is now complete so CheckRequest should fail from + // now on + // + // This event should be reset BEFORE we signal the client or + // the client may Set it before we reset it and we'll then + // reset it (!) + + m_EventSend.Reset(); + + // Tell the client we're finished + + m_EventComplete.Set(); +} + +HRESULT CAMThread::CoInitializeHelper() +{ + // call CoInitializeEx and tell OLE not to create a window (this + // thread probably won't dispatch messages and will hang on + // broadcast msgs o/w). + // + // If CoInitEx is not available, threads that don't call CoCreate + // aren't affected. Threads that do will have to handle the + // failure. Perhaps we should fall back to CoInitialize and risk + // hanging? + // + + // older versions of ole32.dll don't have CoInitializeEx + + HRESULT hr = E_FAIL; + HINSTANCE hOle = GetModuleHandle(TEXT("ole32.dll")); + if(hOle) + { + typedef HRESULT (STDAPICALLTYPE *PCoInitializeEx)( + LPVOID pvReserved, DWORD dwCoInit); + PCoInitializeEx pCoInitializeEx = + (PCoInitializeEx)(GetProcAddress(hOle, "CoInitializeEx")); + if(pCoInitializeEx) + { + hr = (*pCoInitializeEx)(0, COINIT_DISABLE_OLE1DDE ); + } + } + else + { + // caller must load ole32.dll + DbgBreak("couldn't locate ole32.dll"); + } + + return hr; +} + + +// destructor for CMsgThread - cleans up any messages left in the +// queue when the thread exited +CMsgThread::~CMsgThread() +{ + if (m_hThread != NULL) { + WaitForSingleObject(m_hThread, INFINITE); + EXECUTE_ASSERT(CloseHandle(m_hThread)); + } + + WXLIST_POSITION pos = m_ThreadQueue.GetHeadPosition(); + while (pos) { + CMsg * pMsg = m_ThreadQueue.GetNext(pos); + delete pMsg; + } + m_ThreadQueue.RemoveAll(); + + if (m_hSem != NULL) { + EXECUTE_ASSERT(CloseHandle(m_hSem)); + } +} + +BOOL +CMsgThread::CreateThread( + ) +{ + m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL); + if (m_hSem == NULL) { + return FALSE; + } + + m_hThread = ::CreateThread(NULL, 0, DefaultThreadProc, + (LPVOID)this, 0, &m_ThreadId); + return m_hThread != NULL; +} + + +// This is the threads message pump. Here we get and dispatch messages to +// clients thread proc until the client refuses to process a message. +// The client returns a non-zero value to stop the message pump, this +// value becomes the threads exit code. + +DWORD WINAPI +CMsgThread::DefaultThreadProc( + LPVOID lpParam + ) +{ + CMsgThread *lpThis = (CMsgThread *)lpParam; + CMsg msg; + LRESULT lResult; + + // !!! + CoInitialize(NULL); + + // allow a derived class to handle thread startup + lpThis->OnThreadInit(); + + do { + lpThis->GetThreadMsg(&msg); + lResult = lpThis->ThreadMessageProc(msg.uMsg,msg.dwFlags, + msg.lpParam, msg.pEvent); + } while (lResult == 0L); + + // !!! + CoUninitialize(); + + return (DWORD)lResult; +} + + +// Block until the next message is placed on the list m_ThreadQueue. +// copies the message to the message pointed to by *pmsg +void +CMsgThread::GetThreadMsg(CMsg *msg) +{ + CMsg * pmsg = NULL; + + // keep trying until a message appears + while (TRUE) { + { + CAutoLock lck(&m_Lock); + pmsg = m_ThreadQueue.RemoveHead(); + if (pmsg == NULL) { + m_lWaiting++; + } else { + break; + } + } + // the semaphore will be signalled when it is non-empty + WaitForSingleObject(m_hSem, INFINITE); + } + // copy fields to caller's CMsg + *msg = *pmsg; + + // this CMsg was allocated by the 'new' in PutThreadMsg + delete pmsg; + +} + + +// NOTE: as we need to use the same binaries on Win95 as on NT this code should +// be compiled WITHOUT unicode being defined. Otherwise we will not pick up +// these internal routines and the binary will not run on Win95. + +#ifndef UNICODE +// Windows 95 doesn't implement this, so we provide an implementation. +// LPWSTR +// WINAPI +// lstrcpyWInternal( +// LPWSTR lpString1, +// LPCWSTR lpString2 +// ) +// { +// LPWSTR lpReturn = lpString1; +// while (*lpString1++ = *lpString2++); +// +// return lpReturn; +// } + +// Windows 95 doesn't implement this, so we provide an implementation. +LPWSTR +WINAPI +lstrcpynWInternal( + LPWSTR lpString1, + LPCWSTR lpString2, + int iMaxLength + ) +{ + ASSERT(iMaxLength); + LPWSTR lpReturn = lpString1; + if (iMaxLength) { + while (--iMaxLength && (*lpString1++ = *lpString2++)); + + // If we ran out of room (which will be the case if + // iMaxLength is now 0) we still need to terminate the + // string. + if (!iMaxLength) *lpString1 = L'\0'; + } + return lpReturn; +} + +int +WINAPI +lstrcmpWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ) +{ + do { + WCHAR c1 = *lpString1; + WCHAR c2 = *lpString2; + if (c1 != c2) + return (int) c1 - (int) c2; + } while (*lpString1++ && *lpString2++); + return 0; +} + + +int +WINAPI +lstrcmpiWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ) +{ + do { + WCHAR c1 = *lpString1; + WCHAR c2 = *lpString2; + if (c1 >= L'A' && c1 <= L'Z') + c1 -= (WCHAR) (L'A' - L'a'); + if (c2 >= L'A' && c2 <= L'Z') + c2 -= (WCHAR) (L'A' - L'a'); + + if (c1 != c2) + return (int) c1 - (int) c2; + } while (*lpString1++ && *lpString2++); + + return 0; +} + + +int +WINAPI +lstrlenWInternal( + LPCWSTR lpString + ) +{ + int i = -1; + while (*(lpString+(++i))) + ; + return i; +} + + +// int WINAPIV wsprintfWInternal(LPWSTR wszOut, LPCWSTR pszFmt, ...) +// { +// char fmt[256]; // !!! +// char ach[256]; // !!! +// int i; +// +// va_list va; +// va_start(va, pszFmt); +// WideCharToMultiByte(GetACP(), 0, pszFmt, -1, fmt, 256, NULL, NULL); +// (void)StringCchVPrintf(ach, NUMELMS(ach), fmt, va); +// i = lstrlenA(ach); +// va_end(va); +// +// MultiByteToWideChar(CP_ACP, 0, ach, -1, wszOut, i+1); +// +// return i; +// } +#else + +// need to provide the implementations in unicode for non-unicode +// builds linking with the unicode strmbase.lib +//LPWSTR WINAPI lstrcpyWInternal( +// LPWSTR lpString1, +// LPCWSTR lpString2 +// ) +//{ +// return lstrcpyW(lpString1, lpString2); +//} + +LPWSTR WINAPI lstrcpynWInternal( + LPWSTR lpString1, + LPCWSTR lpString2, + int iMaxLength + ) +{ + return lstrcpynW(lpString1, lpString2, iMaxLength); +} + +int WINAPI lstrcmpWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ) +{ + return lstrcmpW(lpString1, lpString2); +} + + +int WINAPI lstrcmpiWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ) +{ + return lstrcmpiW(lpString1, lpString2); +} + + +int WINAPI lstrlenWInternal( + LPCWSTR lpString + ) +{ + return lstrlenW(lpString); +} + + +//int WINAPIV wsprintfWInternal( +// LPWSTR wszOut, LPCWSTR pszFmt, ...) +//{ +// va_list va; +// va_start(va, pszFmt); +// int i = wvsprintfW(wszOut, pszFmt, va); +// va_end(va); +// return i; +//} +#endif + + +// Helper function - convert int to WSTR +void WINAPI IntToWstr(int i, LPWSTR wstr, size_t len) +{ +#ifdef UNICODE + (void)StringCchPrintf(wstr, len, L"%d", i); +#else + TCHAR temp[32]; + (void)StringCchPrintf(temp, NUMELMS(temp), "%d", i); + MultiByteToWideChar(CP_ACP, 0, temp, -1, wstr, int(len) ); +#endif +} // IntToWstr + + +#if 0 +void * memchrInternal(const void *pv, int c, size_t sz) +{ + BYTE *pb = (BYTE *) pv; + while (sz--) { + if (*pb == c) + return (void *) pb; + pb++; + } + return NULL; +} +#endif + + +#define MEMORY_ALIGNMENT 4 +#define MEMORY_ALIGNMENT_LOG2 2 +#define MEMORY_ALIGNMENT_MASK MEMORY_ALIGNMENT - 1 + +void * __stdcall memmoveInternal(void * dst, const void * src, size_t count) +{ + void * ret = dst; + +#ifdef _X86_ + if (dst <= src || (char *)dst >= ((char *)src + count)) { + + /* + * Non-Overlapping Buffers + * copy from lower addresses to higher addresses + */ + _asm { + mov esi,src + mov edi,dst + mov ecx,count + cld + mov edx,ecx + and edx,MEMORY_ALIGNMENT_MASK + shr ecx,MEMORY_ALIGNMENT_LOG2 + rep movsd + or ecx,edx + jz memmove_done + rep movsb +memmove_done: + } + } + else { + + /* + * Overlapping Buffers + * copy from higher addresses to lower addresses + */ + _asm { + mov esi,src + mov edi,dst + mov ecx,count + std + add esi,ecx + add edi,ecx + dec esi + dec edi + rep movsb + cld + } + } +#else + MoveMemory(dst, src, count); +#endif + + return ret; +} + +/* Arithmetic functions to help with time format conversions +*/ + +#ifdef _M_ALPHA +// work around bug in version 12.00.8385 of the alpha compiler where +// UInt32x32To64 sign-extends its arguments (?) +#undef UInt32x32To64 +#define UInt32x32To64(a, b) (((ULONGLONG)((ULONG)(a)) & 0xffffffff) * ((ULONGLONG)((ULONG)(b)) & 0xffffffff)) +#endif + +/* Compute (a * b + d) / c */ +LONGLONG WINAPI llMulDiv(LONGLONG a, LONGLONG b, LONGLONG c, LONGLONG d) +{ + /* Compute the absolute values to avoid signed arithmetic problems */ + ULARGE_INTEGER ua, ub; + DWORDLONG uc; + + ua.QuadPart = (DWORDLONG)(a >= 0 ? a : -a); + ub.QuadPart = (DWORDLONG)(b >= 0 ? b : -b); + uc = (DWORDLONG)(c >= 0 ? c : -c); + BOOL bSign = (a < 0) ^ (b < 0); + + /* Do long multiplication */ + ULARGE_INTEGER p[2]; + p[0].QuadPart = UInt32x32To64(ua.LowPart, ub.LowPart); + + /* This next computation cannot overflow into p[1].HighPart because + the max number we can compute here is: + + (2 ** 32 - 1) * (2 ** 32 - 1) + // ua.LowPart * ub.LowPart + (2 ** 32) * (2 ** 31) * (2 ** 32 - 1) * 2 // x.LowPart * y.HighPart * 2 + + == 2 ** 96 - 2 ** 64 + (2 ** 64 - 2 ** 33 + 1) + == 2 ** 96 - 2 ** 33 + 1 + < 2 ** 96 + */ + + ULARGE_INTEGER x; + x.QuadPart = UInt32x32To64(ua.LowPart, ub.HighPart) + + UInt32x32To64(ua.HighPart, ub.LowPart) + + p[0].HighPart; + p[0].HighPart = x.LowPart; + p[1].QuadPart = UInt32x32To64(ua.HighPart, ub.HighPart) + x.HighPart; + + if (d != 0) { + ULARGE_INTEGER ud[2]; + if (bSign) { + ud[0].QuadPart = (DWORDLONG)(-d); + if (d > 0) { + /* -d < 0 */ + ud[1].QuadPart = (DWORDLONG)(LONGLONG)-1; + } else { + ud[1].QuadPart = (DWORDLONG)0; + } + } else { + ud[0].QuadPart = (DWORDLONG)d; + if (d < 0) { + ud[1].QuadPart = (DWORDLONG)(LONGLONG)-1; + } else { + ud[1].QuadPart = (DWORDLONG)0; + } + } + /* Now do extended addition */ + ULARGE_INTEGER uliTotal; + + /* Add ls DWORDs */ + uliTotal.QuadPart = (DWORDLONG)ud[0].LowPart + p[0].LowPart; + p[0].LowPart = uliTotal.LowPart; + + /* Propagate carry */ + uliTotal.LowPart = uliTotal.HighPart; + uliTotal.HighPart = 0; + + /* Add 2nd most ls DWORDs */ + uliTotal.QuadPart += (DWORDLONG)ud[0].HighPart + p[0].HighPart; + p[0].HighPart = uliTotal.LowPart; + + /* Propagate carry */ + uliTotal.LowPart = uliTotal.HighPart; + uliTotal.HighPart = 0; + + /* Add MS DWORDLONGs - no carry expected */ + p[1].QuadPart += ud[1].QuadPart + uliTotal.QuadPart; + + /* Now see if we got a sign change from the addition */ + if ((LONG)p[1].HighPart < 0) { + bSign = !bSign; + + /* Negate the current value (ugh!) */ + p[0].QuadPart = ~p[0].QuadPart; + p[1].QuadPart = ~p[1].QuadPart; + p[0].QuadPart += 1; + p[1].QuadPart += (p[0].QuadPart == 0); + } + } + + /* Now for the division */ + if (c < 0) { + bSign = !bSign; + } + + + /* This will catch c == 0 and overflow */ + if (uc <= p[1].QuadPart) { + return bSign ? (LONGLONG)0x8000000000000000 : + (LONGLONG)0x7FFFFFFFFFFFFFFF; + } + + DWORDLONG ullResult; + + /* Do the division */ + /* If the dividend is a DWORD_LONG use the compiler */ + if (p[1].QuadPart == 0) { + ullResult = p[0].QuadPart / uc; + return bSign ? -(LONGLONG)ullResult : (LONGLONG)ullResult; + } + + /* If the divisor is a DWORD then its simpler */ + ULARGE_INTEGER ulic; + ulic.QuadPart = uc; + if (ulic.HighPart == 0) { + ULARGE_INTEGER uliDividend; + ULARGE_INTEGER uliResult; + DWORD dwDivisor = (DWORD)uc; + // ASSERT(p[1].HighPart == 0 && p[1].LowPart < dwDivisor); + uliDividend.HighPart = p[1].LowPart; + uliDividend.LowPart = p[0].HighPart; +#ifndef USE_LARGEINT + uliResult.HighPart = (DWORD)(uliDividend.QuadPart / dwDivisor); + p[0].HighPart = (DWORD)(uliDividend.QuadPart % dwDivisor); + uliResult.LowPart = 0; + uliResult.QuadPart = p[0].QuadPart / dwDivisor + uliResult.QuadPart; +#else + /* NOTE - this routine will take exceptions if + the result does not fit in a DWORD + */ + if (uliDividend.QuadPart >= (DWORDLONG)dwDivisor) { + uliResult.HighPart = EnlargedUnsignedDivide( + uliDividend, + dwDivisor, + &p[0].HighPart); + } else { + uliResult.HighPart = 0; + } + uliResult.LowPart = EnlargedUnsignedDivide( + p[0], + dwDivisor, + NULL); +#endif + return bSign ? -(LONGLONG)uliResult.QuadPart : + (LONGLONG)uliResult.QuadPart; + } + + + ullResult = 0; + + /* OK - do long division */ + for (int i = 0; i < 64; i++) { + ullResult <<= 1; + + /* Shift 128 bit p left 1 */ + p[1].QuadPart <<= 1; + if ((p[0].HighPart & 0x80000000) != 0) { + p[1].LowPart++; + } + p[0].QuadPart <<= 1; + + /* Compare */ + if (uc <= p[1].QuadPart) { + p[1].QuadPart -= uc; + ullResult += 1; + } + } + + return bSign ? - (LONGLONG)ullResult : (LONGLONG)ullResult; +} + +LONGLONG WINAPI Int64x32Div32(LONGLONG a, LONG b, LONG c, LONG d) +{ + ULARGE_INTEGER ua; + DWORD ub; + DWORD uc; + + /* Compute the absolute values to avoid signed arithmetic problems */ + ua.QuadPart = (DWORDLONG)(a >= 0 ? a : -a); + ub = (DWORD)(b >= 0 ? b : -b); + uc = (DWORD)(c >= 0 ? c : -c); + BOOL bSign = (a < 0) ^ (b < 0); + + /* Do long multiplication */ + ULARGE_INTEGER p0; + DWORD p1; + p0.QuadPart = UInt32x32To64(ua.LowPart, ub); + + if (ua.HighPart != 0) { + ULARGE_INTEGER x; + x.QuadPart = UInt32x32To64(ua.HighPart, ub) + p0.HighPart; + p0.HighPart = x.LowPart; + p1 = x.HighPart; + } else { + p1 = 0; + } + + if (d != 0) { + ULARGE_INTEGER ud0; + DWORD ud1; + + if (bSign) { + // + // Cast d to LONGLONG first otherwise -0x80000000 sign extends + // incorrectly + // + ud0.QuadPart = (DWORDLONG)(-(LONGLONG)d); + if (d > 0) { + /* -d < 0 */ + ud1 = (DWORD)-1; + } else { + ud1 = (DWORD)0; + } + } else { + ud0.QuadPart = (DWORDLONG)d; + if (d < 0) { + ud1 = (DWORD)-1; + } else { + ud1 = (DWORD)0; + } + } + /* Now do extended addition */ + ULARGE_INTEGER uliTotal; + + /* Add ls DWORDs */ + uliTotal.QuadPart = (DWORDLONG)ud0.LowPart + p0.LowPart; + p0.LowPart = uliTotal.LowPart; + + /* Propagate carry */ + uliTotal.LowPart = uliTotal.HighPart; + uliTotal.HighPart = 0; + + /* Add 2nd most ls DWORDs */ + uliTotal.QuadPart += (DWORDLONG)ud0.HighPart + p0.HighPart; + p0.HighPart = uliTotal.LowPart; + + /* Add MS DWORDLONGs - no carry expected */ + p1 += ud1 + uliTotal.HighPart; + + /* Now see if we got a sign change from the addition */ + if ((LONG)p1 < 0) { + bSign = !bSign; + + /* Negate the current value (ugh!) */ + p0.QuadPart = ~p0.QuadPart; + p1 = ~p1; + p0.QuadPart += 1; + p1 += (p0.QuadPart == 0); + } + } + + /* Now for the division */ + if (c < 0) { + bSign = !bSign; + } + + + /* This will catch c == 0 and overflow */ + if (uc <= p1) { + return bSign ? (LONGLONG)0x8000000000000000 : + (LONGLONG)0x7FFFFFFFFFFFFFFF; + } + + /* Do the division */ + + /* If the divisor is a DWORD then its simpler */ + ULARGE_INTEGER uliDividend; + ULARGE_INTEGER uliResult; + DWORD dwDivisor = uc; + uliDividend.HighPart = p1; + uliDividend.LowPart = p0.HighPart; + /* NOTE - this routine will take exceptions if + the result does not fit in a DWORD + */ + if (uliDividend.QuadPart >= (DWORDLONG)dwDivisor) { + uliResult.HighPart = EnlargedUnsignedDivide( + uliDividend, + dwDivisor, + &p0.HighPart); + } else { + uliResult.HighPart = 0; + } + uliResult.LowPart = EnlargedUnsignedDivide( + p0, + dwDivisor, + NULL); + return bSign ? -(LONGLONG)uliResult.QuadPart : + (LONGLONG)uliResult.QuadPart; +} + +#ifdef DEBUG +/******************************Public*Routine******************************\ +* Debug CCritSec helpers +* +* We provide debug versions of the Constructor, destructor, Lock and Unlock +* routines. The debug code tracks who owns each critical section by +* maintaining a depth count. +* +* History: +* +\**************************************************************************/ + +CCritSec::CCritSec(DWORD id) +{ + InitializeCriticalSection(&m_CritSec); + m_id = id; + m_currentOwner = m_lockCount = 0; + m_fTrace = FALSE; +} + +CCritSec::~CCritSec() +{ + DeleteCriticalSection(&m_CritSec); +} + +void CCritSec::Lock() +{ + UINT tracelevel=3; + DWORD us = GetCurrentThreadId(); + DWORD currentOwner = m_currentOwner; + if (currentOwner && (currentOwner != us)) { + // already owned, but not by us + if (m_fTrace) { + DbgLog((LOG_LOCKING, 2, TEXT("Thread %d about to wait for lock %x owned by %d"), + GetCurrentThreadId(), &m_CritSec, currentOwner)); + tracelevel=2; + // if we saw the message about waiting for the critical + // section we ensure we see the message when we get the + // critical section + } + } + + EnterCriticalSection(&m_CritSec); + + if (0 == m_lockCount++) { + // we now own it for the first time. Set owner information + m_currentOwner = us; + + if (m_fTrace) { + DbgLog((LOG_LOCKING, tracelevel, TEXT("Thread %d now owns lock %x"), m_currentOwner, &m_CritSec)); + } + } +} + +void CCritSec::Unlock() { + if (0 == --m_lockCount) { + // about to be unowned + if (m_fTrace) { + DbgLog((LOG_LOCKING, 3, TEXT("Thread %d releasing lock %x"), m_currentOwner, &m_CritSec)); + } + + m_currentOwner = 0; + } + LeaveCriticalSection(&m_CritSec); +} + +void WINAPI DbgLockTrace(CCritSec * pcCrit, BOOL fTrace) +{ + pcCrit->m_fTrace = fTrace; +} + +BOOL WINAPI CritCheckIn(CCritSec * pcCrit) +{ + return (GetCurrentThreadId() == pcCrit->m_currentOwner); +} + +BOOL WINAPI CritCheckIn(const CCritSec * pcCrit) +{ + return (GetCurrentThreadId() == pcCrit->m_currentOwner); +} + +BOOL WINAPI CritCheckOut(CCritSec * pcCrit) +{ + return (GetCurrentThreadId() != pcCrit->m_currentOwner); +} + +BOOL WINAPI CritCheckOut(const CCritSec * pcCrit) +{ + return (GetCurrentThreadId() != pcCrit->m_currentOwner); +} +#endif + + +STDAPI WriteBSTR(BSTR *pstrDest, LPCWSTR szSrc) +{ + *pstrDest = SysAllocString( szSrc ); + if( !(*pstrDest) ) return E_OUTOFMEMORY; + return NOERROR; +} + + +STDAPI FreeBSTR(BSTR* pstr) +{ + if( *pstr == NULL ) return S_FALSE; + SysFreeString( *pstr ); + return NOERROR; +} + + +// Return a wide string - allocating memory for it +// Returns: +// S_OK - no error +// E_POINTER - ppszReturn == NULL +// E_OUTOFMEMORY - can't allocate memory for returned string +STDAPI AMGetWideString(LPCWSTR psz, LPWSTR *ppszReturn) +{ + CheckPointer(ppszReturn, E_POINTER); + ValidateReadWritePtr(ppszReturn, sizeof(LPWSTR)); + DWORD nameLen = sizeof(WCHAR) * (lstrlenW(psz)+1); + *ppszReturn = (LPWSTR)CoTaskMemAlloc(nameLen); + if (*ppszReturn == NULL) { + return E_OUTOFMEMORY; + } + CopyMemory(*ppszReturn, psz, nameLen); + return NOERROR; +} + +// Waits for the HANDLE hObject. While waiting messages sent +// to windows on our thread by SendMessage will be processed. +// Using this function to do waits and mutual exclusion +// avoids some deadlocks in objects with windows. +// Return codes are the same as for WaitForSingleObject +DWORD WINAPI WaitDispatchingMessages( + HANDLE hObject, + DWORD dwWait, + HWND hwnd, + UINT uMsg, + HANDLE hEvent) +{ + BOOL bPeeked = FALSE; + DWORD dwResult; + DWORD dwStart = 0; + DWORD dwThreadPriority = 0; + + static UINT uMsgId = 0; + + HANDLE hObjects[2] = { hObject, hEvent }; + if (dwWait != INFINITE && dwWait != 0) { + dwStart = GetTickCount(); + } + for (; ; ) { + DWORD nCount = NULL != hEvent ? 2 : 1; + + // Minimize the chance of actually dispatching any messages + // by seeing if we can lock immediately. + dwResult = WaitForMultipleObjects(nCount, hObjects, FALSE, 0); + if (dwResult < WAIT_OBJECT_0 + nCount) { + break; + } + + DWORD dwTimeOut = dwWait; + if (dwTimeOut > 10) { + dwTimeOut = 10; + } + dwResult = MsgWaitForMultipleObjects( + nCount, + hObjects, + FALSE, + dwTimeOut, + hwnd == NULL ? QS_SENDMESSAGE : + QS_SENDMESSAGE + QS_POSTMESSAGE); + if (dwResult == WAIT_OBJECT_0 + nCount || + dwResult == WAIT_TIMEOUT && dwTimeOut != dwWait) { + MSG msg; + if (hwnd != NULL) { + while (PeekMessage(&msg, hwnd, uMsg, uMsg, PM_REMOVE)) { + DispatchMessage(&msg); + } + } + // Do this anyway - the previous peek doesn't flush out the + // messages + PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE); + + if (dwWait != INFINITE && dwWait != 0) { + DWORD dwNow = GetTickCount(); + + // Working with differences handles wrap-around + DWORD dwDiff = dwNow - dwStart; + if (dwDiff > dwWait) { + dwWait = 0; + } else { + dwWait -= dwDiff; + } + dwStart = dwNow; + } + if (!bPeeked) { + // Raise our priority to prevent our message queue + // building up + dwThreadPriority = GetThreadPriority(GetCurrentThread()); + if (dwThreadPriority < THREAD_PRIORITY_HIGHEST) { + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); + } + bPeeked = TRUE; + } + } else { + break; + } + } + if (bPeeked) { + SetThreadPriority(GetCurrentThread(), dwThreadPriority); + if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) { + if (uMsgId == 0) { + uMsgId = RegisterWindowMessage(TEXT("AMUnblock")); + } + if (uMsgId != 0) { + MSG msg; + // Remove old ones + while (PeekMessage(&msg, (HWND)-1, uMsgId, uMsgId, PM_REMOVE)) { + } + } + PostThreadMessage(GetCurrentThreadId(), uMsgId, 0, 0); + } + } + return dwResult; +} + +HRESULT AmGetLastErrorToHResult() +{ + DWORD dwLastError = GetLastError(); + if(dwLastError != 0) + { + return HRESULT_FROM_WIN32(dwLastError); + } + else + { + return E_FAIL; + } +} + +IUnknown* QzAtlComPtrAssign(IUnknown** pp, IUnknown* lp) +{ + if (lp != NULL) + lp->AddRef(); + if (*pp) + (*pp)->Release(); + *pp = lp; + return lp; +} + +/****************************************************************************** + +CompatibleTimeSetEvent + + CompatibleTimeSetEvent() sets the TIME_KILL_SYNCHRONOUS flag before calling +timeSetEvent() if the current operating system supports it. TIME_KILL_SYNCHRONOUS +is supported on Windows XP and later operating systems. + +Parameters: +- The same parameters as timeSetEvent(). See timeSetEvent()'s documentation in +the Platform SDK for more information. + +Return Value: +- The same return value as timeSetEvent(). See timeSetEvent()'s documentation in +the Platform SDK for more information. + +******************************************************************************/ +MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent ) +{ + #if WINVER >= 0x0501 + { + static bool fCheckedVersion = false; + static bool fTimeKillSynchronousFlagAvailable = false; + + if( !fCheckedVersion ) { + fTimeKillSynchronousFlagAvailable = TimeKillSynchronousFlagAvailable(); + fCheckedVersion = true; + } + + if( fTimeKillSynchronousFlagAvailable ) { + fuEvent = fuEvent | TIME_KILL_SYNCHRONOUS; + } + } + #endif // WINVER >= 0x0501 + + return timeSetEvent( uDelay, uResolution, lpTimeProc, dwUser, fuEvent ); +} + +bool TimeKillSynchronousFlagAvailable( void ) +{ + OSVERSIONINFO osverinfo; + + osverinfo.dwOSVersionInfoSize = sizeof(osverinfo); + + if( GetVersionEx( &osverinfo ) ) { + + // Windows XP's major version is 5 and its' minor version is 1. + // timeSetEvent() started supporting the TIME_KILL_SYNCHRONOUS flag + // in Windows XP. + if( (osverinfo.dwMajorVersion > 5) || + ( (osverinfo.dwMajorVersion == 5) && (osverinfo.dwMinorVersion >= 1) ) ) { + return true; + } + } + + return false; +} diff --git a/plugins/GSdx/baseclasses/wxutil.h b/plugins/GSdx/baseclasses/wxutil.h new file mode 100644 index 0000000000..1d7d9f82c2 --- /dev/null +++ b/plugins/GSdx/baseclasses/wxutil.h @@ -0,0 +1,541 @@ +//------------------------------------------------------------------------------ +// File: WXUtil.h +// +// Desc: DirectShow base classes - defines helper classes and functions for +// building multimedia filters. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +#ifndef __WXUTIL__ +#define __WXUTIL__ + +// eliminate spurious "statement has no effect" warnings. +#pragma warning(disable: 4705) + +// wrapper for whatever critical section we have +class CCritSec { + + // make copy constructor and assignment operator inaccessible + + CCritSec(const CCritSec &refCritSec); + CCritSec &operator=(const CCritSec &refCritSec); + + CRITICAL_SECTION m_CritSec; + +#ifdef DEBUG +public: + DWORD m_id; + DWORD m_currentOwner; + DWORD m_lockCount; + BOOL m_fTrace; // Trace this one +public: + CCritSec(DWORD id = 0); + ~CCritSec(); + void Lock(); + void Unlock(); +#else + +public: + CCritSec() { + InitializeCriticalSection(&m_CritSec); + }; + + ~CCritSec() { + DeleteCriticalSection(&m_CritSec); + }; + + void Lock() { + EnterCriticalSection(&m_CritSec); + }; + + void Unlock() { + LeaveCriticalSection(&m_CritSec); + }; +#endif +}; + +// +// To make deadlocks easier to track it is useful to insert in the +// code an assertion that says whether we own a critical section or +// not. We make the routines that do the checking globals to avoid +// having different numbers of member functions in the debug and +// retail class implementations of CCritSec. In addition we provide +// a routine that allows usage of specific critical sections to be +// traced. This is NOT on by default - there are far too many. +// + +#ifdef DEBUG + BOOL WINAPI CritCheckIn(CCritSec * pcCrit); + BOOL WINAPI CritCheckIn(const CCritSec * pcCrit); + BOOL WINAPI CritCheckOut(CCritSec * pcCrit); + BOOL WINAPI CritCheckOut(const CCritSec * pcCrit); + void WINAPI DbgLockTrace(CCritSec * pcCrit, BOOL fTrace); +#else + #define CritCheckIn(x) TRUE + #define CritCheckOut(x) TRUE + #define DbgLockTrace(pc, fT) +#endif + + +// locks a critical section, and unlocks it automatically +// when the lock goes out of scope +class CAutoLock { + + // make copy constructor and assignment operator inaccessible + + CAutoLock(const CAutoLock &refAutoLock); + CAutoLock &operator=(const CAutoLock &refAutoLock); + +protected: + CCritSec * m_pLock; + +public: + CAutoLock(CCritSec * plock) + { + m_pLock = plock; + m_pLock->Lock(); + }; + + ~CAutoLock() { + m_pLock->Unlock(); + }; +}; + + + +// wrapper for event objects +class CAMEvent +{ + + // make copy constructor and assignment operator inaccessible + + CAMEvent(const CAMEvent &refEvent); + CAMEvent &operator=(const CAMEvent &refEvent); + +protected: + HANDLE m_hEvent; +public: + CAMEvent(BOOL fManualReset = FALSE); + ~CAMEvent(); + + // Cast to HANDLE - we don't support this as an lvalue + operator HANDLE () const { return m_hEvent; }; + + void Set() {EXECUTE_ASSERT(SetEvent(m_hEvent));}; + BOOL Wait(DWORD dwTimeout = INFINITE) { + return (WaitForSingleObject(m_hEvent, dwTimeout) == WAIT_OBJECT_0); + }; + void Reset() { ResetEvent(m_hEvent); }; + BOOL Check() { return Wait(0); }; +}; + + +// wrapper for event objects that do message processing +// This adds ONE method to the CAMEvent object to allow sent +// messages to be processed while waiting + +class CAMMsgEvent : public CAMEvent +{ + +public: + + // Allow SEND messages to be processed while waiting + BOOL WaitMsg(DWORD dwTimeout = INFINITE); +}; + +// old name supported for the time being +#define CTimeoutEvent CAMEvent + +// support for a worker thread + +// simple thread class supports creation of worker thread, synchronization +// and communication. Can be derived to simplify parameter passing +class AM_NOVTABLE CAMThread { + + // make copy constructor and assignment operator inaccessible + + CAMThread(const CAMThread &refThread); + CAMThread &operator=(const CAMThread &refThread); + + CAMEvent m_EventSend; + CAMEvent m_EventComplete; + + DWORD m_dwParam; + DWORD m_dwReturnVal; + +protected: + HANDLE m_hThread; + + // thread will run this function on startup + // must be supplied by derived class + virtual DWORD ThreadProc() = 0; + +public: + CAMThread(); + virtual ~CAMThread(); + + CCritSec m_AccessLock; // locks access by client threads + CCritSec m_WorkerLock; // locks access to shared objects + + // thread initially runs this. param is actually 'this'. function + // just gets this and calls ThreadProc + static DWORD WINAPI InitialThreadProc(LPVOID pv); + + // start thread running - error if already running + BOOL Create(); + + // signal the thread, and block for a response + // + DWORD CallWorker(DWORD); + + // accessor thread calls this when done with thread (having told thread + // to exit) + void Close() { + #pragma warning( push ) + // C4312: 'type cast' : conversion from 'LONG' to 'PVOID' of greater size + // + // This code works correctly on 32-bit and 64-bit systems. + #pragma warning( disable : 4312 ) + HANDLE hThread = (HANDLE)InterlockedExchangePointer(&m_hThread, 0); + #pragma warning( pop ) + + if (hThread) { + WaitForSingleObject(hThread, INFINITE); + CloseHandle(hThread); + } + }; + + // ThreadExists + // Return TRUE if the thread exists. FALSE otherwise + BOOL ThreadExists(void) const + { + if (m_hThread == 0) { + return FALSE; + } else { + return TRUE; + } + } + + // wait for the next request + DWORD GetRequest(); + + // is there a request? + BOOL CheckRequest(DWORD * pParam); + + // reply to the request + void Reply(DWORD); + + // If you want to do WaitForMultipleObjects you'll need to include + // this handle in your wait list or you won't be responsive + HANDLE GetRequestHandle() const { return m_EventSend; }; + + // Find out what the request was + DWORD GetRequestParam() const { return m_dwParam; }; + + // call CoInitializeEx (COINIT_DISABLE_OLE1DDE) if + // available. S_FALSE means it's not available. + static HRESULT CoInitializeHelper(); +}; + + +// CQueue +// +// Implements a simple Queue ADT. The queue contains a finite number of +// objects, access to which is controlled by a semaphore. The semaphore +// is created with an initial count (N). Each time an object is added +// a call to WaitForSingleObject is made on the semaphore's handle. When +// this function returns a slot has been reserved in the queue for the new +// object. If no slots are available the function blocks until one becomes +// available. Each time an object is removed from the queue ReleaseSemaphore +// is called on the semaphore's handle, thus freeing a slot in the queue. +// If no objects are present in the queue the function blocks until an +// object has been added. + +#define DEFAULT_QUEUESIZE 2 + +template class CQueue { +private: + HANDLE hSemPut; // Semaphore controlling queue "putting" + HANDLE hSemGet; // Semaphore controlling queue "getting" + CRITICAL_SECTION CritSect; // Thread seriallization + int nMax; // Max objects allowed in queue + int iNextPut; // Array index of next "PutMsg" + int iNextGet; // Array index of next "GetMsg" + T *QueueObjects; // Array of objects (ptr's to void) + + void Initialize(int n) { + iNextPut = iNextGet = 0; + nMax = n; + InitializeCriticalSection(&CritSect); + hSemPut = CreateSemaphore(NULL, n, n, NULL); + hSemGet = CreateSemaphore(NULL, 0, n, NULL); + QueueObjects = new T[n]; + } + + +public: + CQueue(int n) { + Initialize(n); + } + + CQueue() { + Initialize(DEFAULT_QUEUESIZE); + } + + ~CQueue() { + delete [] QueueObjects; + DeleteCriticalSection(&CritSect); + CloseHandle(hSemPut); + CloseHandle(hSemGet); + } + + T GetQueueObject() { + int iSlot; + T Object; + LONG lPrevious; + + // Wait for someone to put something on our queue, returns straight + // away is there is already an object on the queue. + // + WaitForSingleObject(hSemGet, INFINITE); + + EnterCriticalSection(&CritSect); + iSlot = iNextGet++ % nMax; + Object = QueueObjects[iSlot]; + LeaveCriticalSection(&CritSect); + + // Release anyone waiting to put an object onto our queue as there + // is now space available in the queue. + // + ReleaseSemaphore(hSemPut, 1L, &lPrevious); + return Object; + } + + void PutQueueObject(T Object) { + int iSlot; + LONG lPrevious; + + // Wait for someone to get something from our queue, returns straight + // away is there is already an empty slot on the queue. + // + WaitForSingleObject(hSemPut, INFINITE); + + EnterCriticalSection(&CritSect); + iSlot = iNextPut++ % nMax; + QueueObjects[iSlot] = Object; + LeaveCriticalSection(&CritSect); + + // Release anyone waiting to remove an object from our queue as there + // is now an object available to be removed. + // + ReleaseSemaphore(hSemGet, 1L, &lPrevious); + } +}; + +// miscellaneous string conversion functions +// NOTE: as we need to use the same binaries on Win95 as on NT this code should +// be compiled WITHOUT unicode being defined. Otherwise we will not pick up +// these internal routines and the binary will not run on Win95. + +// int WINAPIV wsprintfWInternal(LPWSTR, LPCWSTR, ...); + +//LPWSTR +//WINAPI +//lstrcpyWInternal( +// LPWSTR lpString1, +// LPCWSTR lpString2 +// ); +LPWSTR +WINAPI +lstrcpynWInternal( + LPWSTR lpString1, + LPCWSTR lpString2, + int iMaxLength + ); +int +WINAPI +lstrcmpWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ); +int +WINAPI +lstrcmpiWInternal( + LPCWSTR lpString1, + LPCWSTR lpString2 + ); +int +WINAPI +lstrlenWInternal( + LPCWSTR lpString + ); + +#ifndef UNICODE +#define wsprintfW wsprintfWInternal +#define lstrcpyW lstrcpyWInternal +#define lstrcpynW lstrcpynWInternal +#define lstrcmpW lstrcmpWInternal +#define lstrcmpiW lstrcmpiWInternal +#define lstrlenW lstrlenWInternal +#endif + +extern "C" +void * __stdcall memmoveInternal(void *, const void *, size_t); + +inline void * __cdecl memchrInternal(const void *buf, int chr, size_t cnt) +{ +#ifdef _X86_ + void *pRet = NULL; + + _asm { + cld // make sure we get the direction right + mov ecx, cnt // num of bytes to scan + mov edi, buf // pointer byte stream + mov eax, chr // byte to scan for + repne scasb // look for the byte in the byte stream + jnz exit_memchr // Z flag set if byte found + dec edi // scasb always increments edi even when it + // finds the required byte + mov pRet, edi +exit_memchr: + } + return pRet; + +#else + while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) { + buf = (unsigned char *)buf + 1; + cnt--; + } + + return(cnt ? (void *)buf : NULL); +#endif +} + +void WINAPI IntToWstr(int i, LPWSTR wstr, size_t len); + +#define WstrToInt(sz) _wtoi(sz) +#define atoiW(sz) _wtoi(sz) +#define atoiA(sz) atoi(sz) + +// These are available to help managing bitmap VIDEOINFOHEADER media structures + +extern const DWORD bits555[3]; +extern const DWORD bits565[3]; +extern const DWORD bits888[3]; + +// These help convert between VIDEOINFOHEADER and BITMAPINFO structures + +STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader); +STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader); +STDAPI_(WORD) GetBitCount(const GUID *pSubtype); + +// strmbase.lib implements this for compatibility with people who +// managed to link to this directly. we don't want to advertise it. +// +// STDAPI_(/* T */ CHAR *) GetSubtypeName(const GUID *pSubtype); + +STDAPI_(CHAR *) GetSubtypeNameA(const GUID *pSubtype); +STDAPI_(WCHAR *) GetSubtypeNameW(const GUID *pSubtype); + +#ifdef UNICODE +#define GetSubtypeName GetSubtypeNameW +#else +#define GetSubtypeName GetSubtypeNameA +#endif + +STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader); +STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader); +STDAPI_(BOOL) ContainsPalette(const VIDEOINFOHEADER *pVideoInfo); +STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFOHEADER *pVideoInfo); + + +// Compares two interfaces and returns TRUE if they are on the same object +BOOL WINAPI IsEqualObject(IUnknown *pFirst, IUnknown *pSecond); + +// This is for comparing pins +#define EqualPins(pPin1, pPin2) IsEqualObject(pPin1, pPin2) + + +// Arithmetic helper functions + +// Compute (a * b + rnd) / c +LONGLONG WINAPI llMulDiv(LONGLONG a, LONGLONG b, LONGLONG c, LONGLONG rnd); +LONGLONG WINAPI Int64x32Div32(LONGLONG a, LONG b, LONG c, LONG rnd); + + +// Avoids us dyna-linking to SysAllocString to copy BSTR strings +STDAPI WriteBSTR(BSTR * pstrDest, LPCWSTR szSrc); +STDAPI FreeBSTR(BSTR* pstr); + +// Return a wide string - allocating memory for it +// Returns: +// S_OK - no error +// E_POINTER - ppszReturn == NULL +// E_OUTOFMEMORY - can't allocate memory for returned string +STDAPI AMGetWideString(LPCWSTR pszString, LPWSTR *ppszReturn); + +// Special wait for objects owning windows +DWORD WINAPI WaitDispatchingMessages( + HANDLE hObject, + DWORD dwWait, + HWND hwnd = NULL, + UINT uMsg = 0, + HANDLE hEvent = NULL); + +// HRESULT_FROM_WIN32 converts ERROR_SUCCESS to a success code, but in +// our use of HRESULT_FROM_WIN32, it typically means a function failed +// to call SetLastError(), and we still want a failure code. +// +#define AmHresultFromWin32(x) (MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, x)) + +// call GetLastError and return an HRESULT value that will fail the +// SUCCEEDED() macro. +HRESULT AmGetLastErrorToHResult(void); + +// duplicate of ATL's CComPtr to avoid linker conflicts. + +IUnknown* QzAtlComPtrAssign(IUnknown** pp, IUnknown* lp); + +template +class QzCComPtr +{ +public: + typedef T _PtrClass; + QzCComPtr() {p=NULL;} + QzCComPtr(T* lp) + { + if ((p = lp) != NULL) + p->AddRef(); + } + QzCComPtr(const QzCComPtr& lp) + { + if ((p = lp.p) != NULL) + p->AddRef(); + } + ~QzCComPtr() {if (p) p->Release();} + void Release() {if (p) p->Release(); p=NULL;} + operator T*() {return (T*)p;} + T& operator*() {ASSERT(p!=NULL); return *p; } + //The assert on operator& usually indicates a bug. If this is really + //what is needed, however, take the address of the p member explicitly. + T** operator&() { ASSERT(p==NULL); return &p; } + T* operator->() { ASSERT(p!=NULL); return p; } + T* operator=(T* lp){return (T*)QzAtlComPtrAssign((IUnknown**)&p, lp);} + T* operator=(const QzCComPtr& lp) + { + return (T*)QzAtlComPtrAssign((IUnknown**)&p, lp.p); + } +#if _MSC_VER>1020 + bool operator!(){return (p == NULL);} +#else + BOOL operator!(){return (p == NULL) ? TRUE : FALSE;} +#endif + T* p; +}; + +MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent ); +bool TimeKillSynchronousFlagAvailable( void ); + +#endif /* __WXUTIL__ */ diff --git a/plugins/GSdx/res/GSdx.rc2 b/plugins/GSdx/res/GSdx.rc2 new file mode 100644 index 0000000000..45a9aad126 --- /dev/null +++ b/plugins/GSdx/res/GSdx.rc2 @@ -0,0 +1,22 @@ +// +// GSdx.RC2 - resources Microsoft Visual C++ does not edit directly +// + +#ifdef APSTUDIO_INVOKED +#error this file is not editable by Microsoft Visual C++ +#endif //APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// Add manually edited resources here... + +IDR_CONVERT9_FX RCDATA "res\\convert9.fx" +IDR_TFX9_FX RCDATA "res\\tfx9.fx" +IDR_MERGE9_FX RCDATA "res\\merge9.fx" +IDR_INTERLACE9_FX RCDATA "res\\interlace9.fx" +IDR_CONVERT10_FX RCDATA "res\\convert10.fx" +IDR_TFX10_FX RCDATA "res\\tfx10.fx" +IDR_MERGE10_FX RCDATA "res\\merge10.fx" +IDR_INTERLACE10_FX RCDATA "res\\interlace10.fx" + +///////////////////////////////////////////////////////////////////////////// diff --git a/plugins/GSdx/res/convert10.fx b/plugins/GSdx/res/convert10.fx new file mode 100644 index 0000000000..05044323cc --- /dev/null +++ b/plugins/GSdx/res/convert10.fx @@ -0,0 +1,67 @@ +struct VS_INPUT +{ + float4 p : POSITION; + float2 t : TEXCOORD0; +}; + +struct VS_OUTPUT +{ + float4 p : SV_Position; + float2 t : TEXCOORD0; +}; + +VS_OUTPUT vs_main(VS_INPUT input) +{ + VS_OUTPUT output; + + output.p = input.p; + output.t = input.t; + + return output; +} + +Texture2D Texture; +SamplerState Sampler; + +struct PS_INPUT +{ + float4 p : SV_Position; + float2 t : TEXCOORD0; +}; + +float4 ps_main0(PS_INPUT input) : SV_Target0 +{ + return Texture.Sample(Sampler, input.t); +} + +uint ps_main1(PS_INPUT input) : SV_Target0 +{ + float4 f = Texture.Sample(Sampler, input.t); + + f.a *= 256.0f/127; // hm, 0.5 won't give us 1.0 if we just multiply with 2 + + uint4 i = f * float4(0x001f, 0x03e0, 0x7c00, 0x8000); + + return (i.x & 0x001f) | (i.y & 0x03e0) | (i.z & 0x7c00) | (i.w & 0x8000); +} + +float4 ps_main2(PS_INPUT input) : SV_Target0 +{ + clip(Texture.Sample(Sampler, input.t).a - (0.5 - 0.9f/256)); + + return 0; +} + +float4 ps_main3(PS_INPUT input) : SV_Target0 +{ + clip((0.5 - 0.9f/256) - Texture.Sample(Sampler, input.t).a); + + return 0; +} + +float4 ps_main4(PS_INPUT input) : SV_Target0 +{ + float4 c = Texture.Sample(Sampler, input.t); + + return fmod(c * 255 + 0.5f, 256) / 255; +} diff --git a/plugins/GSdx/res/convert9.fx b/plugins/GSdx/res/convert9.fx new file mode 100644 index 0000000000..4f1f8be5da --- /dev/null +++ b/plugins/GSdx/res/convert9.fx @@ -0,0 +1,55 @@ +struct VS_INPUT +{ + float4 p : POSITION; + float2 t : TEXCOORD0; +}; + +struct VS_OUTPUT +{ + float4 p : POSITION; + float2 t : TEXCOORD0; +}; + +VS_OUTPUT vs_main(VS_INPUT input) +{ + VS_OUTPUT output; + + output.p = input.p; + output.t = input.t; + + return output; +} + +sampler Texture : register(s0); + +float4 ps_main0(float2 t : TEXCOORD0) : COLOR +{ + return tex2D(Texture, t); +} + +float4 ps_main1(float2 t : TEXCOORD0) : COLOR +{ + float4 c = tex2D(Texture, t); + c.a *= 128.0f / 255; // *= 0.5f is no good here, need to do this in order to get 0x80 for 1.0f (instead of 0x7f) + return c; +} + +float4 ps_main2(float2 t : TEXCOORD0) : COLOR +{ + clip(tex2D(Texture, t).a - (1.0f - 0.9f/256)); + + return 0; +} + +float4 ps_main3(float2 t : TEXCOORD0) : COLOR +{ + clip((1.0f - 0.9f/256) - tex2D(Texture, t).a); + + return 0; +} + +float4 ps_main4() : COLOR +{ + return 1; +} + diff --git a/plugins/GSdx/res/interlace10.fx b/plugins/GSdx/res/interlace10.fx new file mode 100644 index 0000000000..86fa44dd75 --- /dev/null +++ b/plugins/GSdx/res/interlace10.fx @@ -0,0 +1,43 @@ + +Texture2D Texture; +SamplerState Sampler; + +cbuffer cb0 +{ + float2 ZrH; + float hH; +}; + +struct PS_INPUT +{ + float4 p : SV_Position; + float2 t : TEXCOORD0; +}; + +float4 ps_main0(PS_INPUT input) : SV_Target0 +{ + clip(frac(input.t.y * hH) - 0.5); + + return Texture.Sample(Sampler, input.t); +} + +float4 ps_main1(PS_INPUT input) : SV_Target0 +{ + clip(0.5 - frac(input.t.y * hH)); + + return Texture.Sample(Sampler, input.t); +} + +float4 ps_main2(PS_INPUT input) : SV_Target0 +{ + float4 c0 = Texture.Sample(Sampler, input.t - ZrH); + float4 c1 = Texture.Sample(Sampler, input.t); + float4 c2 = Texture.Sample(Sampler, input.t + ZrH); + + return (c0 + c1 * 2 + c2) / 4; +} + +float4 ps_main3(PS_INPUT input) : SV_Target0 +{ + return Texture.Sample(Sampler, input.t); +} diff --git a/plugins/GSdx/res/interlace9.fx b/plugins/GSdx/res/interlace9.fx new file mode 100644 index 0000000000..5f5db8bf9c --- /dev/null +++ b/plugins/GSdx/res/interlace9.fx @@ -0,0 +1,35 @@ + +sampler s0 : register(s0); + +float4 Params1 : register(c0); + +#define ZrH (Params1.xy) +#define hH (Params1.z) + +float4 ps_main0(float2 tex : TEXCOORD0) : COLOR +{ + clip(frac(tex.y * hH) - 0.5); + + return tex2D(s0, tex); +} + +float4 ps_main1(float2 tex : TEXCOORD0) : COLOR +{ + clip(0.5 - frac(tex.y * hH)); + + return tex2D(s0, tex); +} + +float4 ps_main2(float2 tex : TEXCOORD0) : COLOR +{ + float4 c0 = tex2D(s0, tex - ZrH); + float4 c1 = tex2D(s0, tex); + float4 c2 = tex2D(s0, tex + ZrH); + + return (c0 + c1 * 2 + c2) / 4; +} + +float4 ps_main3(float2 tex : TEXCOORD0) : COLOR +{ + return tex2D(s0, tex); +} diff --git a/plugins/GSdx/res/logo10.bmp b/plugins/GSdx/res/logo10.bmp new file mode 100644 index 0000000000000000000000000000000000000000..25f6b177c7e6ecd22de266dceb4bb43189d68534 GIT binary patch literal 53094 zcmeI*1-xCw)js}nZ$gk3KK0VRZGo1)XmJn45+JU`jRd zg%Gq*w83fn=Kq~N=j@!^d!v1&zyBx0WZyY^&mLLpSYCl$89*oeH1B`EV-ay-HoWbZO9_L8C^EI(6!l zG;oche@VXPnts>BfBtM&3ZJeGhwan$r^2Ue9g=^J{w6t;il>$>Tb_LK$?e;>-*)M` zTv6e=@XzP#l9c=j=iM~L z0c1`FjFoV;SoL@!iR665-|2Jp=x_5fShB}M;APAgY!&A}%DO70NQ0yB}V5}E>{cxrcn z6xb*So-8wEZ>8bviCBtyPY{=nub^)gfFMF5%SRCb0WTq!jE4j&B^6oKLxssv33 zw1h(VxI@^u&dx8p?6MP2Jn_gQkNoX#e|yR)r&Ov`>Gw&x^wLXv_wEfRIR8KY^FI}6 z)xa;lxP0x)YWM8f^Q4nbI_|jR4nO>GzToo9FaO=|es}cIM}PnO-*4KqDJWiX#TA;W zTD7VnTyn`JHEPs|lg>>&j4BJG1S<5}u3ftmPB=lKd5h-99CM7;>ej8>t5+`_b?eqG zX#R#9x_0eqkWR*r`2YChkFQgwju9%HefHV7u0@L$Kl;&+I0&OU>gc0fs#CWPAeiPk z=bUryx#w!x6feEBg4W!1{`u!Ozr6W*=bhK4O&bFsiqlU&-KEZ*JDVV!b?nqhwHt37 zAZz5OKmDo6o^i$*-Me)kaN~exO`9Eh=%E@^*{rdFdj|{{c=R#HT-mLgd;joL*%~5>L@Ls zotp*@3|R_?q!2z7P3zWf&6jz$91#{F3Wz#RK$(SK__$Xo;4R=UpQd((JnG-g6p)v# znt(8T$`Memko%iEA80AZOzF9j3s@>v71$6v^dgCd4IBLOm%ju6IPcK0gY&IgwE{T^ z`Pt8Y7O_X3r=5CQwQAKGHf%`g6#es`|GaVIM)20SapUgYyZ^_3{0IEg8g;V>Y9(3F zVi)d>K8^y+EwUw~1%xI48aHXe9=X-Sk2u2Z`u)|fe&xvAuIzT@fFv=@BaS#iNAaVP z>+!VHPODtGaJ(@v4ss8J&Zf)?W$ zZSQe}4ufFb|F3okTm-Kn4*_O3wYsxkTc_3N_&PK`WuN(F4n zjoQaKL@ELtl%~Oh2Fc+tcv~KvO}F2^wW#`wVJ21xct{Yj4L>=8qW5s6s9Ot8FF+`% z7Tn%0QsTwJA@yTf2e<7+zH`g@y4>IVYiT*4CZH!AB7*!0DSV2h?T#g%9;nX10RV|} zpokpm)vNcLB)Q@vW%&e$0|yR7O+WtekD(o;Dc6w(nZv24p6V*|p*~u>>Z+^U%Ys}& zyp#xUY(Z<$7HE?jVh)ZWj=Ju;FpB{LQf32J4B6wV9!vm?4bK>iMB^Phc7!fUzw*i} z4?5@|CWBYu2lIna%a$!c7YL94!|^?O^w0>7br*x;U(P)9OjA%s!WuDCB>$72{6s`& zkj%u8nl)<%f$Z8Dy_hC*MigwpE$-FJ;2}c>4;}(~S9R~M*_N$Z!rYJ{LxLTh@CQ*i znK~2g7d#=nef!X_(UODih727dek)h*BE!}lxe=8524mPJYa}&m)>L!QO*gR%vSn7s zA9uX-9on~7*<46qXqtv)OT&gL4RZ`RmtK2qEAeih2GVk1c;SVF& z+O%nV!37t9P?f4xgV$)%INCywo;^=1SE2NY=O21rmCDtsi*`%~q9_#`3pRv37hHHD zBZYS6X20ThM;-|V@I%uqF4#+t9vX+4W=U+;^~x);>se=>Ri$bb1w_imPCV%(hy{CR zAl#V6HlPLe;tfo$K~V~{BX)>A<&;wml})L6(n%-Qs9B?9r;bRVVucE4oPIhoA3kD4 zn!9!H4&mhEj4}M+2S3nJx$@=2xD1NN;f&0^eEITcoOz~W#Y&aRmn%17#K@#vk@22% z;z{k=wKq0rPCw%e*2*GSN3`qe)vJr;%#kz>Iph!~P^V5^(onxs=guQT8nrEtO3IGg z=XLA0=0K7|$o39o_tBE&0T4k5|6(4RZ2>b39x`06vC7C@05Q-p%8%p+klZ z8-fr5C8Vs77P2Ifvc=Hh!*k`~!-mVlh7KP_j{w7SCTE5-RXDuxqKmAh;u*)zUAlm% zLYJ;xDf-N_&a%Y7?QF!!mMvQW9`kR}vL%>_b1acMbnGY$>VDN#&NOJ)u+UHTzu~`Wi8EBq(aXUHkU#y}WsI_wpSi-?m-5maSTe zj?`g0OoMj;5@>JOsFA=%%T1d#waVa9(I(p*F>2IRJ$krNYSg$f=o-dPfA%vYF$;Hb zc?=&%v*iX28tTY-ifGfet?(~g9Wn%wv6mJtTNnxFK5F!6IqWF*?aTT=-Nc<~9_;88 zqKg#_Rh+L2(>H*|O`59K)3|&09zABXQC;1)uL>ufd@_qBe_8KQ$aU-0Q`Q4t!+s-m zYmPL?*r=R0_c3F~=I62L$8q6&NaMm`;liy!>BEN+?;hphNs6}&A3-;A_K(sqpYv#; zXnEww;UhIbkE2J0TU7Zr2X=m^&dckvaKjh4oYDKWD+;IT|_RbONUSMb2%M7+O0`+`!< z2V_P+RIuu^(u>|Xv|5WcZQBsL$_68h5htrZs#xPu`*0|hfFE87*&48@Z0xvkyUg0> zlE#i3FZ)j(JAOPuV4|bPjAf)SOqk=wySlSX4yT-YstAY0g_;w_12#VC)VY(b1``=S zVZtu6VH-a|sX1Z7L`O}DvdnS9#EE;FC%Wb4Y@T@Y#ECcGoaBl3-M4k*=+QApr7P!a zKd&4#HEN6;3T!qOk~3}@ld2<}PjZQ>-|pnt6dq$qIOMV0Ws{9(qGpw2p`_!u>{Z4d za!|KpT8P>&ekMglteO2t^(kNy7>vyx$D3)ZD_Y}ZAyD6b*Kk|Snm4apwMy%@ZJFM< z2@|lTCkK@(SFTmNcE4+{?bM|UmUF(uOs!~v9Syi?ApM$`M)*Oi0=7`KW~~D&w=8Tw zCNt~Rg0)|kS^w36ZHHE?RXZ9TNdlyfti|%Bs*y1G1b7BxoRzCo;eeP7^WkWyenJ9( zcuYTr!lVd{+|kEuo-}dNw#-!dBNEF$nU-%(YC6V(^Z5l{jP&H%mtDp&-8^}6iFuOq zld_jg5ZjPdtm)ErdCHVY**rNGCQq3nD;FuGC(}!5g-KJV%$~iK(DpV@7(2ne6OueW zl!M0Ee#v^@&GX|YP8^qHh4Fy|CWgX}bB-2n9zXHs@i}{^<-N`mZ=RGuVp2jE000l$ zU{I_)dE%tW6DLoaIN5QV`byOAPEKmX_$gxK=utz44TbjZJ$wG}l=Dhm)~&Gjyv)LP z3f6tK->u*6Kjwk|ys$D_#;A-aKaU8mP!1f077|-PqG9m*&4Og=l*v=4?tY#!b!w8Q zO_`eHY15|cMxL6~af}7Gp*&9dw=MoXu`KlU^!bm@?(&kSE7XFUiO3F{){gj``+1 zY04fbw+IY^qNlli+T>}J9@Xv4t~DN&OPf=sO_xI<>MoS`AV;0LMW-Z*`uJSyV!?Us z`VGsRTccFd>oOCc%q;sbv+kQxw|;lP+RyhttCCnkYslp=43kljGVteMki&4dTXP)HH7Q+O1IjlqyD_a{aL>E3<% z9P`I=`&DmWaKq}%+&40}e4bhVEnC=s!#8DWcc~Xvuh+-jRm<>f4!dWA?J+SBEm&O( zw?=Ugi8;_LF`DKKAcU&`mAN6$-IF{oTY2_8xSE&bq%d#Z>>cMh^XBbMo{+3_qa4AoqpJx^u=*JcKd2^W`-z&OvUYgP(nZ1;fj1J(COVy2OtaV^U)_-$ik+d# zsNC%;tF{vr=U>X#bv&PUWDIPUkIL81wvv3wI|kP;HT#ixw#7nu8)_q!V%rC@`J2@8bv{IESJIa=g`Imew@l5ga z@veCOUq5;lkk?36Yi&6$A>$?TytG1v@)uuh_o?jp7yRVxOAA{KwvU=bM_)69m`igF z>%RQn(Wm_3gcCea@HU0LQz6orT(YYnMDkXHja&~D?AUnLaN6mopHsH1?OzW^D(cDe zLcJynSjRm`)xwrR$kpUnjOG9CWA*A)x8-Vi<^1y{l&e**9q>`dd& z9TkQNP>02AeJ2mLV;?r{L*U9g%iMI)oIElcn0WA;2CI`iFNim4B=(g&k5CRd_dJlJ zkJ*_xNZTL-2M^NJfSU#l^jI!Db{d@I(6wxVLEAT~80;1 z_`(Y>JpcUj%FjIW%!3a;xPJZmg$ox>nKFfSwC~Wt^C?dNJsqfCv*uMjdvZNQG0cXx z9qHUgV!ET4EsPm6a<**whc6Q&!6vHplrqqogEnXHyWov~xn9CrodN9FAK zT!Yzu?EoY<%VX9Ec#*qAVds}EM646Wa>b<-J~_Wh?HWO7vsY)$fV7<=Vlde zO`S9z;(ucnz!;>v+-p2MwU!qzUAiqVTe?^dXO=C==A{bZGEYumoHDUf;vmJ zsUwR8_SL6vzh9qldg(gdG9&NLto*bP{*&m)dcpEJhDisEeW+T)W)D2@z&r1}^OwBz z-h1yk^X8jxZrZeI$1ob+ojeJAZ-T zVd;wH%T}&jzI%Da%9Zl+m8-VZU$Mem%pwFZAwZ%y>RarLxyP1QtXj2QUcG9CylS;} zmaSO1ENMaIY$QXrgtmOS^V@n^wQ9LWR;S{<_Up>WGB@=7N|4&|Na z5hJ!5{O;x$0VdHPtj1XjBVi9#VFswIvTYU#O>JL%*}V3^V)u0Sgq zj=K^|ne7bN3oNt2-r^;?n73eI+BlSIR#VaHbK8bt$JOIDJ1{C}YS!#IZgHdA*+K%* zrHc|<0A_0w%Vhh>#^XkutewD7W5&b>f63F6F-{sh#}s$bnaR{?(+ytR^EI5<0`=uk zy&R%qdD-$6NnY-_RCz@Sd3iV)Fd%EnsF}jdh4xd z6kNs=PdstoefL3VpFVv~JN-1TQk-|*`JR6hxM71FtQLh<<<19qvx^lgSUvpD zv+zmwYNGI&1Nmq!w|EIVO(d*~IJ}7J)oX59y=Kko zTh=56<>cdFCc?z}K+NHrUW)_h8Z~O#)7M$QETm{-nv1$d-Z{H*VrZi}>OP1raqZ2TW!$yiP|%7LCX@De5Bu>LvlAwzwyA zW1|In@kS>R;a;{whVG4!UcX_(hFfpli+t;Cw-z1Q_?a3N!HI zT}q}A-_`UsrcMFLn3<{u8#Zh>X_Ch*)Hsa*Z&^c46t!DPJ!(aD0s*cHX?SRC@Fa8W z$)^^y8J7jNr8jTWZoh6*GBaKlDTN0Ot3N&D!rG5N{y6?;0y%34 ze|=07(}+g#)mL9}6JjcXlcmJ7&pvCpA&|rszd7tM$^u2B0@nm-33EmkO_+22`VAp( z*p@M>*aM{50;j;TsEn7X-=KjuNYb0SwtQWpAhUzq^&4*0f-cOR#8PiEcI;U1-H9LS z)U7*u^k~kD5^W1x>3HLg289#e==0VV-AUfQ(;WcccH8Z@-F|y0+#U*hl(B(y+wFJw zFQhx}AiBh*CkIYk%DVEj5S)8{QUw_b=PbtrHi9BAVk6`PkRVwT!Ey>P=7Fc>oB9|@ zM|L*6fu=li(@#I`9X4dELEqZI)!y6lswRJ7w*xs~>_47&p1}70 z`|oF8tmfW(?Pi{Q1yB4}J5^H$o2m!jhAGjhD2|*DIaAdH_q#7)C%*$!R@! zg{*@l#ab9{`HJNh;x-l7JI80%DEg;I{XsZXYS{+|t^VZK^*U1hpa1-4vCuvD+;e!< zrkSC4W){3%u;E+li2Zuc_FgyC0el>J@nbX!6^z^*S;VKGe)|6V?~@pFFgsq1u>&5L zjh_t?(?*`$Y zUwsw4A(M72=xlY+0#pl`gs&1(QUS&+A5boEdP5%VYYn9ng&@a~bpSl}M%p&s)nqKf zCrhiiWQ<+tMf(yh^T@ykw=cc)k}XH9NQPh!x;9cUFk3(}8iz5nc=z3RBZTPD8dI75 zYCN8dM~DhUbnC4OTn4>xA3#X^xX`Q?{^ zJb$#s%c(_>2Gce>4#E_;xd$J52y_vJU`cq%*C#BCCU=q0sSpD;fPpy+Tm>^8aeFnF z)@YW|kvz0>Lv%p}d;wcXCcaE)1fdwOOHa!=?_iuw?u4%P)l%_>(z^Y&rC(DNJRiA|kfHm6$At zh+!fKL7hyj2`rI?j(C`qkPIXjQMC5i8p$&v(xYo#Af;ICl~-O7UC4oWw$ox?1q^nACF21$P_&;4KSg?{3lK6eyvKzhe{Oomi95bS1@wr1Evy9z7DR$f z5TasJW((h|)3tEHTL&(A_wb9V^BOFU+?Y9wc=XXne_yq6!SyS>s#j{mw;nY7s!=!I z+3<=Cti+GeA_zx=h`U%2Dia>udtW$&bl-hxwrRGfssF02Xez{lN%ALKil4VXqCU~E%@X_f*i5J+ z>V_`VZD7V?@G!r5^Jde5bDXJ6n*x|3ew=W`QO>I%K4kLlHCeXA4e*$5ft^z;1&?%`}zgQHw%FDo8TEBQ9@ZPdzMb1-h zv{|0TAXzTS&@632k#yHxci!cg{0)V?o=~d7=JQ{jS@;<-p9;7r2J~Zu{39+yRyMJC6!2sd`ZnTIAxINmeaf*usA%tL>_!iz=bw*TaiP-YTUSSY}1!mVL)hM;bUOvOV9!v#!ujY9VyyJ zfEU=4hGh{tGEKsa8YDWTXLh`{*c<%VJJ;!(yj3b!5gOr*NZo9K5xLhGJP>0w#=|OI z)dkN&vj9)>{IUksk6pNh<3>If=KQQMTLB>57)PkGWbiCkv+PARXSg=#RZvr#0I966 z)GD>CEG8rY`yRlP#Sak0ybVX8gj4}W*QNk|ZXrqQakeS=hHX zm=jMv{J0YjJ*Dh1mFiBJJ_C~!K^icHocy`({`*0@Q1rwo1RN0_@ealA1_;AVT zPY*vFbN=PrnLj=Jr=sbR2Oij(M_R!z#*c}EjDc8miQznJp+r16efo4!xAh}nQ=%~g z56z}Cio+%lCt(Q#qXo-YAmreH%8cL#BhijPk!3pLE;=Fv1(OBobY)IF((Z zIBL}n6^mi$+AYE`x8x_966T>eF$N*9V(PQdE&`$W(Rr*IZGjk7#wr|^eS>SR>1R^L z$%PFVJXFj~`##3L$i!fx4U*x0&2I=#;?{hz|1F>Ws7den3l_4PD8T%&#j^8mCj>2+ z8cS!s8#W``ZVm7L@UT~6BK`ypq%%%{_ z@CJBxX2euLWf-EzM<0DG9#G0Cf;m`IGauZQHm*Q!sD?cf7YXAW0u(uW8x7a-^19HnX3={zE^MnzwjTzs)d z8EIQ<<4!!O zYmZ*$#R3AzN=nZ2M{FD_c(ggQXZyVqnqmcff$_(YJCYSuqBW5NOTk@K21pi1G^nPe z9cNS~RXEq6vi5lp17@wXmSTXK_+k@u211ZE1ZLBdPdxFY|H2{Uy!6CVPs$-Z_0*F| zA^Z`qHgQvkbRLP2W^ zyFk_shRwTm?AXcT!lMC5Bb*p@AgffB?y^QOcYCL9;WhFtg{;J!41rg|kjTNVSXjh~ zI+iULunDfvtSD3+gUL_03#89J`)pmujbGN0UxASAFVO|?YZ(|MJi#e2E)%g=#_p`5 zEY%TR{=LSp8fipIN&vJSihzf>B6!xU3AVuGHS5vd`0?Wbm2##0HuKBO1ZJXZXmw@J z!d8Q<8p4By6(9ccqMGP5N+~u{+!2r){EJ~&y`$YQ&wN|=?DwOJO$s`UIkaxaa+ftC zpR2Cze|)7nrH(tN@YM1R+H_>0{8R#A?rLrmV#k#+3NO?cAdCYy~Rd&=#B>EhG=vFl>-Qh&V{oCuc|I3~X4J*ANU? z-qO27>6WX=+@8N>xTphBP_gOY)z}C%fvA=(tiTe+kA_*g2d`{*ZF|;EkJ~~;& ztgwQcFkvEJqf=3kb%zGEV@L|_MNPJ(2-cFEf)qqhu~YEo0@b84ZkKo9XTjjH#~y#= zkw?v%e{ml3>zy>{g@cIqI_et5v3IJmoAl5_59fGl+h46Q2NN-7bN8^HUjP!k;k@DP z#1M4hDv~iB((wHC=QUerhTc(d%NDyy2R81*zu?d4Rt$_tqazRqMH4`z5=S7;q=Fhv zTKuBXRfRq06wcjLdf{6?Z+&CUD>|K8rOr=IKDXeMa-|ygV-B5q^(7Mw3OAhf(++un zbK;379v45^qNHMTHxZF=+?QrKW~XZ-S9YGX@tYq^^o1OXK##T#EbfZW8h9EtYOI3I z8Q)k1Boha!{F=Oxf9`oX{1rzmy%!n{5C=w3GA$EdW_r|+o zBX&gq5(5)2fI$n-t1ok>Uqga(;SglHDj*TV&z?1#eu;%IR-X%oc~KU<00U!z6OCJ0 z3rJWxf5b3#VHRNH68s=c(uPU?jx9vb`SepyKlRkprgQH-_pDmATJ&XO7KiZ87(O)z zXQyJ6^e8`%%8~ zR{GX&518>njoNi=;Gm1t1XwHZ6Q&SF;>8g7jl)JBUAg`Nm-jCid4J*J_e#y%e9(<6 z55Kg|g%v9Q^sI{uFKSrOV&Hy_`u_5m6Mp*Z!&`RlX4c?e3_#%o;F%F&LIu+%85`i9 zvmkRar%>`_xkgT^6e{uZ77H{*3mDHO)#B6~ksfQZ{KRs&1>RWMlEsUwRIZFFF&hht z4XVs9m@m1xixa^(XnjKxlFhjuFnNXQ=^ z=(-da0*4)Tm>Z2PUmYrg5KU1;KFLI--*(&WEJ(noBNMcI5ZL6muZ+_Wp!!AdqcxzS zGPZy^^eaX(HM{OaN^X(#9P?9=#$CdYan)e6?pL+jWF|abxO&U}Yd=4>S|e=&KdT2q zJ9q+Dt`<~uN=lctYJWtnR{J%(zF^#=h08xIHRI*|JB~TJVaF5CIQxVejSsGSWns%f znVz!>s&)9yxtAPOxj~sz%YX0qGq3J<4LiIF5 zokuTxv46GoWthh#S~et}h!=LF@D=PaB;;sWV#o$>7+T`e=l73=UC0$OIp0ik!g ze&L1i1sMk<2vL;I4GDKW9wN*b4aR$QM}uaqWi#b6wsBP8rx!KE954%8wt2FC{d$AJ z52j{6A&$Y3VH$Hx%4{UIACIvD5g`r8soS_|L7xSlyY08(>mOVhUWzj+F2E&xqB|Sq zU3&F7?vffmsMoEq$IOB;4;L)^YpI#9l&;q9_ZL(uU$?Q1(}U}ED_yREUl=GD^*~|s z0fiSfIIz_~$1-PDJoLCzyj(#5C1T8mo=bSrWQoN@pcjYWNhmd7gQPw{waW+FRHFi` zF>y|WzW5Y#r~T-)O%{{bKNN&x38YGEcinZTkAeqyW$F3EMW~QQ7j(*GxQV>P>{>Wy7&h(mm8Hx&Ofx5BA`iT6b~6eMg@)RvAOnE*49Gk^OG@^`-tczKe@oSe|~QfloNWk%m$4iR_rj_+Z~K$-bxSqsm$`Pi zpBl?7dbeQFyQQvQURb`#foE0ps{+15T6p=5rRsDm7;;C!ijVyeS?R0imF_)v|4W)2 zSF>5UD%E{;8p)e!iD1%^nGp*}6AO*-J`@cVDMN1AvL&k9u5yQs_|Gb zN8v7_vlui5(sM8Tc(zN$<_jnGmpJZN-$#09Ak>W*jT4*gS=yYf&U%^$tr&2}m%EEjTgII%CLf!WeAr~rO6Yln)ZMCa*<7?}wbTY9*KzQi`#0f30)v;`Q( z5=<;GQeM)C2BQr5N-{x!+?lEA5e!VhAlV}mCLcaaQ_`T}BR9SAhPg=kVh9{4JdhRr zRcO?rpzEZ9r626K=$#|#cNB88>(KGkORF7HyJPq^(Df_AYu>Y7%`E?@K<>XPbLEtb z-xC;mSLWuY3p}Bi_h#YBPfL4*J^qn0eHZT6q~Bo`>NaWL#i%%Luf|b_rOq3h3?b$` ztZmx#+G~FX3;_e{#x{JS1t7;tT&X}}!Qfp&R-y%*My(VYFPSxsBX-__OmLK?gl-(E zjG?(i;38zMM`G`3Qx~=tFPkth;ovnF&HTGo4E;0000}wDtx3 z6pa|sK2q}HtOgMdGT{mdgaP7hQl$d7V;+L#3t)pI2Bad5$B*QUi%5*O<%lictjF`E z8mqB8i*8{Uw?z@u@%rnV`~zz1E88ae;@y19z<8o(&+m@P^kU@Vr99AtqDDwln>rP1 z5r{=cqehL@BtQuyfE?GEi$r>pX3T2Y^O{o|ww*k4j=o6AWSNQOKr{qx`WDPjDmN<_ z^{66 z{!8BVJ&5nM9`vi`ea@_M8J%J<;&mh?5s3dM28Mwd|0Q^de`4`X^D<5g3an*^(E5uB z=LLsXH8RzQ}0O;MuEc}C~#zBsjJ@&Q9fOd++5AHP%j3icf z6uTsw1Vs<;zWYv+)4x8JoYsA{HLw>8PyWk!!|aLh@%7hUe?9!A%4eSqFRlQLU2*ho z)kFI!qTn!!0-3;)yWGMY?8N-(kw@$uVN`TYdQk!@>zM>ZBkIR>C=%ctF&?mKf8oLf zL?_e~l_6WPIv~Z{wL^4fZh=5z+94~TidWMklIN8yE9_3#Wx`qvIc{-N4PvnzvvR?} zZUF-u+5nH+O*42-zVnSYOy8)Os!OOq6vQMU)&M9jH41Fca%tbn+N2`)#G^(`mNX2N z-xobHZ7U1hVhj-m#&0m{^B_HX^**FR(}Ldf3R(~C+`FIKxlvZ20szDz8Le;O{p#Eb z1=lXa{JtEWnf-dkw?SuZ1o-fs&|n4Mrav9v`=Lyr)Q0bTUG4xov2$PBui@3l)M?fH zsy^JMVZ)?Eux84M8zGj~OHp{`J{t*p6i0gk&31Tdrb;Y4HPenNu!LtO8qpxB>ImAg zqQ((gs-{@rKB$RZy#4k&tnZaqHVTvOyYF7H5XfNz)dWSv73&kSH=j9i`n3ijoV@gx z_uiA=`^#VCBxQ@akJ+=^{>+)Pm0)+BHot)%*(v0MD_;zO1cl!SCY=#f&?84?>kNuE zy}^yX_R7ZavZ(zck(Jx+6>z~I4dFb9YOFw@NCm*)Bq<>cwu6D$njKW%iHl(mX+Wb} z)UoD(79k{cru|fTiypH2&eATyeum6>9do@h0y~ znH?VE;)&6bG_1SfIqsEVzin{ID;cethD77kYVm@)<5CVVC@4jRHUXI8RKP!GjWn3H z^JyJjQ_2lD+(4@EujRMjdfQ~5c_zG{vtYq|4p(@NYl`M*u$%20 zxI~*w*cOweu0TZ{{!s+ZE-a>i7PO!g4kKOos2V<30sK_9JRun%7luXGKz$MUx{kKs z$jUSbEuL!=nxMkgE(eV_Y)yohpAg5Ayntu(8=JAvpZ_cr z3ASK~$}f;=bT`zRw$HfuBOYIzMt%87uq}NU|Ln$5oB8yWSAI7w!-KN1ArRv?HUl6i zUWxgmS=ftXi(!ueoBQ(R%MvJM+hG*uCA8r0u!mKW)foc8|IIFM2DdY2%w!1UFn#(A z5xb=pq9DKvFAV<^4Y4Z$rV@w+=AbV}VUGrl$`?s=K~o-JU3$qSwok1K#4Wz7X9EYK zNX!64N@l@Rut!Z%pL>}(Sn?eV3NnMb`PcV6t9q9$*c7!L%B{036>Ne@Q8oFaG6gfz zh^Vg!gvik9g8=yg{>TIkAmTw^lRa`lE+Uw_|j@CHy=FR1F z3{nfl(x;z%n$6q(isVX8TSGjx!x+lEvk5AA2jnc6M2|)YpCAN3ar#l1?818CoO8~; z`|i5}F-ACH*LT7zTO5inMKW`7qiszdM07+l_J~LfiiBQx8&Ek5uS$wlkgzWD@uoW? z0b>jUP7;S=;l>kjJ-%MI3$f*S~&96aa6KBJ&3y zdXRoK!q*TI$uI;Cmq#e%5pLR$nf2$)EnkG#X71|;S8rX|KrA#fypQLX^juo{L2fya z{XML&n2J80neKk9+)+ACXM?_S6uL>lOkl36(l&aV)%(ZC5Bk0=3X9?(#?)Q zh)y-I^rjYZ5x#u}_;=(np@q5m7;TN3Vn=j}*ii~jfNI1=$ZSD_jDm3?XSbs%k&@U3 zDOszkMmq?EvN&AJ5MF~kBX^}!U}D-7w7>UmFlB}y!UJH|blYvW)1y6&OD?^HBhy-H zKI!ci_!>w1#P#vV-UtmuNu$y~D}pndh< z{5Y6UzrZSB-Wy)S%S?SfGykoy5wzxuWI2#s6o)#ABZvitaG-VZ$h!}z)uC*|D{dTk z6IQ`skY=>Fw8(tZkt>ih7#k08GEfO1i{+Yh?|BbnV);diCllh9p2*;H>d2(f|+I#Afj> zV2DoHjO;!7eR&OX6UEZ!@r*y8Z~0u#{>hcXschOZe|~uDpm-`CWpR>;G}r| zJc|(2r=Ny*BSdc@D7;iOI9}mi~&Q3pINEq{*C)4KPLQQG=v=0e;dHhG(s%& zP5AY^+;6J_e^?PG%Ypq?eNyNLCzCI`mFY10{HnG5^fyw~BXOnSN*wKuj%EZGVEc_XR(Am7W62k%2MC$V8(*nc!-t_+ins|As?%Lazh~45)C?o z&jmI}0L@|mPQ+dK%17G>_`FHu@cN36e&fh0Rh=l_c+{6&O~DVgQ3r-q5Vl#l8x^|N z2!TVUVL-CGsDpn&jG}!HI0R!BCQK6VA^g}ZNs>Qli=*gTvyx%6Q=1i%J;8aBc0&ufX2=O|3P~ zapy;KH%{hnUZf?C4bV82pQwSHb<;+2%%pyRdzHxp)ZqU zS?Gu^AOQxPhaA#SM~Fr8abVWY6vu1$EpQPEAH)%eB1Ekm0fexxbPC`^9hR#I%7z2w zvW{E|WoMG~jZ*j!z9o|sVkw;YCgdb3#W8P-szNCZpo$iZoQ2T7*i7%J0IM@hgAA%q zUQ|G(ZTj)rWk8$H}%p#TamfYWz-u0_<@p znl)5%i=uXtrYJ%@Gk@NE4xJdm71OdEzi?tyq(_4h3}&KbFB;)|#yfXTc%_Or!O1ZM zjS@Bmv$O2Ivg?(8)4;Bm28{|Yu^mIwDL>`BVHgtC8+N=`Q%Ig);}Fd~$5CJ83SVlR zcfeqQ?w);?aHtsZ{_>0q3%g7TYk@hNl1F3V=jBVx(F($Hz`Lnywj8)%^TAh6{8fee zwOX{Z9KfhQZgO?O{5Q+2{_LoDOIV{wGLJhBCX;uqKgd7gao$vm=U0tC`K5@sfJ zL3c!C!ASeepB&7c*zvh}p|W(Bg9@TuU`y=%Sa=WqF#rz#d#}EI<9Ku$Jr_sr`4>}C`Zh#T4o-z)@+q7tlEP07e*0~k zHeQNLLni9VPg_3!;`5M`LnwOm`T3SlKl}9Ux8EWS_6X;I4PB;?hDoW+IZ!Af1WPa? zf?H2yV2RdHuHQmODX@xTfm!1u#)%+jqCn|Z0oJx|G}D(}dWp>WU#voUjEHc8yQ&3c_zEj%ekJFv&15!6`~t0zCW_xT~+CutV(nqB@l zcU~qg!_%56gi)0(E3*CatMGEv-#!ap@fJ05nn*0RBNnAo)?o>TTSy?4WZDsk3XLcZ zR9M;cY1056lkmuxN;QMTGu%1=a7I=fFn}|?7-UAzJQKDo1h=Tnjh2~sCT*Oq>B7Al zN2=SUzkioRE4gzb7oS6R#zdv~Cpjjk1iW8omVVyrW~%Vf6tsnFGGh6@&D=- z3vnDpYHH2zeOZW62g4y}ZL%`eI98%INE7DejZ?p(DKDA3OZ~WVXC}BvFM_40L@0gr z)mP!ikrUA@{9CdKirBlcnQxI{2zZ#h;R&mFG+*Adiu~-;&jJ^P9RFmwiimh!i}H=H zY(yP?Yqn0Ex(u_<<*oK_IUqCRm9Qwt*#Z-gx5YxYe|O-VSN%T55!ITu>E2tI4Pz(~ zEfBRMthQ?3sj$<8!lm!~O@OL(8xSuE6dCYNK0*n%g?8{QbWw!$uNjaMz64!wV7SVJ zi4Nfq5iT+e^WrsBE8aM>vN3`%@u9aV?!W&5&9-gV){oCHWWy$!7)7`j%ZaODM*A@+c91cJZ;3@2k+-VMXP6FD0Op21)UAFUZ@ zAc)+ys=?5{8_mwD$L!pQxY&qGstJ9~)JegE$XQKR!ymz48y7PfN)>%Tl;_A3YXoqKrQ_7!W^@e{

4*}UwA+19WS zW@ScB!Y&abbBh&Nis;woCzDLuC+gUykr-Gkg9^XwUPgh4z#9obI}BT5r1$EGRLuxy z`c+0$@97f$Y16um#Ra1igiwk$*_}zLsVod&n3~lZGK|vL(b1#C@HDY#3a9@oTnJD! z(>+Z_Vvab(fZ3+OaC5>-$0&jHX#9krCt?H}9i^8PNH_|RA=qCPFz|zD*0WB5} zKd{%V9{sM>QGVt8c@-%eHf5O?wn4=Lq}N`5J)BpKs#)3+61I+{WP zl;vYA=gW}lnFB3A3rV6Q-W=Mw(Wpwjh=Qoz0DOB(7(g|=M(tX)*^5N%p0^@fjT?-< zVyCtsJYD5s^zT=b)t+rLjoq+1#@jv?v32W9?bjcFNi)h}A137+y>X(UAk!uj%Ls6NTu}7>Olmv8c|SG)SgO)YlIl7M@Kney_~5 z=TEM81v#@Fotj!Yui^z)jmr3mg5vNJ=Ktc0@I@y{`&uA&L@oH_Pq8$LGX+rr2Vt9< z&$S26KNe}+?X-;0*cXq?mS@bE!QCSeubGKy30zXpcJeV`BSDZbj{W@qCqNPWjrzs z+PDz#+o|(5#jv46yL9P_lWWzgY49q;TWVslL9%5^@zI90A-%`*w5cY$Fh}}gWmwBc zp`AMJDxNIRWx9%&%!`8q8&m7hrRxuBw8;#)(>p~T8QUZM-tzZ%J*jiAacPF7p%jWB}lO zTDGG1z#+Cdgg4|C`9TGfwbAsu-~EnVK$k5|b2MeMUK#az-R3*kf)uUrh=%PFGn(e8k`?0}<}B0^Ok|a}-}0VTps^(MeM_47SXC znE45pR4?c?=lg92A6C72*{YY>e&U-xb|)+5)jS15Oa=K^U&KJI8&Sep;u*f*ak!JD7zffWS#e!^?|5 zqcqqu4skDFkSzkI4rg@7gAkhMN_G^zgJiXC%67SOK&WPCQ zTH>QUbg<4sU+jY2(Hfi57n$IHn?JfBJp&M`6|t|_4vRp82n{b8293B&z|S6?L62JS zl7_u0mI50F3HDr@Hu)MF5$ifih*SUrgy~RsvtT3u<6fEtfbRXS`^70`FRfIWrJJK+ z@2PRc2F(kqH*MNe&2)@eFOchQ@Fy=TyHcpesGO$`>p@OLnJT$pD-NR--!e4!njl@P z&#j0$xC1ujWQpSaH#|wyXzG7@z-WzZ|Mlt9>CTY=Z2v7C8EQlYG)Aig3=yWd+EUS_ zy_gG3vRS5NQE>M?_x$j}+L@bf_45~=-}zs>9kBY-pEe6WuNUJMU8qbuf;3BJv6E=i zjt#orQ)R`8aPC0M3^6=v(wgWozPn|D0L&%DNyYjkDLnSODB#Tt8yJ5i%5pS9;P_(jG`4q9VrmLPYfFO%HU66#;t>8cM2a z4Q#kwJ_~Zruiv7u>!k2~pLJi`sy%S^$A>oU6>ULHoeG3N*8@|FU!e7?7ap;>eNX#U zKTbsN%!+Y|wYUb}lSNp~SunDWXoG3vZqc<6h+#R|&ju=YufS_yZ&*qPB&_m;%_Lx6By=&R7skN0zfC=DmYp$Ib zwOb?=cp|})m7~Z96}Zbsqq1dZOGH!RE2~R88|pA~#v&x!Nz3^I+gJX7NCFWLNR>J8 z4mKPt9PBm-v!TmGRM?&As0LJVdkT0u_U;$HH0`UPtG5JOSor2K_1gS1_UAac6WN&c^#H0^iqd^5xDMVxMk#qB=`}ku_McEwqRG;c{>S~-l8MI;cEz| zh=1Anc=lcWe^CNh4y!}5#jhB^OAhu7twt@YnF&-#pe4m}iF4FQjJUX%88c`8sCN6z z@O%AS3|ly8;pSuNwe_^owt(OS&0<|$Fgng???R&z$FSEYjl4M3vrro^abL^aiD<9e z-{p;YKj^Z{y89~p&q@Fis5^og9|{<3^$QZ11~9}+uEGQBP?=E_8IUus3U$=C3OKz^ z^YB}F3*QZXVa~==Yd7O-xC7`Ccv!dEmh@Ede-vOvM!4_eJ_+om1b`XHfs=y}&x@Qr zuu2V_dE{@a1mpa0qMr}-*@Z{m0n&f0UIJNRGL?7r^yS^}w$ zupsqZ$^MKN6j8cw7T5x1Al^dZJJ>~P2(jRd1+r)H-Yu+Hvv#Rw{R>xoeBksKetl|L z4+_0#!F=|5RQqn+CxQPf2}Fw!$q0%d-(IU{)3z3EIobm9s=qyaHsZah_*#e~_u;B% zRpKjOsXp@JnuQaeJYdSx7u9XTr|feF|5vKq*YAI$1fpFCpU}RS+3YlW{Ax|%jX3X( zq=^TSER)4Z9%Bt0GW6iSa}H_Izen$@?Q^>Qzi~+Wn%XCUe-jC?4y$I%iXzFfI>o%V zdd~0C|HctxY|mO{xaHqu@cV|bPXhn@5?~H?S*<(v?Hc{>3x8ja|5g%+!PP#GuuG-< Ee~RW6xCMc{4qC!hJLE@AqB1BpPsg?t6T;%XO-D?b=nfek-r7iL-wG zTaWse$#x+BSzeE)i2r*%iR$>%UFX%0u8pS7%hu!3=`a7f{^$O9X!{dq)a4JKyZ?)w z`}^)#?h&uY>wVpR z)I2hU?)K2&_vd_n=Pyp`FCTR8^3ZsHx#ORY-oNC*tK5Ts?!?~xe)nI-U+C1`OvJq`-60k)76*%2`svg{shh5zuLXk1KS6#z5f7zzP(JvZ4X=` zyWRJDc=wS`kN$k{UXPypKmGpydadgB!0gnx?!~;=u8FHp+Wo;E?d;B^yX%9$x`+Ac zxjsDB!(;sPRX=^EdjvJ%`(z*NJnwUGiADv}}%%p2F?mX4c(HSzCNB^l?jLDS#a>;{tynpL4T`%PRwf86B9?8s5 z^Ss}`yPx~^|M!-eh;D zzwm!~KzAthe)#D=+F$7EzQzYns7t!Olou0DThs7I(J zD=RBAGjrs~k!d}9Dg+4SD@FmT(o<7n(uYA5gs_Vfh&Ldss}pCEjF(2RNM9IJ2%Lw z&JF9zcp1gwvfHHgw&Ko}EHaZu`-7azIYer*p%WEnBuf z_Sn{)k8!ZRwysaV{@q#;n8?Y=8JC+2cj(chhcnl5s!d8r7Qb+)h3beb%y*)m&fYmsBO*#0If&}jMU9e;z*-{=Gy{lTU{s6|?!B^YQ41Q!Ln zTC+dc>~~Lv7CXU3cA(h~H2G*8cTaIGBQTDy!IoC<*b6PI)?{nfnKZM`vg&QK(YBf# zv)Sh?wrIX3HqG7d+AgwviyeE3G;6VKw3uCOk!3Ent);fT%u&sTm8Mm08l|Q&%dm=1kov(X2AVsxYkC(zGhgTj@1RWHf!Iq0P{}#eZ)4G*d6uwP~6@ zRi_z~HMg0CrrV4HJnJEq)sg6=l6K<6rFniZI7 z2TT2dQpcJp4{J^{d1_7>1-hQEX%oEcSQAZ-`)1mK5}$vT-#61|Pq%qdyg)Ob@p)s0 zG2JjrEW5&SDt*2x$1JmqS*E<7>4tZPuFW)z63Z;LIVJR2<2bcGq1Z~>DX}e{qR7Wb zaleI8Wbh7Uz!|2rS+-r~_$qvXIsVYRK*aoDpxSn-ET_V1Y-~(QO6u0Irc9YKapJ@w zLkD9?K>ogJUtpdkJF{(?I$}EuEE}o>4b7&n+4i?Mj-oin#Y7`$u>JK;5YldOLW_J6 zi~W&HgAuBQmIP>lB_UuT_-OWn1G)hSdP-C1ku9(Q9{{4k=hO=V*gADKfPg&B`4-Kt zvF(KpK=C#Cn1*k$Y-vFA(bnt}&@|dcgXK1XQM2W15e(S?XsP40+CJ6%%Pf1TX)U%u z595G2Yk_4|o91jypRJo!I%sezO<%d`D>3aEhBIB44_;*QJ^2EB`K~s}awggagfm&e zrO4ug@I7s=D%Ammj8kPG}*4Dv<9vP3|J&Pttu8O{+kf zP*DD#2~)_k3nIEpyxl(yE6*^;>*hG!C38JP)5qvEW2_COGiI>ZjF{;U&h-1IS>`0K zm8+SVhLK?yW9j2HvP?7Al6y_oAXu8vwF`>^ZmK(le%~~|Rcc#Rwlh26RQRnD2LhD$ zGs$AA%uOLxH}g4So6~eK;V<*koD#>L&eQQ|mR)Gug_ew8EUybXbs+O9i+3(>k8Z^l zTWPZ_Uzu=g0O6|(_-X^exdDH}OChEWMOG600S#5Si(9M2EUR7f*U0j1Rsn2 z(w6wxG8AYKVN>Vlc22D$C~W>0e^ zfo>}S!1v_ci!hj^Td+|@uy8$nvc<>Y1H#m_GTkWCOj@~MOUA4)%re6$)kWl#Xvhgz z;RB7IE=qJ`nwP=#NgC9ra9|qwrk)Sy{ZX4}+!t56+Gnd37~Tn%mSgIW`Doo5t(#+z z0fv#S>*GzRo5%4L+y3H!uPES5wv7Uho}-&%O=FBnH((@~7;hSdUVW0+oTv%ly0OR( zRP0O^VuW}_nV1Lr_9Fw38O{^|t^vaGG;j(2tO7dQg7R6mufk7r%6$kG!Ng?s>J%@5 z2k>^|QvweZ8V6Y;9*g#abG%p%8YE+kD;mOa-8!}{wD>cA`JqOv~Sk=NM zK?6`ki-2_s%`SGFr9RN$0zw1^8V*zp>xauP^#g)%dqm{&a8yT_ZFpHIv^W%O3PJ2p zE>NI9JBWn9A|K5K1UoWRgU?s*cNPK&e*mhl@df7laAq}LiotsD`(tI;%@WNWur#v;pJWLnLp(PHRLCW8Y( z&|u9oY?P(h8oG^HY1!qb4jN$DMF!;KOz|R=Ez}sK^AuQ~@FV1e9Gs+Ug*s%&jy}bp zKi>q20w#=EHncC8D6>>kJ4$i@3jk-7d*}c+^p+Uh4*ao$`r#=yL7otJz6J;^woq1{ zp-m7SNy`;Eq31~hF{o`20=M>{!c7rH3qY6!7Tr8KhMuJX3oXO2vUKRwy@aU>aHsi$ z#Wso)dXYZCKvM!SK*5t`d9rnTyzW5J>PB+Ad>x4B6TDD5KSggWw&2m|7XBJP`UJFw z2Ib5^0*Q2?n@A~IzUW={REJa0JC}sYMN}9wEiXKMqDU0!UucM;I9;a?($tu%(cmSb zkIj&GLgPh+Dtw`8f2bxTfT*?u6+YiA3%JM^Q4P%#KyZWxR=@;66k*1&f;JHth%~$0 zC;Me~ zIS_TIEfBdPBD^vbTonwj4n?kvj9wl_eS-XbO@0Ik;zTS4faOAAvENmkI4CThp&A0N zga8nRhzcQaYzRREQVIkh8yfvKj2?QgGdbdFR`jPQGSto2wV4#;0oU5ZRpW+{gc*B^LvPB`O@Cha+3xUhbfhPS6MW_UdY^WP`2u2^Q z1QW;!OL)47AGr&n2+^p`^dL%6R$x)uTwO00IVutbJ+stuP?NZPjz3uK7fWI`nwSq` z!Vy^_(!y=>{%3YcU({DPlh_mJU9&8N3YYjR14uQISe5pqNt1f_?%j=vapT4%_2{8~ zd65v<6JlL25RIOv6#D>cV5uJ*2stmno9 z27@aCfwo|1aYRH*IAT#Sq9x=uF&ge05XyQG(*Z8;HfSFLM^w~X!G(dq!Z0`pH--=f zA{Eel>IExyt!P&MTF0yvP`W>{|Kaj%{R^zX0!Q#vXZjVS#MWV|%IL8gb;KSxL%QfS z7Az)n-SA)BcEqdQNrt;r6@vFrFCd{{UdGC+vE#MDu};8W!`x-i-l`%n{K7ztsO>vWJ0SYqkZ zZ5nrhQW(%P*$XC+3NTzJR|5)mw$Q1OV~R!u{ldEoO&cA5isG=q7mWxS%rm$OOwa-P zMdRWi05T>BjXUFXUycK{@?0oR;TeIz44-d`BZfkbX=gg3Uu9Y%GjbV6F?vj8cJfVs zz6IJ~2E2{XOgpMF7`Y%6*&K{)5d}L`6F?)fF`lP7ydQ2#<0=sY6CGp{^cYd;kDTp~ ztO_Af{L>wIb)21GBh`3hUL9`(-VLexU{P3LWd{6-7DON+5@;?hSNW9+6!4XWd?f+2 zBWH%A2;UZUuEg<`s)qS8!}9Y%XIP>rP8ah&P#zGCYc7H-t`CIj#We&(z)EO&C~|c;rZXy*wk|qybreT} zt^Nqw5?^?cbdwPl2YrhIC}5(4xki8@bJ1$x=Cz`|h&)*63qs}%4%?9H%c7gYL{QlW zI2r5(=u|e7v}zo4fsTF^T;ONs*c`@uOVC{F_z)Ef4aRgOjrgok!xq@S`6jjkzBRFy z=Gfjj+L%?tuKe=sr(eGIr@y<~xThLXj?M#73lyfna81NImZ5KwBbbO?)wC?r8fSZQ#g(KOt1x1CE4b7qnIbB%Atrj^%h0d401Zm_ zPE{JtWHEFBmOu&(D$}xa9O;Z1563JCiN}WLDlqL_%gV5jh{mH>kOEcM1eoh0 z18BTk@Z>hpR28!kyB#gD$n+N*!5LP>EIXni5TPUu5&-)75hh%O01~JMaub?@5@D>0 zgkkPj47iwl_=}Vo3?MKR!tLQ`bfmT6nDw-X=+20!)!~R0;qcOMn6pa)VJr+KU4RAG ziVh~$xF|wM0%-~d2nHn^{7Mp7fjSE%N65L(EZRPP%ZZNdCs%Acv3&cUq4uo@*{;Z2p2@um zmS%3**S7VU_HEC%ZhTgnmMtD6q3#LZzH?Hx>}%b!f9baUOS${xrCI&vq;1{b-e-0? zNZRpYd*O;)9%oWV_NIfaA|bN%%}+1e^mOZnr(3r?+ivChfZf)kZBsYo!GEW$%Gvf} z`_|`Mw>`78pf1}SV``b+{HCnh4YRSv`&Oj#2IO}8S{rs$pa}J!*K_mXcHTcqL)6Nc zs8vxr-dMT)^%Xl_SrI7rJJWq#+`k(Ta2MYcUvMVsii01N1}*z%IY6gdzcO(>iDh1_ zAyWkta|~mFI98nbqQ2mo6~f1Tg#0cRaf^`f&+`KdksM_rbb$gs5n2}}6d?E%tsyZh3Y~`hSb#5Zod=16ejrX6&4I5MklEcLjKspViFT-?AzA!_#4|lxpMsDPmX={#o7P;yBm`>4Q%W|NXb0zW>n?q;10-=v^ptaNM7wu`k<&4d5&-De+taOA`H4}E{_oiUAr z{YCl*{QT$$t||;>QHNcpQ@J^L%{WGzyebQBsN{#n&ZySG%;)0|k9_#SvG?CQa{TnR zxEbNjXB&Cy_ue~1`+#=&u$_w+NVk>YI&o3ffBo*pm!F;GMzp%kB?U{e|ND2hCbnf^ zVg5gVb7SkiCBS#{^NarTZ>}R?GAc&=@$1h%xFj?D_rJMr75JQ4{{Q~#n_G`8PN|7~ z>9xnc`usGv`0u~Ev1QK^bF|%Odg`&4wmo%t6?a^|v*z0y?|yRm$VczVGxo1apV~H_ z$Dg=8Cw6&k%I1XBEs0ltdE%qbkAHvXok?qR?PAf0oP0}cmkAKNg<|F)Buay5ECL9; zM&EQ!F-Y4y*)J-)2*Y5TA?kEGZjHB&dj}G6^&I<_EB55IO!z z<;5z)V3vkB5yB@BQfyynhCWQzm1)6};ixQfm4xiifZ$EqnuMf_*Cv6=2 zZ@<2pzkZC82jWvAhFG|M%zys+Gn6P4CozK-SOpt0{_VTZCvC{UWdS3=rGOhxEB9>N z-@tJ=^1prm>BfDHFqHQ$KYRJo3#d1Yu<4m*xYeb1_PzhkK5W#&#bf{1cb`pc9=maG z1N{;Ga^%~e#e*@^1QY-M?PrC{Gp!=u2k*Y{-lZ217_^PgHeb5*9PW11dXX5Q-2|NHw-^E*aMsBV_mDD@&m*e7P^DfWtEa+3bPe*I}) z!|3&U8~**ftE1)*5F;Q@^G*=m9E+NLtonjQ8UOnI)k)1+qIeXF+qG~>#(#YGd4Bt7 zB-6is_t~cBn_$76PuBAk_88;+3;RB}@EnTMrpM*Ed{YhLYya`x&$b{gmI1b&RY6^j)J7H{1Hn5P;@%bpA0bYgu3}+8a6X+nr#GR!AjwpVJ zSUw+?BE*J6$85391!9LGo8d_`yc-w*7Z7J;%9(GrNj;2;n!ek3uBU5y^7xeZu|xhsCz z09J*#p_)XM0uZz=D`9~R$rpBJ{OQi6g7u@Y03i8-wWI%Z`|`wfBgJm0Hfe&14I}^M z)+O{ke8$E+)0(5_tseQ$cPkh(GrwijKi|1Lv2_$;7d?lo z@Ld$Nj{eu%mkXDT;#ctvI-oE&R1g??h_?OC1bxi^l z`O8QC%k6gyR*b}pBF@*Dd^7|3uDNk<74`kot@rYqMxY~IdVTlXukTT&gXr?u%o1pu zU}9wCHw|NY`E|pgJ8V=>k(u8*QjRPeg|z#(+n3fpUW3}R;j#IDx_KF!`JJn`bo=dsg=37d9-e*uwi;Zv8+KOH%{A0HxZ&{y_Bh=b z@4fiO?sa?SOPTQB1`a;i?-O1<;xav3UujN&{Rsxt1cM~>0;Rl{kqSIDj z4*q@&1@U4agJC(imc=|8hilo8xIIDAI*SYl(Ked>R=wA#^5UK4p~MS^7Bs^|=>ij? z2cS8tgbQbm563E=3zSuf&?!HmU;;o)!)MrqCow5-vH=T1?(+jM@2GX5nB9@%a@JI&UxC^HMl6nIb^`S%Gr?d(2uxs704E+A!4SVYE@@AE!sBtV+oEGO zMuj_69HO~P^8o~y$24L3NSbbklK5GbjUq+~L2Oq-ppSI}LL&6|i{=Y~BUB334*lm} zzLvjklo-bItnsUd|MQn;^41O|D#U958E8s{YexR_7w1IZ1R_K=E3CW~BmU{jGmr*4 zi8STT<`u5rQ~gh0oP%Hg@r&2iK3N6tc;oDrOXs%1N;uy6M5TX{_15XF7hl;5x5`;K zm_hPthjwn8^M^0bp$HeYjQHc1=L(mNv~X(x2;$htiNzzCQo-U8Xt-~m-TKyl>)bvYC(_z&RZQN>FfX3m{N|}`M6lOxp37N#qJszKAAfmv{ZrMD*oIwm zc;OspyJpL5JJVdZea=7p;&qfCfc1}GoL;x3+R73O{LNRkY}`GsaPw$hX6fnN-mj#l zA4^YqDtX%OEE(a;Gok7D>qOdt2BP{ByT$_Jy`j%kpyUKldqmbwRkpXddr3X4YB7#V78KOL!~}ff2bfDguW?bA%s>qP#|egM~@~lyHJH zap*SLp=QbXaE*4?^(5|Wu^xQr{y2rip`>;!97pq=f;Gec{>l-yZ=PQL51$_U~_%Hcrd!r8TdfA!cOt{(gSr$?9Vnx<77l7>=c@j5585BmEn$NzZs=pU{g{rgXk zcJ3;J>UmGA9xwU*)#E_n51$@hv#XRNZ=PK9#);K%ABI}9Z5HJA#>;EYzqAfjeM0Tv zzq@jDT-}g$Tgn*6nWE>n3}FtrEdybHyl>vLHO%|qFL?Zle5UtWu|h=e7^ zzi?tLaD4OCHRn&Q1)2_?S46+E_w-VG{IY?}?T?=x<`Mqk%8}JO%Fzelr0nqXAFm$& zyQ@c@eZDbfW=P_0Q?%jLy|~eut!3a94PD$m%B?h^jtQJ#syx)2C)T26=QR$dXYRuO z_^B9t<)&GPlhvEcIY?;wjic)>99^r8@vh!j$`Pm)FVH_-J-U8RMPOF2UsVbq2+qY*Yu7$iUbKBIgOt9J*XND2o-d`R>`msv{l}Xp zEgu%1W#h(1WQv5q9g9Fhi=Sbj;D^crm=ThMQAE;|L=O=U*el`!fPV28!R;_3@R!%) zB~h-EN^w#E7ki3iWQkIQ#ZcvU;}c+lEDdZFe0SUe+fG8O+^SyoiT}f6H}i{ zO?@Fb@%h;JJu$JHqH!@1fxs|Ux|UxUy^;+A3k(qq1?&$TRxN(AlJM!0k>}<|i4RI_ zC()z0gbQ8WNqoSn1!~OPj{bl9-t&1K{YSR;%3nDsZ`HuO)%_FdBj;@%_sv_oCUguO zw_;F1>)@E`u=pQf5cucH{JBd8{q3b^n>J3G&@y0L%iv$0+cbQBDlci(wxYki{0y4U z-&}rf_4aA7lGl#3zIM1BN~HhV4bxD@-#E1F+@UtRz&pNf00;AG`>)(M?e8u>3$2ZB z9LVKt39F?itzN2$Gp|_58MZbUVSu?|WZGZb4FSdF|dDpEf=5ZY4#LNJX(D}qn9FxclNEi<; z&%Sc)41yD@*A@TW<>y2;WT}JeiV1AsljiBxK3P($=rr5{x8=%H<`z zbI2K`2;qtsmRpDycD@g#fuI@@5h4>n0ePtaAzNT4l3OK?C~>7=s%&ggz2;iDL!;M4 zraYdQ{z8hNz}1~ZHeg-c_r+o!V!iR4a+81SMe?pG~NvSU;r#zn&_e5;e zrbt{&hzG<8mIpcy4e5e{B-S{N0^GMt{Elh>0w{n9w@J(nnbBe+JS6)_vN0rVCm{r7 zgu5vnc`F9|^@W{T9sR_Fu5skxJj<-c0W$eF7kBzdMyj+WYpB%LNEs?MwG!-DJ8yCS z-@Umrx4FMYV$uwQ5po*(;H>D_G>NO=-M_u?SjWaGu%vVQn@;a<#NWAR_v}wktU9}| z>DIYTr}s61lH7&;=$upAx1)11{pt4JvG&t1G^ba@dx{M4pG>o~V(ZRlGMXIIsw!);=+iq$GQ38?+u`Nzi9 z^aaq+7h&r0)qQ?Azw^?O)|1aSe12lh&rhu3iGF@!^~%*#tdZVx&o-ZZrpYthvwVfT zjlfhNkz`)cnq5WE*PKnc;W2wkzrYnLR<2E7&>@E^$nx0xsA&hQv=RES%*ipPX_P9#}kmBr>6Fby1v1 z#+xmM5rFegcf_P4Nu^jJV=}=c8(iXr6@pN+f{*=_i$vqCnCqEk(|9Ff9=2{~nn z=Io}t#@_JmRU0S#aC#F^SlL;Sy|6F(&z!c=)or7Ae?6;WJ2n)q>MZE!oWMceS?^jW?puVYQY^3?^oHGKl4KH#e<@(18S)9ojBmi*PZ4SOChk?e$-R>y{d z73&KahbEZ>Gql+qqvmvsT(K#imR1`FzheTlocgrCI=4Qvz7J%AdGW)UjX8^Y$B@Yv&8R62_#Erik@5Bvwigh+)AX6jGN!<*Jm~b*;fh)%5}~>F4>q{-BZ$^a=hd< zrA>=jzA}IL%7PUg6DnGUuk4u6u`<7-y+9kSIm3<0IV0xG8Ep;KGRu0eTvdSMCAv2J z-Pw)pPfQFh4CXHy*s*rPidBVk8b=ZwOq&vQ=BXN9U}fV7JKNv0D5iZ?K3A<=TR3Os zXwnmgZ0)`A__W4z6SloMbIF-W>HCw1J=Sy0-iaNL7p~k=NGefamKB&Txd1MmB26V| zQ4~<*sq$sTUDoBM6^2eO3(0yYnN_BnY$m=5Vw&YT?*u7}>=C7F7IK16b}(XgPy!4p zW+#5xS>h7JkpPURB&`;}<5ECWp3MNE%B1fG7c#V%l?TF=(vCHFPmPP+GCgL~7 zCT@>I!%El_pZH8-%Dx`G4y5#aQBt_hsg>M4$}c1bP%=CK1f#h-)$2xFIn=UZ)3}aJ z<5z4P-?1U5_q^E3Wc1&BkcHD~2 zoR42@9$J$OT$ZlO{_f?KuFk`|M}**yLgRwzz=NDw1_~zQK%}Vlsb$g2JI1G$#Y*xW zxzMBl!Ixn=lPvcOvxT$=&r81eT+I{PrY&DH4hV8YK0C}&GS0-eO5;PJFNsNk-)V+{ zmyrzc?E3WYPINrJy|}G&T>JW*PYyPYXh|H|nEb(ujk_M3wwgy) z4!14q7`I}@xILSSEPPmUSptP4z$dXrZ{=<@6QdzEsp*6_5_i0eVjoupfAt%PxYY+25Z7et7Flk)6Vqh^LD>MZ}jPVB(2A;xR_N@ z{+6&a4~sV#D0TQKybU4*zyf9?!T_5i&BWA#s~y5_iTY?M_H~Dyhf*gtUXnsi;iPB_%u=8?z${ zK%g>_zvTJ^f{M$#-XwC!QHn$uEERbG8i-Y(fT4-GDwDea#wW>Keeaq#FM48<9kX+)+F&v8`n=bxx19(C-;=Ryt|a#F4QMn z6Z%YjNKGO~UfEsBmj30-hU0bx0p&{uzw~%1$=(`CM|kzQjy*Pm$L{Qpijb z!e+Clnw z4-Ono5wf<&TG}YxJ5tkzdGz7Bcc|AhR5xg2RW@IV6}2#wv?}i8izT&>j|nUfN=i*x z5Sk)(W{mgYUn;L1a^kU4JXgSr>LYEu<%|cBeyAH_j9MCw-W8qja$?$>J^P>S)%S2} z!V}Svt0GX+gS8QmDtQ~Cwoi8ys}Q4F^cM1BQD88<1=()>hh&K({7C&nItmL>Sz%IQ zGS?s=D=FHlY=!rUkPtb6IjssT#HyGp;XF5vEm8z|Lk60p<5<{XC{AMPE9FhO=2U!p zjx9w(bAq5MqS6Ucn^fU@5Cz@36G3c1f^Ri`M{ME~c$N|o49U+YB|Vpz{8UohV{s9k zk-_#LLL%5Gb~SQBvh&1WD_T&n8HchJT5KZ{$dM&!oY(AY^E<6R-!dQSliOS~Thc^Y zEFZE#RThbB7F7x{Bh^H91g>22m#{5JKb|9Y19`;~)SsntS0vZhmbX$W;Yvx>FEb;` ztsqG|Wj=`kh>@X-BrUed5|BpDw(xF?R5D4%Cj(q6-NaE&g#?LaTtfDdx=K}nO_hKr zAaENx3}&{-ToM4~!;+&QJ`mqzKP-u$0`4a8fi}g*7ul^`eW*|1fczy%Q;?7x>Eb$p zbP57cmi20yBxsrQY*N!Xhz)@PrcZSUL2J{z_^%ifk0yxpDGH+e4e{WE#gEqsQJA?@ zMW{@qakiapk;NpeoU{+N*_J-m)JK}e2u&Mdc!wF@)WADrF}s#f#94f>Ju-=1&6#$e6-51j4`3YEw)I$ z$;vN;`cz@~=9uDYo#U`0B@`S%R7e<31|f19nh60&Vr!^6A~Yw0pfvcCG)B%&6;Xr- zAuz=soEq>?7Hc1q~J4b|ii+z6<;*P~@iukRz}|2uZ?xk}U{Th@LEsJP&2` zj3Yjw@?zD>AKeHMOi04g+He#ZuA8G0w?@Zri;UYE1`y=DlHf!rBBDuzgbGEt&Q}$i zLy2m(CEc&p4lH*71mur+aF$B-k?bf;u!A-QEI4Q_wrFBrv{;nyVO3y5ARDML#f614 zfqV|UW#VS5l$gDkUN|Kr6fC7u5VFv+s#$~e3(>)6rFutHAXn=Y^pM*j#Ap!rk?1#q zc)sSHPgbCW!S!n0Tdk3vFGd~3bj0|`QikbIk!XR0oMbBwMWiszg@X7oa4}Blc4#P< zH@YxjqNyFFg8?r>d*r-!e_qJng#tlF?hZbXQutLQbB&@3Re=hWCF(TOKw8LMCR5ku zm5d79mSm>pS)M$VA5Nm`c(0l3p&8>m#GDDSX=LqX80bsL1{&lK2x!(QgTxLO8AuM2 zHi!z4@Iwd}RUrMfwBB#^O+VQyerE!;!V(Xq{$P@@Z@~oBcB3?Hq{fA)VMrQZmPir! z0fZ*G6u1P*lO~jf6B7SD$qsPJ1(L!oABcDZP#`h8gmgbY6fHc4v~a*JS?HQ-hS)c% zs2iq&Z%}Fy5$AKsRuCR0`?#K_o5UO3*DH46Odqays}evsVrf&zMCqJVQHZNWbOLao z8ejrFf(H1j)QStFkot%mcGTyVY-YiPirGDa36DpR-yS41O}1;?dQ$wNXaGXencAcV zLc-b$gS-j(uoNpuO%X*$K7tV9Yl7a}ZCbF+0UvHf9#Eix5?4;Rm{8)u21$}Es|p(M zxstFX#Dd2edXWSU%c9~>`Q*BosS5&C(u_!zgQ^Y(6$Lty>UAtzsc4fDIfz}OPzxN8 zBsgD^!wL7V^`zMWQE)6Pg>7hy5{sdXiX<;v`ZKXKk@2 z=?jx3H(RRSR5i8G4q;xRby7zpd9mbJm^oevc#Rdxb!C9s%K8IRV45K!0p>2%*BP2N z$|FKzB%Or@o$0=CeN_CO_|$XB{od`<_jIqMCwqk2!?^gsB?deoL%LYQO>>y;9j<#u zcnNGUzLRBPY?F2+MgRm3%PtXOgyVLz7*V!62$@|m-s|i92+zHzMR0G0OwIl zf|SSsf+!OG)U_fjD zkv6J~6lf49s_<0&PUuS#vlHj5_yI^47e(7D-TBQ81l3I$;S;xjLrEk8&Tv z1Jg_RyK*H7Uq+!)ODp&n7)1S{K~xkXqE3z9SSX3dl2E3=2jkWiGm_dmH`)P*n&n0F z5lp!CLfuwHmwDlq(eY(eq5xVG@0HGENulHb5y>Hd0bvay?kN+iHkkl-=XD7jfyEr< zc2^%wK3$mvs^Ehn+px^tG=ET-Z=ks&7=Q&(01#B2@{@EtU;&>sh;|7d7cl^_>nKR0 zG#2D=p};AGhuZ<+p`$f{h7p!A%r-{aaxJPwLnwN8Y|pc4L*5-Y@YQ}PkEcYnMiFsC z33Ny!&%i?kCJfAS+*YD}WoZoPV7}yvwSf^XTfi|k?aMa*dz5E91*6B3hjleqwdS~Wznx@J4VFXkGQ`vwR+ z3RXT6$$X#igG2Y7l07<#Mi)v zNBtvxOVA*-t$`3J^lq!wh?bWnpRKm16yxYCCdcxX0mS zghZcXeN!Gwjam{3jyYv#*+31IN#x2%lS_bzYf{Ma1_M(6MvK_R6woR5Uf+bq!cINUgBs+ z+DSMMHB~V2=Qy^D5UHjh2e4jLLLq;mav*~R2gsyd7LrwIB(Etb+9F=vS}B8YX&FY3 zvq)kLEtVe>VW}xv6N147gtE6m1Ez!Pf)!sA7D)xNw*YW ziyDKCjqXH3ElOIY2C%Sl5#P`|m0xf!mn0Q3RNhWECWLCppHbzfqU0iiCkZ+8;>EVd z!;ZvP#ylhjpn-xYQA`r0u4|bBz`UdBCNaB+ z_F%$O(LE2R^n4*bc~g(bmMAzZStBS&+ywzcY(rMG!NA6_P#T&@rm-gRmmZELEN)k-IuOdP!tbM`GOC1PVL@%l!@+6I9_4AR&_+jgCm}42cZc ztZXn-mhq5rCU(7Kz8a&6PB78Q>Z&4oL{5MS2@EJ$M3U*WM8TphjJqjvStQkSU_wH5 zwLu&UaCSGPL$>HY&3mi@GHQDCx?4R<;FI zV=ye0}Fo_0{PlVI<_2~U%&y+RE(G8Ih8gy!p^HGwmXE|D? zEr3%_C$YCNQ^pu}hHj0~^wEMjbF3H3gInW1V*b1eguP)9fDg4~h>$Ax#iJpDS*TuKEa@OBe2wOVH&@UAubL!_w6U%N%67y`A7u;a zkM&I6o-7%ut3whju8-grqPZ#LiI7ksUD%Z^(F;*TMK}@0P&%+kxco;kLFTwr7)f=u zP1VxEAVx!EV>ohgIC@!ROj|g%Et(x-G*K;~7?jY3!AL{}MSJstH0qxvw}ZHXt3=6? zf3k#)yQ~4RJc;5h7UfFPVO>jtwQQ(iWsL`-)+#?7?LugOfvZ9JMP^{e&vyc4A&ECd zx#<0=tz^7E^v~Lg{?U#z zQmhSdDVWICFf5Gmtoh+XJ@*y(+~zM7Jqn`6q##^D8c=uY9 z35JS;s79{YB+(Zf%o9xF3P3^L=X7O25EDY7Rct9Z#fh2~idqyEw=Huizq$J54C@e~m$YC0=0mwf*KxTEx8wMiKYYT@#jB^* zJ=VccW?!8$Q_K-_y4NoD+P(B1Dao8Vf8{i{U`%(*h1Y(oCj8rv)j00$SO^Tw|}#>D;=)h}Dct50vgn@b(|v z_oJ`gWDZQGbK7z~#_JjE!G>T-0f{|eRAX#sXx@REH%6m_yfp6q?PazyEU)qV_bYf0 zNmf+7FY@WgzDN5EIM_F3YjX7BX#4_Jim`r2`>FH$zWV-y8{dD-q%8?a+Y=JE zNaURLB3ZGOaI~;&98lTlha&dC7V?HbG^#235?a4l065>&N`iPI&?n+oME80owePuJ z{on4>_toBMPo>4Q#bf4nyYGlkh=d?z2j4cHa&BQ1%9^F@$nA*6PO$NzIb$RYgE907 zCLXXZ@I^=rd7&?=F@Uxsm{=BKMFO^wm=}v8$mbv(7Gs|>a}`2VYiXkDpg}DNVt7kx zEQ@4Pgo?hj7@Mc7s!md*lz4FS{v=BZ%iPVFQdWiQcVYqotYy~V8*5gUPrY&Xt!sDQ zx_0;C^}FZU-njer+wUBzZLC~aU$wBIyr!|duCAP&+J=&a4HdQZWpzzu9O38s%}b^K zy$jdwp1*$Y!u2~`E8}Em4wu(6pGdE_zP{qX;m0~REorE)SlGk}GL&i+oT_gqr*$1^ zEai^1_2mtX6*Y~en>H_DCSTsYcXJgrls7p`@ZL| z-+AlG=dUbXQBzk}S=T62t!a?EFuv^Y2pnlF-*@OS2Ba}twSD``J8v~KlzUTI%SOkF zMi}TRT5@XasndJio_qGL5jySd=`x1LdU@9B}6P+l?Z`C~i2`sST$_ujew z%{yOy{r0CnKU3FO8H(`hz0pD?6;VPaIUdk}9S%`r*^rEagdhTJMADl&i+>jbRC-@calIQ<5@((lt)(Awu+con^fO7K2ieA@Ol2!wx$D zOM(DKa9J>IXY$Zj2aR}dSf6)##~+GM+z`c3_~t2Lky=O~NduiU@9`-q6^wX_@y4GLQ2btPHL$+b>iIYz->~)6y@^$WV>)IJs6Y-iJ zvEqs?cCLPM{LVFQ{Tdx^UO#u^+L`jQiDoYYBgWg$%b48G=%=x-_w^cmG)RoI9>;Ux z!gCBOBVRjn+yJxC8_@=&F`z)?D> zv?gm;guDX=_u8^y@%8I(Uc33`jhnB3ar3o{@4PTJbEuK5*(3e9ya=H~U*Ofsc3|-o zC;*7?!Z55fwlhqPGAUJwhvIvk=r!pPinO%o2@iOB5M1_zacsx)!rBSytQAO@> z5E8M#!%FmmNE|!UO>eB{z_Y7wfBw=}pTEpj&G6XG&rb+b!Nte|s2=UsXD6aP9#r5itg>FVWugZigwsZu*_BkFM zmO0D-%_OEKChf_TKIeP)zSuAANUzv6@gzy`5-@SG=OuY?h^9;P0zqJulQrn=H=n-t z%Tu?$eD&HdUb*wd>Gq{_G5C!BUVV&6DPN`;$_-M7h<}N|ZgfX9i@U@>9u>bgCVqch z!twaDS9%V5z5kF4g9n`Jn|wGSdUKSd3bD!)D+Uz!F?3ODX*{VstXj2497Oq;-;!2< zFD}3WSpz4AW8>Dtft!ydYBo-oQlsGmF1z5Ow zCK4Z!OZ+$%#4^BEa2M@R#n=*uU`1GWQCRW>-M=kCv<}5dVFCoOW*-gbs4N7_qEC$` zH118&Ufk1m^XmRvA052$@qt^Pym<4Y7jJ)ZQ1g1kN^g{s3mQhdEE3*qV%vi}iAFTa za^iSJ7FJKW`SE^fAIY`1K0d&H#^`~f>QcOoA6F&gOP=6VKSluW57kw)*Qt{HDzQ^> zj!OcSBwm0U-0L$)7dO1o#(^i>8Sus@FW&m(d8T*m&ia5F@cOR{Y1OK!NQU^{7DH}DqJE*sN@pJRi0FDSU<>9GuuyIy#De2+aDibTRV3$ zcCRGgsr*z58nC5BS`6`8ah|%lQ{^rn?f>-R-pTnR;oTL*A8A@ zVGr^Wb-<+)*BF(&y9d0xH^TSn7Bn1mhiRN*8%8yiot@{2Kv zhvQO@rS?DFXXwQtL*DG4ej+)3S1i6G$ttIGQFJgMXJfN2W%u7dAbwqxQ>W`yCSNBs-GSMlaS4W0_6c7Tb2>257bIH+ z=a{UeDC3KdP62|eBWB~(A;H94 zSxHUB42c{DWFJoyepze`>cYGV~SP$ zm-*0|4?VW{#-(TOUfO%}-MzQmc6l%4W&?sI%Wg8j1bP~2>ii@^hm9f&F5?p;mvEF; zS5DFbL)$PlB8DI7<@5N^-E5%MTahKk?{Pe9$Pr6%9arexT{ZR9W*21v( zR;*~mOh{Y8AQw*yD^+4AhrNe(HZ$g}OS^AfdWsR))>RgYyH|Lqhzj|nM5;3^j|v+Y z30i4!Ca;LAZoIo^eOr~x>fLAVyz}&}_x9Xk%d=nJQ&BX|6X7v>oBm-Y#Sp}bqZUOc zJ(1MsP+I!Yl;oZ9EM*7NByM30H|_q?P!~OebmDyuLAvoe8i)roKE<=KeeUfK_ucy7 z1-92eeBsp>*VulqG0Y~oLU|p$GkSe=@*awud!)UP(&M?rxTj)cpNfiqCI)+=-%GuQ zzCL8|`9Z0#q{Qxu#pMq@Vmh!`tUP{}DcUd5cost#F9|M!39$w=^rG;*P|C)Xey{c$ z`PtBsSBLj|rEeU*Cit}b*H}pSn9?25A#sGzI!c$Dngh!rY(oMVe@-J?7XAnNWQX7d zz(XbBlNtVpV|EYzLh4DYlte?^T2gv}f2k%wNgd!IoCiK+nH%7u}m<* z0SlI}A%xf6)LP8S*v7UH^P<_$7K%oR5*0}PriHBHFZmy&hO{{}-u>Zib+^v%xbx=D z+ZQ&omHqQOuzJ7^3KG)7Zn2RNA|4t|+^bftm+-aN3JYwE%)0Vi`ro;*{nmx;H{aTR z`}{Tr$sW;L79fW2Q?Om6{z^fyCX`N&h+2rrU35m`Ygn6j^>FDSAqcAyN=3gCbHOm9 zwF6t~+$r5WzwIt_yRhAzn<~6OPIzve-+B9@OrBG6mRk?+OPU3r7pEW&OARF_SCj;J zmZ%CjM5+xltf{A#hh>CY7q@Y3eMz34?v+(Bv173+FhsGp(cjeyvgE~j6SYCTl0Q4K z=`MFvx082uVcYFD)veF(*tEC=rz~cZImk3e%QE>ah8ep!I%QW{ZuG^Kt5Lj-J4+HuNM^;Y&w3%m3X?qRTgz|0y9e08Y{1<5wlYtWhM1Aa z*2Zw`^Rc}SC-ym*O6`Bb?gT<0;Vq%K?J+$bPabfv_n=n?3_908^>~k{t$RVF^$FGX%x3AxrOT))}It-OB>Dd@!j@CRsHQv6d zL5p;H76KtLtiU7je*oeBIT%(`CuW22BSR&V+7Y;*fwcA)2SU>&2L)q-IC<7X>pMND zNy+NtvTm~!tV&r3<$dm7IU;6Q8ZTdqC3fvfJGS%2^J zrrT#XW)ABq0+hd^Q|y%l8)QJKVb9h8w50lw_=L-g)NzWAR7Hhg1@C#WgU;&~6p`ca zjnfWqsAkxk=Vbg_XExkD*U3+!Z&-1N0WhI%>P~wTGpA4X(4LYzgxyG9soQcqKKL(C z$np9pj$={$XwTvG3mB8BGQyqH8^1oizJ69Vaso8K?3g@U-b3LVr9L^js-n;x{w`Ol zhi**c_L)t$PH(&`kGxSH`CRA5reYnfzo%vmwsD z$8+D}=1WQdki~OKeeAe007IHcIiA<>f{-+1#sc|UU;z@4ROTXi>u!@(nkfX7m8ksU zmz6XT(V9eYQi-#;W~suHY$%mCPTB{BdoT z#yqi-TYP_%jnG(siu~DvaK|0?9kq@9PoGJ?D~7>HeNZlJTl;~TzOvV|y+*wy|aIOv%)m$QlK*C=Ut} z;RkDsBdIWqPi9P>C94)p_6PWjOjFhJA;nmSvWy2x<4k4j5maiyXz!juWqY*s*0H7cjxB*I5fW%zl3qoK zkiRpb(y*z$Aeme=A6etU!bGf}Hu~%1%kFWxYFy4vX8&~9F>5U%Q4kEM6P0;G4y`J? zd#sIi`^d7}$CuIVuG+H0<+Qsm$@cc~HjaFAv{iPx<|AX?V|vGz&;bldZflX~PG28i z%4jlA<-tr*W*b?Byr4H97 z*qh_!FRidV8*wg_mLcgepT5w-z0@St9c8x5c>ICsao$)jG_DOXQThEt$(Rx?2!0md zDws&yoffk_2JV9ACotUd4DslLJf0yE9=8V>-UN>p7M`HEg|G)NsVW4|`)FFwb7ou3 z?Nh73?%h+X?w#ytnlU!2EPD9)A^lJEqnJN(OC&3RMYcr7ES1F~W41>1*qhk@xt;^} z_wVy`YV3wcaaWQwE`bfn&VrSbIweuR9LLO)_yp;MIE6y2xxCC5y(^;6vGmbrM~pl@ ztmnR-flVrb4fB}v5#=z%bikHI>d2Zk7!YU!D#67!S}b-D1Hv%orLjVn^8WFq$dnap za;o^9eqxOR^6W^j6Ie)IzbXikbwk{W5djA{AcfmE*Uw_kEd0O`a@gr0YvWQOO5wDs z2PMNx!VEC(T1ONc%mu^)C3wh#l}>P_9l$PI;|r|yhg5T6p`=BCzvluzz{If?)9)Q> zzIUjJ?bioo=j+4Gd>#OSwkA1lQ*828#Cc6MFNTxSLon4K9P)D(P95o9`1QdSF8}&) z%bmkb*#lGf6DQc=__#=)t}hzKu(!C&!4?{Q`1#GDmU{=9)Fs?h+Sdn{d~;YPaQ8^_ z*M}By?LE{!&Apd;WSMsT)ZtoBccoZ^&e-I@!jLeThJ+ZH7W<25Yt%et z&fM|#5%sQ^-r*)r4NQ*)YIs$(VN#xr4rWaB5DHFOkYg8TVdZc4<%TGfQW{NW4C+YQj)qB*)u?_Z)A( zy1zNfZ}&gmFXh>kxUF&g-75Km49(%_rBM+p!x38}SOY5knbclS_3W`V5g|d2tUt#u zAwo)?GR2$QtvHc~q1GXvW`|}4!c<7Ciim$9hMe|c=Y|YEK7duq5EABmSv3~@M~ddK z^YCy}GMn$n*Oy#g2J|wZKgZ&a$PlmtIyCXfl1r#CA=@dow29Wkn7F@QEcyf106I0f z|40b=D*`O9CQTMLaE%5qA!dXMPKa8Cfvpw?kSul59NlHkS=$GvGd@~kc~sBww`=e| zp*WEvi-NRDT4UN!k5-Y@eUSwKT4xG02?*4dVtS z;jY6Z!v64>`sYv2`}*KQPBHkMgLUk<-R>Q1q;c@BI?Et*`}%Ohy%%K~cMmqw`B&;J z6O#)MG+f$M?(u8z9Rx9xy!)cu@!NwkdCqb`siW+eecMkdxw{PeWdZ5LrZTTZn=GE35}{tj`Y~p7(6&oDCr7c>)ymp~ zvv|1B#c7|680+lca_N$w54vjU{q4#s;HO0PJqC$w3k#2e<9E+db2^3C6}YV2l&E!}NPI z@2y_1-~9HF1oT_;_sr*ePE{dcPRcD`_zWcjjRHhpR8s-i3o2T(b`+WyX%O+JEvMSx5Telyf-n&~g5-vjc!YXdOq5KRFcg3fijJ_0 zaHOsGAY%y;Ie;A%%^Fcgb9jT%*ij)$FYWJUO7c^Wev+{p{P-C7%sV82?XizDW=8k^ z>?0@s<5G|2GV|r*-ba@--?u!|go1Wx{J1)V$??Cq_{Tc^!AIGP@J8nN4D-v*WzM>P zsdo%%kbH1I+s8lRf&*N0Tvyh;am=pnws2zr51cg%BkCXKnq!xGxxPINVMQinK0gx` zP>wvEx>czkqaggSNQGUxD=k zXGGgVXo!Rx`FeQ$qmNO~{SQ7y<@9TLKplRv}-v7351U!N{L+I_Cion(>||i`Ef?u?dF;fr(LDZT^Q<_gyX83q0W z4Qdk9Tu|;Bu`-jyHV%iraIHON`N1dWRjKD^l?cH}nj{lB36V??870IIGWAu%BqIMn z{q&gTC=A3x?u8*gKrtW$gk@AkyderYT5KbxQ4Ho2n#NQ-s9Yxu!jB24)P^ z4C0r8OaLbk27p;UAXuTK|HklIUDl{B^I3X#tY{O&gw#4L7ldpv45s>%PUQCRDh{yH ztYt&*(tCSoZOV=MrjS?g<4H74E&2Bkwwgty@AUQs0nhKR4?w*JmUGI%skMW3Qx8-T z9`twlVzZ;c>L@p`Z{u9PRjC~GtNCWoi+w9vr-(=A*fcxS z-M({q>#pUkneJITR<^KG_bqSZ5i^D{u%el#7;ihf+c>OcS|NTwbf({~KCJ%1(%}2A zFXe)EP9bxQ+k@+t&6~zuVZU*!yt}<;?sh?Q8TEqWe(q@{=>kw0HFP`1CMcyF44SWv-SK3F>K^t5T`rc|CTp>Yp2HyA})#Npzh zbQNh(O;lKfYPi+}P))i`kUe_OZHX7XQBd`6MeUvG&3|bk2VQfjy7Wv5Rnh7GL;))_ zwMN9>h)jdg!fFCSNKq+RzZGg(NgNknfe@C6+)PR`_hC8aClfJekXwnga&D-eKh$?ht(n-zj4+nn#eV}!5i|D!|~Md1drYk5Bmye-qi!k$FKA}yqbA% zrT@Xzj5ew&?1SEtIX=a$?Fs^D&!)MKBTJ4doF_v^hbn>Wh7Z?BHI(yy1U_UdN%r24SgyRuLF zSGEv$!XCiPz@$vOVdnVg6@4v_uJnv?@YP=K|HjCgbWAoU=DCmhpV?uYgK$FxfGkSo z>cK8nln0jwZVa!kEQz}dovPu=%C{;DHmDMekx7~ z1Q9*Oj98_ylXd>>fV&eE?uc6)G2sdw|lTI9@p8-4${#@Euz75@JF?aXw-Q^*ZU!apla zFg4T|X7xwMP<27M-*0z6#~X3{|NJl8y5~(NAyL?qj=vaZtt2PGk(fsBy}p!bjz(d2 zKfEmK2z{WBwm9s}i^9bmnCA;F0ALzTZB6kRZw#$N?m7NHImMWW{6J+#%ZljwNH^T0 z1!cn}6(eO+Mk>n>m*Q{ZTGL~WFQ2ta=ncdFp)eZ63oLIG>*cfsiX6w0=su-9Fx%IB zTRrPv+UNasPSaO2D$bXaP*MJ@%SC#~Bm$99LgGcoGqvAQ6NbY2vNFL!XuUCxQ9{u0 zoyd=2NFeZO3`aqh^9(u2b}dry*_Rl05=V%JiXC6Rg*BgEHc1QEas>Mko7+<|;UWC_ zIF*%yocpbFnNY0GDt^v5gu<1_-Y>s8Iq0GolE+;J!huBMSYS9s_r^j~LRXG<-@o4T z=!@Qm*E8eSGY>!SAHUv*o-i~UFeAk<*Z^c`&cY^SofuFiV$0AV7_8XW76}J(z_%ND zMg5S;HiG{Emb##=eAHZ%w?OztulMmT11o1TrxY$jZeXpXR2%+2Q?Q*S3lS7q&4T4l zcbjJIf)R*lMn4NCf!a{Z_6KcFG{34SzOc1Q0kIZYbn{!vHP|rQ3tDB)v4@R{(xk5> zaOLO<`+51WqhIjB`*y6JNr78<0hl6ROHI*2b1(wEs5N`yqPEi3>Oz&(&R3B(o&?ep zY4L^X!#L;uYKcXl%IA4uPFd7EWqxZJ7fE@MWOv+I7>&&e_+|*Tj<<1j|4P&kTF0t1 zvUmJbz`9xY~+4;3R5 z*iL1Ki>XWl1flPcG#SpsQWyPX3+srU`a%C;0vT{P-ysn>k!M2tus&9lDX2JCQuB3X z`(K;q{drEy^_KFp<)pP0@I)q_cK|w*yQY#(0jO27rm&i%FuJk>EiUE<5v@Hb5U?(o zB6BqBBp*AA^gty1sUc5+(!Hi|nn)3@Jq8a!q%32rQ4dtut`|`&ya<-16;;8oPt_(2 z4p&~?pgp42;n75*RKoq5gI7VhQHmtQpo;^M97-tnPulKqj6f_yj`#iqdg98^^6{^F z8KXCPMsM_v-rPQRqo0tDltC^L!c1rB(e%&pL2uZJRwgubh737aNCEN(2-!$wh3Ye(9f#D^6k%}$3CddIX zKePpAvb9^;gIA1ifm+9f7Euzpf!x5b0b56n0!x+P8rXjL3Ls^R^{F4+%#423_mI1~ z+4tZ^-@sbw2`uHnQc=tVp=p{WytfFQ;2bapo=_unkte7LKS#52k+|i)dLJ?osS60a zRPT?r2SIGO9ged&?kyD;jJM27%#Lwa?5CK6Sn`!a-O?r3`o}-*`_E5z_OEIVPLsur zoG}C$ny2InmT>vOs)EghJ6EtCd(R zG`FIy0Wq;wK_^@X<)Yyxg_E%>CS)`G16a<`ff%5(eix*T$`Z@BU^(Fo4Ume=P36Zg z%LM+d0;Ld@85#l6!EV>IVj-4uVCJ};@&F^KJ!fQhZV*Sr^;9L@?r$e{uD+>E0|7*V_Ad+ z3-gwk2DX8I5Goy6>0S`_#>U6(BRXC5H!En}BML|Cu`KR(Wtei?OC#M6zR7S)_rKO% zJ@~qBU|oy9j!>dut)Q+L2U09+k)Y`)FDWFlOVI*Zc=Lc%CW8oU!adOJU;xquYXM&1`MU8*>D=D!cJzsEPeB+ZDC&w!h@Tn_6gdU;!{Oa-knnWeogJ$)n!8h zqkv){cPs;iaO~PW&n^7Dh7q0ky;$xYLZ?v z1DOysgjor~?$r>PZAb}3d_%!Dd^h_&gg~LEV~Rd$ARh+sL809H2bVtlw(tI(zVUA| z5AO6mxYNsaRk_PTspXX#d@3v=XT7kiz8ohlw~!Fcdv4x{;!6o>yh z3PELSZ3(mQ;kW%}}MVsvXX2J=J3cc6o6 z0=}Y3?{qcl1t%NA7Uf#&#BQUWM#Pr2eZyq%`ab2-q#W%!j1`(#06pS=*^Kw2YIUQItYy8WBv9Gp2ywP+2 z%ifV4^L>S3E-17IU+-*r!W8}&?k}!+ug+_8FAsK)f7K^lYt-$5EpubfdSxfeD+bFe zkCf1(qGVq|;qH{kA}KeZ8Vz_*mY6R~*@Tf}#o9XCs0~|Lq|zqJAXY@6wWPE&wc9N| zP*8THqGGrTQ6sZWyC0SEz>KrW6T*Y=t$6(c$N|G)9il?)xgbQKf}q=C%#I)#5r!d8 zl$}Y)0QUl!$Y-vtOb~Z6wWrEY!2V4uA)_QzDgk9BtZFRrl9}IMNKFOxCeLsl>M=7=)oH8Ub?&*FTb>0zHTb;9|$CfZB!>-o6+P1bI3+7Q+h1 zx`VjcE^}wY@`n50?HK!^_rVVtJ$~Om{$1wYN1JzTYTL1~b?2tG-i>Yj8}(>6*zVuh znjajH+0Z($sjY8A%f3v?u${Z@Y5%o?|;`b`duH_AN^kM@^4?h zwzp>vml}Afbzps~xiZ((8|6pt9l3ZrcXA-partByI}r<#e2uQ(*JXIiF zw2{Gs;+?`J&YY$Ghca2f8Avb^fLH(rpo&-iM2*jf7u^;%+~9FVcraHem8gsOY9-Jl z9HX`V=Gr1@&6|B=H#6fm2kw2i(HHTvz=*AX7I5xpf@Hupx9C`L&4ubzJj#*MuQxLf zbylW(L3R3-RN3+3Qap(xMWDNh$RZPzrP0_*43^uC1+`f=M4bNdz`O~UQr`2NYEKo~ z0{&!hj)(}DK9OkQB5YJqC&jF=DBzmlrj#BlB&e0zm8O&jXFIV9q?3pd_)c;vS%D83 zoC}3y^btt3(AdrhNs&m0$Q@a!9>iitlIf8oVu4I3M!Vm^xc>V6Rii)ljos@Tz1ur_w`bIB@bv!O-uw4@ z*cktj(VHI}rh|VpgHwLWXwTh#oqM-u{;Vo8?EnnvjX~5IlcRg_^anrnaJ2__^)3uH zcshP>J6AR*nhWaP^Fx=iZ=@rC)B*Q@>{~pmf=nz{km~Y8SS^-Q9%Uop{;kkXnO@Lw zn`V!STyX&H>PW`P^9W*7Q{`~;w`=qf?)E(Vv6t)fS^74$Yl5!~(&&P#F%T@QFL|oO zeeq6B`84)^s685KkH*>+IhHc3^#c(PPYVIO57qkQG_*vBp}L(epbF*7TBD@cVBui| z8hv1A{qYbcD_VK{o8HlHdQnu1+bRMjAs~dTs@_5wjrfI3$%3xJo$FdSX8aD|b{&83 z4;!Ne;R?#@MoI|55|J$-QSoL`@$TZHUl&2Vse1mMDT+7Msl6wOd%@aC7>X={QgtAM zfhrMRBXdBV-QtM%jM5dt1HPd5#R%tIG85$^MKFI<9p&OB$Qk83x(K$@V~SsT)eA$D z3o)r^U+%QwOvG5SP$;XJgbS7jLL zVJQPie#U0k#4OdZ33*miC6FH`EkRt?43ptt+vK-p{`WeOUqwlT0C~b*N)5(EZZe!q z;&Knhl%hD|Fps3oH?`mT-D;-Z=-qxZZ8PI{B@LQ5OnE!Z9!ZSdZzMwpJbs$_kDoHY zc-%daG?1#Jk??SgFc*tOOHJ83gU|i#)>eD)*u6|{WTiwCGkd9tFJ+7vX^-6~jH~fV za6=5bPO+RaOm}V((8BM$P8}u~wq8#pkliwV2~P=@BUIkkc9g z5bn&0vf_jIljzKllE4VWp9nPyGSXaI@VDz*F(y%0V|V)g^oN&75t2d`9R#tp)lPDo z=gjvS>vR}e_CLOtWs9y|*j|xXktjb^UNT%%a-@iGY|*}glD(;tx6*Pc_K~hAEG1)c zpnxnl4y90Va3>MPr=k1vf+yJ;5u!tgh0@ENa_Mu}_0xc{%QB--V9G9hp z6v)nG{AD%16&!>RF9spD6BZPv7xj8J;EFqKWx|3;7WhqGL}4h|Y4ga0`X~9=Icx`( zlqIXg6UK*7nqj?q!XN~85bI)S8-c)S*c4;pGq9KP17Wr!Z$wFFfS#gQL5L|~KE#wp zL<{z(x4hILH+9%aAN2~3#nZ#_g5w@KBQ@f=C*1fc;Q>Lu0*98BN3_@kjE%*Yec*-s zc)Z^nsdIF^>K^pZFJ?%0DCP`1XruJ7o8T?Tj%?4^lC%mcO{j z0sa1JKSmdgTB2)-0SA;9=~4*|6c<`m`Q?pKY}Y$g=FId%xgj`rO@L4ZKoInVuiB6A zO${Ad#F-g^$d7@`|s(ct$r`e7wOKHyP%xvTNtzIyfk_x%rl=o|gM=j-=ZFPd5D z^ZZB#@m&G|3ucx7{fpPezVFAt!_0g1L+0h~26v%bb-JQ-xS(_}ReCsGeyFJAU?CE~ zBXL;_DjFG8ohhq2Q(AhW6s)+Yh$(Jn3bDjeHmXhnrV@KmSQ%y;BbpZ?$;^UISdTzi z`n4piP*5MxthC~I1u{pwB+~>Jep$pqWP+OMcAYQ=61fM-*8GV5mhe|tNEF~&EJi6Z zcL)SaCQG>95kCcgB6fhQu#iaxHU4BSnvcc@6Q(jaWRnu58+6Zmj= z8|-I~#%2W>w4cxu#^Xzp6`Q#eDacOoXA)&j7=(vh>tmib?06?*g{KmQXA|i&>GZjH z@@zbL#`R7{nactU!aHjt#3U1mznU^x#aS7|nutf4GV(k%AxIursvHQ<_d2OV@q%MY zgvdfW!*Ek)qS51F_gDm>BqIXigfhU$E_K!ug#zRvhJgwInDQBh3Yq>O%2kj7q)&$7 zV#cn`#_ZRnftct;3&yE2l0Lu2FJ= z4eY3JgGwTh358VW#-ej#;h8b4sYqkknoshy8v-~MzGDB3`oce6e#shdqkrrf{bAtt zZ&%}%?aj=+{lOY6I&4er|FQ4yUv1mi-IydXe!7B*Ut(fS#YansIAK8%MlC&2R(7GB zYQCv|m{Rl6bif%?s7ZaN6n-a>6XPV~l2RH3Z{SjwApE<6G8e)MXZ8U^teQp?2 zihIl+fHdS|Py&`?Q-L#w;e#meE*HjI>g*B+6_JhOW1sQT7rgYlPWm^Af{O@5EOp-X zPC3X5fhjx>{D0N6BDestpzLO#bxh=yN20A(CX{{`HK9lJjA>5QF5U_8-Q-9#e#R+0 z@21Wr5R3RJ$2lHTmT-TV1Dv-au|1KfVe;fG>iCq&hi?Ja1p}cMQ$7M=2+!GxtUylg zy%r=%4rDTHdLjkYERSaAA~~1n*mi8-!PdkKAA$+fGNYfBhTxYWa>8-N*DFzvJaLRAWWgH;vBRT6zg!K7Es0zzQ?a0c7E&kCCY69#`0K*3 z-Y7I%A3!Ez%_011+6@Jjyj4dE6{O-%)M!L%L$UgZ+>UxbK&e2)|8jTz?GIKPOBE|o z#;OcU>!020`}dpMhITJ3FH0nzO;E;)jgKcm#EG1Q6vI$i`H50Wz9H$GetBBWjp`Y9 z>RP{TZvL*R?#nuQhnAiyf|wiMLiYB?1RjG*j0l>NiYHX4W@B=LRoO@WnUuo{wdrDA zq!VfF#pyWtq(C6}Rw9^Rf2A9F&Y>h%a)1Ge(2H_f7(;zC7^r|qOpQ0Ln>yO$WFk5n zfIujlxz1V@Sxd^u1pc9M8l)8zJ%?a*%!}o_vQ#FaEZhI3M@U$c|Eqo9xA&DEL7K zvKw6VS7VevDOqhg0ZOr1GJa$QCP5S006Q_(jmMOpp;nwUzIqcY%?IM0K|vowoK$^L951!CAg;{B-=dbz)C@~# zwh9yy*&c!3oWL`CXhWp#r^evHfxza`Z(>+%L}WuSYK2G=nC}FXh_e!KB}q>IR5{_s zFOeCx+VtdX>bG|G(WEXP8$fQH|xE2=59J4Rt( z&3iQszip^IQ$+=zTHdhgpnV~%C6tsiCbdCX5q1>r8aNN?>AF;>f^ro!4wxKj6|sY< zr!|aBR8>yFQkCJF?^7h(SW6XzCSVFl=-@r9A=IjvnM#)pP#g)!29dCoa??L26Ec%* zHajmgMs#F2NuwA&q0&5x4#|ALbOST`p*XW1fhc@CfqF>2EwMmbBrkfYb58tJqF}^T zUhjaLdcy@)OGj>|JW>^X)#W+_0uT#i0#UPKp+{xQx74~qo7smk3fqP?2$e|4rER3jOft}Of2PE zOp7VoDJHfpU|SN2Plp=A%?2EWK;REk=LHmzU*yv*tiSfHuYqY*9s?2X9&l_X^uo1LV}?%5R1AH{&uLrC*^_j(L{VVEEK?V+KDjA3hqYuhX7gwGwuRK>)ajv5BLPga(>Sz~KO5(5i&fq@3Nt#Z^_}#)Q2N#SldrR?0e~#7WhGWU2|Wl5C*k>XwA^Bd9qu z^b)cngG!9)3OAEcG{6qM#8VF2kmPg?tS0gkuz+k;?uOnjFbvw3lBpA^^jVrMoAGw4 z;6f^SKAF1U6yj1~`%56=c;mnlMFG<5D)mF7FJO@vk|vvNr9t(*+fATX-2Wu2V|0_j zWJ2Z=r{c&2wprq=n>s5!fwqVv7AJA$Jm>BA_~W&5^!)ynIN9N zc79b*Bb>5Eu_@t?kqO%SP!R^s`S}n%*CIx$$eD8;NrqxI258OPnI9z(FJ>eZfN4xb zT$b2sbVisd&jiO&h%|;k%Eso#+(k+qdyAd;JlAcDqAHLWsS`Yp2Bq+#^}%3+WF^uV zh%`roZPD0FibNfEi3s~>u$$hPEX<^eUN0otilsE=bOliG>Wfn#7M%LqD#-h5Kdx^2 zqQ2#u8SOtc!@W8CukDOk|1@*XUuSjv^Q`tiwYGfQSocXy<;g0Fjs*C4(Xk7+@~|C( z_Tm*=s|jbq#O1`pfr&*Q8QQfr{?~@q-#1P_T}6F{&iD z8-}_dEF~(#bLFszh`6Wd7lUiP%T?d{gDT5l%DX3`iPKKmUGlr;4wiH(EZB=op5 z&xt?l&>RBe-G$+dj6DTTO@U}#I8vK+wu#F!O_arJ3L{vNwwRiJK`BX3FR=GeU#yF# zUP;hNqGV5L#nI9!CrhSYtOR>8<#!cRKdP+0Rt1$byVTn(DV7NUwtgI6KmL*N|{IPkd^0zwP`D@m^L zh*p)qSJLoNZT*L}6_{$jP6;J}gAFIgb1)t#3dd0N33C0_L9lsno3Z@3tAcQ5YAwOR zCNpRXZBeCscCasPGiMeR6`>i--~9RW^~{jAut>p0OX)WgU(r*Nahr0MkMXNfUKa4m z0TK?2fHK>zD|li=MHR$olT2-}<)CRveGlZ2iE4i5;Ka&sVi4&Dp+tyxz=7aDvD;1U zOC%4((G%1u#YZs192{)xOvpBS+r?!;GBBcC15?Lvat=ge#?!{xwvGAhMr=1U3nHh& zpo$vGLZnj?^Q#z-@;rJCApK4oj`noH?)u^lkH+gu+FmJW&=?0 zx#CaE@QMEwnOFpz)Jd)Jk}DI57d-j2v&DoNXz?Qim`O+j8ihw8SBTUSpf#KynnV~~ zV7bW^l7HTkpmS>Rp3?FoB}7=Kyi;EN;Z!)oYrdFXf2XeHZu87Pw_!3N4b68Oo4#wn zh-&(#q3P>}rdy45pI1-)u#(tRdV7j#PS`C(&I5u882D}opGv> z0EYD*tdvX;1wl}hpHK=9fk@Gn11fYtDUgZrbCHqrnLML7>65NHTAECPWr2~UN`V9ck~ znPlp0s^DT0nLr!G&pYT0yb4?jMV}5wgfgK%oS-i*h3VVMBX}3u-Xjy?H^T6%AsLcR z5HJLWe?|B4{1q6b+d&6!dQ^A(#n6(neLe)^r zXvu__%s_3C<@T0%Ohe!Xg`1PfRdMQYM1n*=)Yh=1K{A2K#CC{$hBA6=V}rE<$&EOj z5iBlPlEB<)o$F7Q?k%YrESJ^v;nar9bmIoGK=2Cec2-eFBC%6{01{s4YKeH=5>dn;ai zu?&G|c&EN{s1i0qnIJ1Yhm7~4kZ++s1fv_RSm|-Zy8aR=qM$lN8`57bQ~=Z|&1w$` z{$j$_vE~RGi0Pl@@{$~MbaWJ?42ci5r#KMk3F9ar2XRIxLg)F1MrI9F3{~;9smx};#iqD;VL0P z6gD?7pW)R0D;AO6Q3g80+2iODYYGdB@^!jU z0$HbbDPdGZ0m@jT5@Z6I0wiM*sWwSrS~}TH80vQavU=WQZYk`Fg6R&Cqh^FwMg!3H zuL)|b4j^Ij&zFX=qd=9TEhHj@o@_bOl7LD=>3U_^xhbT8#1h2-!h;Bs!Hr`PIWuF4 z=W-y4-gJ6%k`e?G8wBWqNn%ByArJ+I%C<4O%r;`d#8H}Wu4E#y%t5M>FDF1+l?v30&xb=!?f%OL$WT0h~V$#>>_yys0}ML_K~M{ATCxKaHk0_0az3^7-`s=g8kNV z2oj7I9WzRTMSr&|TLlkN^$S;~2HU2B8lEQ+O@JT-7`k2V#@x#APF4I^gD?;?4|Aym{+vC{E2l29YR{dlrTx_(BQl) z=qH-kWq?w~V-#iOGXcte{LlK83@7dg#bqbu4q2JVx6?pdkeq-R3~gQbR$=*`vcgOO zzcSVO^lYTMLMD^E@t~qlplR{C!Kl|MG>fke3SGsTi=pZE;t<=v>X9!4q6!NvnYda4 zuD9#)oRNv9@0EmrdS=G0`ufjn8?IDWe=>dQ@2e`_t}GrbVP#Lek`VfjNCBP}IRor5 z!l;1tu$@eRKs;0=>@Wgh1T{!mAwMXvp!xP2aUfNV@7FcI*9;L4jet?EM5ECw?lX~i z7cIN8?KHD$SbW*Con(0uqr;@6iAZCRyge9V-i1oT)P{s{BlKXj#VcF4u3NXRw!S{A zKCDG05n(I$B!9~PVZ$E&^chcF=J69NlUaq8y=?vxj}ObeA$unOSvk($=FhzT#PQ}} z(Ou^EW=}WIEj}av(vusHPkCxTFWGBOJjQHicjy3nmi@d>Uco-g9%Jw4sl#;r$G2^d z%|A}K+%KKZN6TL`f2qe8G;3k*9=TlhZYNwgyJ^hr>^2vKkWm-$Lo4c^$SG=$F!yI~ zGJkzN&3_Q{Zx!r+1)(T>)C(iKy90kZ_j)3 zal&WQTlfwhICS{%;r{;q&Q5yv>1}iGlRa$Y}h;0@eKRa zzK@>St;dJkgY&y3J-I(~1^Yy2+4q`w8@U4~{kL!N_&Vm?ypZJ%p7^?X``nf7>E=#z z$Lhb|S6p1&+S1n2(vm7re_eC@Z{B|A_rLqyZ{B_P=;1?~w`^JV{PUd)7R>9Mi|@@} zM{d}s{~wQ?9rNbT?VM*v?tJ9WUOBfT`+x4-xgDJvllJQ=J16a*c+IEYB76L#Q-0>U zKX>H`hjB|2o;}{2|9Erq7N3q?6HjED`Ns)g`>=oZyw3dbIxPE&`7$SN=dP)JopU?q>#gl=FI=$bh3A*8U9+yI zr)THRov&lsk!~*HLbm`KU*RNmy?6Xfk`oo3u7l-ZN;NZ~z_21zC%fb1p9v>QF z3=Ixv_Y6Pzj_kK1f5ItwI+OZ$KJ4JPg6TzmT9@Xx-r zcAAel`PISUU-}ffRr{B}xc$68_f7I+>e%7IV+`|!Kk=PsFZB2y$NbVCSKy_mPiK#I znzzuo<~EKFA32`CAA10=m=F8(TX2&TcTU_MvG;E-%YN>G%j(MZTh;qz|FqM5X?kw9 vIcAVEhK@7Xm;EYohYb!7kBp3*K7IP)#fzuUo;`T*;I?hs$Yv)J_!j>g;gdLm literal 0 HcmV?d00001 diff --git a/plugins/GSdx/res/merge10.fx b/plugins/GSdx/res/merge10.fx new file mode 100644 index 0000000000..1f91fe3e8f --- /dev/null +++ b/plugins/GSdx/res/merge10.fx @@ -0,0 +1,27 @@ +Texture2D Texture; +SamplerState Sampler; + +cbuffer cb0 +{ + float4 BGColor; +}; + +struct PS_INPUT +{ + float4 p : SV_Position; + float2 t : TEXCOORD0; +}; + +float4 ps_main0(PS_INPUT input) : SV_Target0 +{ + float4 c = Texture.Sample(Sampler, input.t); + c.a = min(c.a * 2, 1); + return c; +} + +float4 ps_main1(PS_INPUT input) : SV_Target0 +{ + float4 c = Texture.Sample(Sampler, input.t); + c.a = BGColor.a; + return c; +} \ No newline at end of file diff --git a/plugins/GSdx/res/merge9.fx b/plugins/GSdx/res/merge9.fx new file mode 100644 index 0000000000..dbb210660f --- /dev/null +++ b/plugins/GSdx/res/merge9.fx @@ -0,0 +1,25 @@ +sampler Texture : register(s0); + +float4 g_params[1]; + +#define BGColor (g_params[0]) + +struct PS_INPUT +{ + float2 t : TEXCOORD0; +}; + +float4 ps_main0(PS_INPUT input) : COLOR +{ + float4 c = tex2D(Texture, input.t); + // a = ; + return c.bgra; +} + +float4 ps_main1(PS_INPUT input) : COLOR +{ + float4 c = tex2D(Texture, input.t); + c.a = BGColor.a; + return c.bgra; +} + diff --git a/plugins/GSdx/res/tfx10.fx b/plugins/GSdx/res/tfx10.fx new file mode 100644 index 0000000000..7be092dfd7 --- /dev/null +++ b/plugins/GSdx/res/tfx10.fx @@ -0,0 +1,501 @@ +cbuffer cb0 +{ + float4 VertexScale; + float4 VertexOffset; + float2 TextureScale; +}; + +struct VS_INPUT +{ + uint2 p : POSITION0; + uint z : POSITION1; + float2 t : TEXCOORD0; + float q : TEXCOORD1; + float4 c : COLOR0; + float4 f : COLOR1; +}; + +struct VS_OUTPUT +{ + float4 p : SV_Position; + float4 t : TEXCOORD0; + float4 c : COLOR0; +}; + +#ifndef VS_BPP +#define VS_BPP 0 +#define VS_BPPZ 0 +#define VS_TME 1 +#define VS_FST 1 +#define VS_PRIM 0 +#endif + +VS_OUTPUT vs_main(VS_INPUT input) +{ + if(VS_BPPZ == 1) // 24 + { + input.z = input.z & 0xffffff; + } + else if(VS_BPPZ == 2) // 16 + { + input.z = input.z & 0xffff; + } + + if(VS_PRIM == 6) // sprite + { + //input.p.xy = (input.p.xy + 15) & ~15; // HACK + } + + VS_OUTPUT output; + + float4 p = float4(input.p, input.z, 0); + + // FIXME: + // A litte accuracy problem in many games where the screen is copied in columns and the sides have a half pixel gap for some reason + // (probably to avoid page breaks with linear filtering). That half pixel coordinate gets multiplied by 2 (VertexScale) and occasionally + // ends on .9999999, which the rasterizer floors to 1 less pixel we need, leaving a visible gap after drawing. + + p += float4(0.5f, 0.5f, 0, 0); // add 1/32 pixel + + output.p = p * VertexScale - VertexOffset; + + if(VS_TME == 1) + { + if(VS_FST == 1) + { + output.t.xy = input.t * TextureScale; + output.t.w = 1.0f; + } + else + { + output.t.xy = input.t; + output.t.w = input.q; + } + } + else + { + output.t.xy = 0; + output.t.w = 1.0f; + } + + output.c = input.c; + output.t.z = input.f.a; + + return output; +} + +#ifndef IIP +#define IIP 0 +#define PRIM 3 +#endif + +#if PRIM == 0 + +[maxvertexcount(1)] +void gs_main(point VS_OUTPUT input[1], inout PointStream stream) +{ + stream.Append(input[0]); +} + +#elif PRIM == 1 + +[maxvertexcount(2)] +void gs_main(line VS_OUTPUT input[2], inout LineStream stream) +{ + #if IIP == 0 + input[0].c = input[1].c; + #endif + + stream.Append(input[0]); + stream.Append(input[1]); +} + +#elif PRIM == 2 + +[maxvertexcount(3)] +void gs_main(triangle VS_OUTPUT input[3], inout TriangleStream stream) +{ + #if IIP == 0 + input[0].c = input[2].c; + input[1].c = input[2].c; + #endif + + stream.Append(input[0]); + stream.Append(input[1]); + stream.Append(input[2]); +} + +#elif PRIM == 3 + +[maxvertexcount(4)] +void gs_main(line VS_OUTPUT input[2], inout TriangleStream stream) +{ + input[0].p.z = input[1].p.z; + input[0].t.zw = input[1].t.zw; + + #if IIP == 0 + input[0].c = input[1].c; + #endif + + VS_OUTPUT lb = input[1]; + + lb.p.x = input[0].p.x; + lb.t.x = input[0].t.x; + + VS_OUTPUT rt = input[1]; + + rt.p.y = input[0].p.y; + rt.t.y = input[0].t.y; + + stream.Append(input[0]); + stream.Append(lb); + stream.Append(rt); + stream.Append(input[1]); +} + +#endif + +Texture2D Texture; +Texture2D Palette; +SamplerState TextureSampler; +SamplerState PaletteSampler; + +cbuffer cb1 +{ + float4 FogColor; + float MINU; + float MAXU; + float MINV; + float MAXV; + uint UMSK; + uint UFIX; + uint VMSK; + uint VFIX; + float TA0; + float TA1; + float AREF; + float _pad; + float2 WH; + float2 rWrH; +}; + +struct PS_INPUT +{ + float4 p : SV_Position; + float4 t : TEXCOORD0; + float4 c : COLOR0; +}; + +struct PS_OUTPUT +{ + float4 c0 : SV_Target0; + float4 c1 : SV_Target1; +}; + +#ifndef FST +#define FST 0 +#define WMS 3 +#define WMT 3 +#define BPP 0 +#define AEM 0 +#define TFX 0 +#define TCC 1 +#define ATE 0 +#define ATST 2 +#define FOG 0 +#define CLR1 0 +#define FBA 0 +#define AOUT 0 +#endif + +float4 Normalize16(float4 f) +{ + return f / float4(0x001f, 0x03e0, 0x7c00, 0x8000); +} + +float4 Extract16(uint i) +{ + float4 f; + + f.r = i & 0x001f; + f.g = i & 0x03e0; + f.b = i & 0x7c00; + f.a = i & 0x8000; + + return f; +} + +int repeatu(int tc) +{ + return WMS == 3 ? ((tc & UMSK) | UFIX) : tc; +} + +int repeatv(int tc) +{ + return WMT == 3 ? ((tc & VMSK) | VFIX) : tc; +} + +float4 sample(float2 tc) +{ + float4 t; + + // if(WMS >= 2 || WMT >= 2) + if(WMS >= 3 || WMT >= 3) + { + int4 itc = tc.xyxy * WH.xyxy; + + float4 tc01; + + tc01.x = repeatu(itc.x); + tc01.y = repeatv(itc.y); + tc01.z = repeatu(itc.z + 1); + tc01.w = repeatv(itc.w + 1); + + tc01 *= rWrH.xyxy; + + float4 t00 = Texture.Sample(TextureSampler, tc01.xy); + float4 t01 = Texture.Sample(TextureSampler, tc01.zy); + float4 t10 = Texture.Sample(TextureSampler, tc01.xw); + float4 t11 = Texture.Sample(TextureSampler, tc01.zw); + + float2 dd = frac(tc * WH); + + t = lerp(lerp(t00, t01, dd.x), lerp(t10, t11, dd.x), dd.y); + } + else + { + t = Texture.Sample(TextureSampler, tc); + } + + return t; +} + +float4 sample8hp(float2 tc) +{ + float4 tc01; + + // if(WMS >= 2 || WMT >= 2) + if(WMS >= 3 || WMT >= 3) + { + int4 itc = tc.xyxy * WH.xyxy; + + tc01.x = repeatu(itc.x); + tc01.y = repeatv(itc.y); + tc01.z = repeatu(itc.z + 1); + tc01.w = repeatv(itc.w + 1); + + tc01 *= rWrH.xyxy; + } + else + { + tc01.x = tc.x; + tc01.y = tc.y; + tc01.z = tc.x + rWrH.x; + tc01.w = tc.y + rWrH.y; + } + + float4 t; + + t.x = Texture.Sample(TextureSampler, tc01.xy).a; + t.y = Texture.Sample(TextureSampler, tc01.zy).a; + t.z = Texture.Sample(TextureSampler, tc01.xw).a; + t.w = Texture.Sample(TextureSampler, tc01.zw).a; + + float4 t00 = Palette.Sample(PaletteSampler, t.x); + float4 t01 = Palette.Sample(PaletteSampler, t.y); + float4 t10 = Palette.Sample(PaletteSampler, t.z); + float4 t11 = Palette.Sample(PaletteSampler, t.w); + + float2 dd = frac(tc * WH); + + return lerp(lerp(t00, t01, dd.x), lerp(t10, t11, dd.x), dd.y); +} + +float4 sample16p(float2 tc) +{ + float4 t; + + float4 tc01; + + // if(WMS >= 2 || WMT >= 2) + if(WMS >= 3 || WMT >= 3) + { + int4 itc = tc.xyxy * WH.xyxy; + + tc01.x = repeatu(itc.x); + tc01.y = repeatv(itc.y); + tc01.z = repeatu(itc.z + 1); + tc01.w = repeatv(itc.w + 1); + + tc01 *= rWrH.xyxy; + } + else + { + tc01.x = tc.x; + tc01.y = tc.y; + tc01.z = tc.x + rWrH.x; + tc01.w = tc.y + rWrH.y; + } + + t.x = Texture.Sample(TextureSampler, tc01.xy).r; + t.y = Texture.Sample(TextureSampler, tc01.zy).r; + t.z = Texture.Sample(TextureSampler, tc01.xw).r; + t.w = Texture.Sample(TextureSampler, tc01.zw).r; + + uint4 i = t * 65535; + + float4 t00 = Extract16(i.x); + float4 t01 = Extract16(i.y); + float4 t10 = Extract16(i.z); + float4 t11 = Extract16(i.w); + + float2 dd = frac(tc * WH); + + return Normalize16(lerp(lerp(t00, t01, dd.x), lerp(t10, t11, dd.x), dd.y)); +} + +PS_OUTPUT ps_main(PS_INPUT input) +{ + float2 tc = input.t.xy; + + if(FST == 0) + { + tc /= input.t.w; + } + + tc -= rWrH / 2; + + if(WMS == 2) + { + tc.x = clamp(tc.x, MINU, MAXU); + } + + if(WMT == 2) + { + tc.y = clamp(tc.y, MINV, MAXV); + } + + float4 t; + + if(BPP == 0) // 32 + { + t = sample(tc); + } + else if(BPP == 1) // 24 + { + t = sample(tc); + + t.a = AEM == 0 || any(t.rgb) ? TA0 : 0; + } + else if(BPP == 2) // 16 + { + t = sample(tc); + + t.a = t.a >= 0.5 ? TA1 : AEM == 0 || any(t.rgb) ? TA0 : 0; // a bit incompatible with up-scaling because the 1 bit alpha is interpolated + } + else if(BPP == 3) // 8HP / 32-bit palette + { + t = sample8hp(tc); + } + else if(BPP == 4) // 8HP / 16-bit palette + { + // TODO: yuck, just pre-convert the palette to 32-bit + } + else if(BPP == 5) // 16P + { + t = sample16p(tc); + + t.a = t.a >= 0.5 ? TA1 : AEM == 0 || any(t.rgb) ? TA0 : 0; // a bit incompatible with up-scaling because the 1 bit alpha is interpolated + } + + float4 c = input.c; + + if(TFX == 0) + { + if(TCC == 0) + { + c.rgb = c.rgb * t.rgb * 2; + } + else + { + c = c * t * 2; + } + } + else if(TFX == 1) + { + c = t; + } + else if(TFX == 2) + { + c.rgb = c.rgb * t.rgb * 2 + c.a; + + if(TCC == 1) + { + c.a += t.a; + } + } + else if(TFX == 3) + { + c.rgb = c.rgb * t.rgb * 2 + c.a; + + if(TCC == 1) + { + c.a = t.a; + } + } + + c = saturate(c); + + if(ATE == 1) + { + if(ATST == 0) + { + discard; + } + else if(ATST == 2 || ATST == 3) // l, le + { + clip(AREF - c.a); + } + else if(ATST == 4) // e + { + clip(0.9f/256 - abs(c.a - AREF)); + } + else if(ATST == 5 || ATST == 6) // ge, g + { + clip(c.a - AREF); + } + else if(ATST == 7) // ne + { + clip(abs(c.a - AREF) - 0.9f/256); + } + } + + if(FOG == 1) + { + c.rgb = lerp(FogColor.rgb, c.rgb, input.t.z); + } + + if(CLR1 == 1) // needed for Cd * (As/Ad/F + 1) blending modes + { + c.rgb = 1; + } + + PS_OUTPUT output; + + output.c1 = c.a * 2; // used for alpha blending + + if(AOUT == 1) // 16 bit output + { + float a = 128.0f / 255; // alpha output will be 0x80 + + c.a = FBA == 1 ? a : step(0.5, c.a) * a; + } + else if(FBA == 1) + { + if(c.a < 0.5) c.a += 0.5; + } + + output.c0 = c; + + return output; +} diff --git a/plugins/GSdx/res/tfx9.fx b/plugins/GSdx/res/tfx9.fx new file mode 100644 index 0000000000..b7f6dba519 --- /dev/null +++ b/plugins/GSdx/res/tfx9.fx @@ -0,0 +1,313 @@ +struct VS_INPUT +{ + float4 p : POSITION0; + float2 t : TEXCOORD0; + float4 c : COLOR0; + float4 f : COLOR1; +}; + +struct VS_OUTPUT +{ + float4 p : POSITION; + float4 t : TEXCOORD0; + float4 c : COLOR0; +}; + +float4 vs_params[3]; + +#define VertexScale vs_params[0] +#define VertexOffset vs_params[1] +#define TextureScale vs_params[2].xy + +#ifndef VS_BPPZ +#define VS_BPPZ 0 +#define VS_TME 1 +#define VS_FST 1 +#define VS_LOGZ 1 +#endif + +VS_OUTPUT vs_main(VS_INPUT input) +{ + if(VS_BPPZ == 1) // 24 + { + input.p.z = fmod(input.p.z, 0x1000000); + } + else if(VS_BPPZ == 2) // 16 + { + input.p.z = fmod(input.p.z, 0x10000); + } + + VS_OUTPUT output; + + output.p = input.p * VertexScale - VertexOffset; + + if(VS_LOGZ == 1) + { + output.p.z = log2(1.0f + input.p.z) / 32; + } + + if(VS_TME == 1) + { + if(VS_FST == 1) + { + output.t.xy = input.t * TextureScale; + output.t.w = 1.0f; + } + else + { + output.t.xy = input.t; + output.t.w = input.p.w; + } + } + else + { + output.t.xy = 0; + output.t.w = 1.0f; + } + + output.c = input.c; + output.t.z = input.f.a; + + return output; +} + +float4 ps_params[5]; + +#define FogColor ps_params[0].bgra +#define MINU ps_params[1].x +#define MAXU ps_params[1].y +#define MINV ps_params[1].z +#define MAXV ps_params[1].w +#define UMSK ps_params[2].x +#define UFIX ps_params[2].y +#define VMSK ps_params[2].z +#define VFIX ps_params[2].w +#define TA0 ps_params[3].x +#define TA1 ps_params[3].y +#define AREF ps_params[3].z +#define WH ps_params[4].xy +#define rWrH ps_params[4].zw + +struct PS_INPUT +{ + float4 t : TEXCOORD0; + float4 c : COLOR0; +}; + +#ifndef FST +#define FST 0 +#define WMS 3 +#define WMT 3 +#define BPP 0 +#define AEM 0 +#define TFX 0 +#define TCC 1 +#define ATE 0 +#define ATST 0 +#define FOG 0 +#define CLR1 0 +#define RT 0 +#endif + +sampler Texture : register(s0); +sampler1D Palette : register(s1); +sampler1D UMSKFIX : register(s2); +sampler1D VMSKFIX : register(s3); + +float repeatu(float tc) +{ + return WMS == 3 ? tex1D(UMSKFIX, tc) : tc; +} + +float repeatv(float tc) +{ + return WMT == 3 ? tex1D(VMSKFIX, tc) : tc; +} + +float4 sample(float2 tc) +{ + float4 t; + + // if(WMS >= 2 || WMT >= 2) + if(WMS >= 3 || WMT >= 3) + { + tc -= rWrH / 2; + + int4 itc = tc.xyxy * WH.xyxy; + + float4 tc01; + + tc01.x = repeatu(itc.x); + tc01.y = repeatv(itc.y); + tc01.z = repeatu(itc.z + 1); + tc01.w = repeatv(itc.w + 1); + + tc01 *= rWrH.xyxy; + + float4 t00 = tex2D(Texture, tc01.xy); + float4 t01 = tex2D(Texture, tc01.zy); + float4 t10 = tex2D(Texture, tc01.xw); + float4 t11 = tex2D(Texture, tc01.zw); + + float2 dd = frac(tc * WH); + + t = lerp(lerp(t00, t01, dd.x), lerp(t10, t11, dd.x), dd.y); + } + else + { + t = tex2D(Texture, tc); + } + + return t; +} + +float4 sample8hp(float2 tc) +{ + tc -= rWrH / 2; + + float4 tc01; + + tc01.x = tc.x; + tc01.y = tc.y; + tc01.z = tc.x + rWrH.x; + tc01.w = tc.y + rWrH.y; + + float4 t; + + t.x = tex2D(Texture, tc01.xy).a; + t.y = tex2D(Texture, tc01.zy).a; + t.z = tex2D(Texture, tc01.xw).a; + t.w = tex2D(Texture, tc01.zw).a; + + if(RT == 1) t *= 0.5; + + float4 t00 = tex1D(Palette, t.x); + float4 t01 = tex1D(Palette, t.y); + float4 t10 = tex1D(Palette, t.z); + float4 t11 = tex1D(Palette, t.w); + + float2 dd = frac(tc * WH); + + return lerp(lerp(t00, t01, dd.x), lerp(t10, t11, dd.x), dd.y); +} + +float4 ps_main(PS_INPUT input) : COLOR +{ + float2 tc = input.t.xy; + + if(FST == 0) + { + tc /= input.t.w; + } + + if(WMS == 2) + { + tc.x = clamp(tc.x, MINU, MAXU); + } + + if(WMT == 2) + { + tc.y = clamp(tc.y, MINV, MAXV); + } + + float4 t; + + if(BPP == 0) // 32 + { + t = sample(tc); + + if(RT == 1) t.a *= 0.5; + } + else if(BPP == 1) // 24 + { + t = sample(tc); + + t.a = AEM == 0 || any(t.rgb) ? TA0 : 0; + } + else if(BPP == 2) // 16 + { + t = sample(tc); + + t.a = t.a >= 0.5 ? TA1 : AEM == 0 || any(t.rgb) ? TA0 : 0; // a bit incompatible with up-scaling because the 1 bit alpha is interpolated + } + else if(BPP == 3) // 8HP ln + { + t = sample8hp(tc); + } + + float4 c = input.c; + + if(TFX == 0) + { + if(TCC == 0) + { + c.rgb = c.rgb * t.rgb * 2; + } + else + { + c = c * t * 2; + } + } + else if(TFX == 1) + { + c = t; + } + else if(TFX == 2) + { + c.rgb = c.rgb * t.rgb * 2 + c.a; + + if(TCC == 1) + { + c.a += t.a; + } + } + else if(TFX == 3) + { + c.rgb = c.rgb * t.rgb * 2 + c.a; + + if(TCC == 1) + { + c.a = t.a; + } + } + + c = saturate(c); + + if(ATE == 1) + { + if(ATST == 0) + { + discard; + } + else if(ATST == 2 || ATST == 3) // l, le + { + clip(AREF - c.a); + } + else if(ATST == 4) // e + { + clip(0.9f/256 - abs(c.a - AREF)); + } + else if(ATST == 5 || ATST == 6) // ge, g + { + clip(c.a - AREF); + } + else if(ATST == 7) // ne + { + clip(abs(c.a - AREF) - 0.9f/256); + } + } + + if(FOG == 1) + { + c.rgb = lerp(FogColor.rgb, c.rgb, input.t.z); + } + + if(CLR1 == 1) // needed for Cd * (As/Ad/F + 1) blending modes + { + c.rgb = 1; + } + + c.a *= 2; + + return c; +} diff --git a/plugins/GSdx/resource.h b/plugins/GSdx/resource.h new file mode 100644 index 0000000000..c2c41e76a1 --- /dev/null +++ b/plugins/GSdx/resource.h @@ -0,0 +1,55 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by GSdx.rc +// +#define IDC_CHECK1 2001 +#define IDC_CHECK2 2002 +#define IDC_CHECK3 2003 +#define IDC_CHECK5 2004 +#define IDC_CHECK6 2005 +#define IDC_COMBO1 2006 +#define IDC_COMBO3 2007 +#define IDC_COMBO4 2008 +#define IDC_EDIT1 2009 +#define IDC_EDIT2 2010 +#define IDC_BUTTON1 2011 +#define IDC_BUTTON2 2012 +#define IDC_EDIT3 2012 +#define IDC_CUSTOM1 2013 +#define IDC_CHECK4 2014 +#define IDC_COMBO2 2015 +#define IDC_COMBO5 2016 +#define IDC_RADIO1 2017 +#define IDC_COMBO6 2017 +#define IDC_SPIN1 2018 +#define IDC_COMBO7 2018 +#define IDC_SPIN2 2019 +#define IDD_CONFIG 2020 +#define IDC_SPIN3 2020 +#define IDB_LOGO9 2021 +#define IDB_LOGO10 2022 +#define IDC_CHECK7 2023 +#define IDC_LOGO9 2024 +#define IDC_LOGO10 2025 +#define IDD_CAPTURE 2026 +#define IDC_EDIT4 2027 +#define IDD_GPUCONFIG 2027 +#define IDR_CONVERT9_FX 10000 +#define IDR_TFX9_FX 10001 +#define IDR_MERGE9_FX 10002 +#define IDR_INTERLACE9_FX 10003 +#define IDR_CONVERT10_FX 10004 +#define IDR_TFX10_FX 10005 +#define IDR_MERGE10_FX 10006 +#define IDR_INTERLACE10_FX 10007 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 10009 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 2028 +#define _APS_NEXT_SYMED_VALUE 5000 +#endif +#endif diff --git a/plugins/GSdx/sse.h b/plugins/GSdx/sse.h new file mode 100644 index 0000000000..4efc8a96e0 --- /dev/null +++ b/plugins/GSdx/sse.h @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2007-2009 Gabest + * http://www.gabest.org + * + * 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; either version 2, or (at your option) + * any later version. + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#pragma once + +// sse2 + +#if _M_SSE >= 0x200 + + #include + #include + + #ifndef _MM_DENORMALS_ARE_ZERO + #define _MM_DENORMALS_ARE_ZERO 0x0040 + #endif + + #define MXCSR (_MM_DENORMALS_ARE_ZERO | _MM_MASK_MASK | _MM_ROUND_NEAREST | _MM_FLUSH_ZERO_ON) + + #if _MSC_VER < 1500 + + __forceinline __m128i _mm_castps_si128(__m128 a) {return *(__m128i*)&a;} + __forceinline __m128 _mm_castsi128_ps(__m128i a) {return *(__m128*)&a;} + __forceinline __m128i _mm_castpd_si128(__m128d a) {return *(__m128i*)&a;} + __forceinline __m128d _mm_castsi128_pd(__m128i a) {return *(__m128d*)&a;} + __forceinline __m128d _mm_castps_pd(__m128 a) {return *(__m128d*)&a;} + __forceinline __m128 _mm_castpd_ps(__m128d a) {return *(__m128*)&a;} + + #endif + + const __m128 ps_3f800000 = _mm_castsi128_ps(_mm_set1_epi32(0x3f800000)); + const __m128 ps_4b000000 = _mm_castsi128_ps(_mm_set1_epi32(0x4b000000)); + const __m128 ps_7fffffff = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)); + const __m128 ps_80000000 = _mm_castsi128_ps(_mm_set1_epi32(0x80000000)); + const __m128 ps_ffffffff = _mm_castsi128_ps(_mm_set1_epi32(0xffffffff)); + + __forceinline __m128 _mm_neg_ps(__m128 r) + { + return _mm_xor_ps(ps_80000000, r); + } + + __forceinline __m128 _mm_abs_ps(__m128 r) + { + return _mm_and_ps(ps_7fffffff, r); + } + + #define _MM_TRANSPOSE4_SI128(row0, row1, row2, row3) \ + { \ + __m128 tmp0 = _mm_shuffle_ps(_mm_castsi128_ps(row0), _mm_castsi128_ps(row1), 0x44); \ + __m128 tmp2 = _mm_shuffle_ps(_mm_castsi128_ps(row0), _mm_castsi128_ps(row1), 0xEE); \ + __m128 tmp1 = _mm_shuffle_ps(_mm_castsi128_ps(row2), _mm_castsi128_ps(row3), 0x44); \ + __m128 tmp3 = _mm_shuffle_ps(_mm_castsi128_ps(row2), _mm_castsi128_ps(row3), 0xEE); \ + (row0) = _mm_castps_si128(_mm_shuffle_ps(tmp0, tmp1, 0x88)); \ + (row1) = _mm_castps_si128(_mm_shuffle_ps(tmp0, tmp1, 0xDD)); \ + (row2) = _mm_castps_si128(_mm_shuffle_ps(tmp2, tmp3, 0x88)); \ + (row3) = _mm_castps_si128(_mm_shuffle_ps(tmp2, tmp3, 0xDD)); \ + } + + __forceinline __m128 _mm_rcpnr_ps(__m128 r) + { + __m128 t = _mm_rcp_ps(r); + + return _mm_sub_ps(_mm_add_ps(t, t), _mm_mul_ps(_mm_mul_ps(t, t), r)); + } + + +#else + +#error TODO: GSVector4 and GSRasterizer needs SSE2 + +#endif + +// sse3 + +#if _M_SSE >= 0x301 + + #include + +#endif + +// sse4 + +#if _M_SSE >= 0x401 + + #include + +#else + + // not an equal replacement for sse4's blend but for our needs it is ok + + #define _mm_blendv_ps(a, b, mask) _mm_or_ps(_mm_andnot_ps(mask, a), _mm_and_ps(mask, b)) + #define _mm_blendv_epi8(a, b, mask) _mm_or_si128(_mm_andnot_si128(mask, a), _mm_and_si128(mask, b)) + + __forceinline __m128 _mm_round_ps(__m128 x) + { + __m128 t = _mm_or_ps(_mm_and_ps(ps_80000000, x), ps_4b000000); + + return _mm_sub_ps(_mm_add_ps(x, t), t); + } + + __forceinline __m128 _mm_floor_ps(__m128 x) + { + __m128 t = _mm_round_ps(x); + + return _mm_sub_ps(t, _mm_and_ps(_mm_cmplt_ps(x, t), ps_3f800000)); + } + + __forceinline __m128 _mm_ceil_ps(__m128 x) + { + __m128 t = _mm_round_ps(x); + + return _mm_add_ps(t, _mm_and_ps(_mm_cmpgt_ps(x, t), ps_3f800000)); + } + +#endif diff --git a/plugins/GSdx/stdafx.cpp b/plugins/GSdx/stdafx.cpp new file mode 100644 index 0000000000..3832007ce8 --- /dev/null +++ b/plugins/GSdx/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// GSdx.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/plugins/GSdx/stdafx.h b/plugins/GSdx/stdafx.h new file mode 100644 index 0000000000..7a3b2d9fd4 --- /dev/null +++ b/plugins/GSdx/stdafx.h @@ -0,0 +1,107 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently + +#pragma once + +#pragma warning(disable: 4996) + +#ifndef VC_EXTRALEAN +#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers +#endif + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later. +#define WINVER 0x0510 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later. +#endif + +#ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later. +#define _WIN32_WINNT 0x0400 // Change this to the appropriate value to target Windows 2000 or later. +#endif + +#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. +#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. +#endif + +#ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later. +#define _WIN32_IE 0x0400 // Change this to the appropriate value to target IE 5.0 or later. +#endif + +#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit + +#include // MFC core and standard components +//#include // MFC extensions +#ifndef _AFX_NO_AFXCMN_SUPPORT +#include // MFC support for Windows Common Controls +#endif // _AFX_NO_AFXCMN_SUPPORT +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(_M_SSE) && (defined(_M_AMD64) || defined(_M_IX86_FP) && _M_IX86_FP >= 2) +#define _M_SSE 0x200 +#endif + +#include "sse.h" + +#define countof(a) (sizeof(a)/sizeof(a[0])) + +#define EXPORT_C extern "C" __declspec(dllexport) void __stdcall +#define EXPORT_C_(type) extern "C" __declspec(dllexport) type __stdcall + +#ifndef RESTRICT + #ifdef __INTEL_COMPILER + #define RESTRICT restrict + #elif _MSC_VER >= 1400 + #define RESTRICT __restrict + #else + #define RESTRICT + #endif +#endif + +#pragma warning(disable : 4995 4324 4100) + +#define D3DCOLORWRITEENABLE_RGB (D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE) +#define D3DCOLORWRITEENABLE_RGBA (D3DCOLORWRITEENABLE_RGB|D3DCOLORWRITEENABLE_ALPHA) + +#define QI(i) (riid == __uuidof(i)) ? GetInterface((i*)this, ppv) : + +template class CRBMapC : public CRBMap +{ + // CRBMap + a cache for the last value (simple, but already a lot better) + + CPair* m_pair; + +public: + CRBMapC() : m_pair(NULL) {} + + CPair* Lookup(KINARGTYPE key) + { + if(m_pair && key == m_pair->m_key) + { + return m_pair; + } + + m_pair = __super::Lookup(key); + + return m_pair; + } + + POSITION SetAt(KINARGTYPE key, VINARGTYPE value) + { + POSITION pos = __super::SetAt(key, value); + + m_pair = __super::GetAt(pos); + + return pos; + } +}; + diff --git a/plugins/GSdx/svnrev_template.h b/plugins/GSdx/svnrev_template.h new file mode 100644 index 0000000000..678f7916d3 --- /dev/null +++ b/plugins/GSdx/svnrev_template.h @@ -0,0 +1,2 @@ +#define SVN_REV $WCREV$ +#define SVN_MODS $WCMODS?1:0$ \ No newline at end of file diff --git a/plugins/GSdx/vsprops/common.vsprops b/plugins/GSdx/vsprops/common.vsprops new file mode 100644 index 0000000000..164f159de5 --- /dev/null +++ b/plugins/GSdx/vsprops/common.vsprops @@ -0,0 +1,32 @@ + + + + + + diff --git a/plugins/GSdx/vsprops/debug.vsprops b/plugins/GSdx/vsprops/debug.vsprops new file mode 100644 index 0000000000..9d5c60bf96 --- /dev/null +++ b/plugins/GSdx/vsprops/debug.vsprops @@ -0,0 +1,19 @@ + + + + + diff --git a/plugins/GSdx/vsprops/release.vsprops b/plugins/GSdx/vsprops/release.vsprops new file mode 100644 index 0000000000..5cc40308ee --- /dev/null +++ b/plugins/GSdx/vsprops/release.vsprops @@ -0,0 +1,25 @@ + + + + + diff --git a/plugins/GSdx/vsprops/sse2.vsprops b/plugins/GSdx/vsprops/sse2.vsprops new file mode 100644 index 0000000000..1ab36cb277 --- /dev/null +++ b/plugins/GSdx/vsprops/sse2.vsprops @@ -0,0 +1,12 @@ + + + + diff --git a/plugins/GSdx/vsprops/sse4.vsprops b/plugins/GSdx/vsprops/sse4.vsprops new file mode 100644 index 0000000000..601b39f585 --- /dev/null +++ b/plugins/GSdx/vsprops/sse4.vsprops @@ -0,0 +1,12 @@ + + + + diff --git a/plugins/GSdx/vsprops/ssse3.vsprops b/plugins/GSdx/vsprops/ssse3.vsprops new file mode 100644 index 0000000000..ac7fa1a064 --- /dev/null +++ b/plugins/GSdx/vsprops/ssse3.vsprops @@ -0,0 +1,12 @@ + + + +