X360_Device.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. #ifdef _XBOX
  2. #include "X360_Device.h"
  3. #include "..\Render.h"
  4. #include "..\RenderTargets\RenderTarget.h"
  5. #include "..\RenderTargets\RenderTargetDepth.h"
  6. X360RenderDevice::X360RenderDevice()
  7. {
  8. bHudRender = false;
  9. bDisableMSAAForPostprocess = false;
  10. X360RestoreEDRAM_id = NULL;
  11. X360RestoreEDRAM_ColorOnly_id = NULL;
  12. X360Upscale3DTo2D_id = NULL;
  13. d3d_device = NULL;
  14. curBackBufferIndex = 0;
  15. for (dword i = 0; i < DEVICE_BACK_BUFFERS_COUNT; i++)
  16. {
  17. screenBuffer[i] = NULL;
  18. }
  19. screenDepth = NULL;
  20. scene3dDepth = NULL;
  21. scene3d = NULL;
  22. current_RT = NULL;
  23. current_RT_depth = NULL;
  24. m_pPrevSurface = NULL;
  25. m_pPrevDepthSurface = NULL;
  26. pEDRAM_Color = NULL;
  27. pEDRAM_Depth = NULL;
  28. bInsideTiling = false;
  29. pLastSettedSurface = NULL;
  30. pLastSettedDepthSurface = NULL;
  31. }
  32. X360RenderDevice::~X360RenderDevice()
  33. {
  34. RELEASE(d3d_device);
  35. }
  36. // D3D()->SetRenderTarget(0, m_LastSettedSurface);
  37. // D3D()->SetDepthStencilSurface(m_LastSettedDepthSurface);
  38. void X360RenderDevice::EnableMSAA()
  39. {
  40. Assert (bInsideTiling == false);
  41. bDisableMSAAForPostprocess = false;
  42. }
  43. void X360RenderDevice::DisableMSAA()
  44. {
  45. Assert (bInsideTiling == false);
  46. bDisableMSAAForPostprocess = true;
  47. }
  48. void X360RenderDevice::Cleanup ()
  49. {
  50. pEDRAM_Color = NULL;
  51. pEDRAM_Depth = NULL;
  52. FORCERELEASE(screenDepth);
  53. for (dword i = 0; i < DEVICE_BACK_BUFFERS_COUNT; i++)
  54. {
  55. FORCERELEASE(screenBuffer[i]);
  56. }
  57. FORCERELEASE(scene3dDepth);
  58. FORCERELEASE(scene3d);
  59. }
  60. void X360RenderDevice::Init ()
  61. {
  62. pEDRAM_Color = NGRender::pRS->GetTechniqueGlobalVariable("EDRAM_Color", _FL_);
  63. pEDRAM_Depth = NGRender::pRS->GetTechniqueGlobalVariable("EDRAM_Depth", _FL_);
  64. NGRender::pRS->GetShaderId("X360RestoreEDRAM", X360RestoreEDRAM_id);
  65. NGRender::pRS->GetShaderId("X360RestoreEDRAM_ColorOnly", X360RestoreEDRAM_ColorOnly_id);
  66. NGRender::pRS->GetShaderId("X360Upscale3DTo2D", X360Upscale3DTo2D_id);
  67. Assert (pEDRAM_Color);
  68. Assert (pEDRAM_Depth);
  69. }
  70. void X360RenderDevice::Reset ()
  71. {
  72. }
  73. #ifndef STOP_DEBUG
  74. void callBackFunctionLock(DWORD Flags, D3DBLOCKTYPE BlockType, float ClockTime, DWORD ThreadTime)
  75. {
  76. //OutputDebugString("GPU Resource Lock Stall !!!\n");
  77. }
  78. #endif
  79. bool X360RenderDevice::Create (IDirect3D9* d3d9, D3DPRESENT_PARAMETERS &params, long deviceIndex, RENDERSCREEN & m_Screen2DInfo, RENDERSCREEN & m_Screen3DInfo)
  80. {
  81. params.Windowed = false;
  82. params.DisableAutoBackBuffer = TRUE;
  83. params.DisableAutoFrontBuffer = TRUE;
  84. params.EnableAutoDepthStencil = FALSE;
  85. //params.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  86. //1920x1080
  87. //1280x720
  88. //params.BackBufferWidth = 1920;
  89. //params.BackBufferHeight = 1080;
  90. /*
  91. params.BackBufferWidth = 1280;
  92. params.BackBufferHeight = 720;
  93. params.MultiSampleType = D3DMULTISAMPLE_NONE;
  94. */
  95. /*
  96. // EDRAM tiles are allocated in units of 80x16 pixels at 1X multisampling,
  97. // 80x8 at 2X and 40x8 at 4X:
  98. #define GPU_EDRAM_TILE_WIDTH_1X 80
  99. #define GPU_EDRAM_TILE_HEIGHT_1X 16
  100. #define GPU_EDRAM_TILE_WIDTH_2X 80
  101. #define GPU_EDRAM_TILE_HEIGHT_2X 8
  102. #define GPU_EDRAM_TILE_WIDTH_4X 40
  103. #define GPU_EDRAM_TILE_HEIGHT_4X 8
  104. // An EDRAM tile size, in bytes. Note that 64-bit surfaces have an
  105. // allocation granularity of twice this (10240 bytes) but can have a 5120
  106. // byte start alignment:
  107. #define GPU_EDRAM_TILE_SIZE 5120
  108. // Total number of usable EDRAM tiles:
  109. #define GPU_EDRAM_SIZE (10*1024*1024)
  110. #define GPU_EDRAM_TILES (GPU_EDRAM_SIZE / GPU_EDRAM_TILE_SIZE) = 2048 tiles total
  111. 962 лимит
  112. 960x680 12x81
  113. 960x648
  114. 11x84 = 880x672
  115. */
  116. // Get the user video settings.
  117. XVIDEO_MODE VideoMode;
  118. XMemSet( &VideoMode, 0, sizeof(XVIDEO_MODE) );
  119. XGetVideoMode( &VideoMode );
  120. DWORD width_3D = 0;
  121. DWORD height_3D = 0;
  122. /*
  123. 1280/720 = 1.7777777777777777
  124. 1024 / 768 = 1.33333333333333333
  125. {
  126. params.BackBufferWidth = 1040;
  127. params.BackBufferHeight = 585;
  128. } else
  129. {
  130. params.BackBufferWidth = 907;
  131. params.BackBufferHeight = 680;
  132. }
  133. */
  134. if (VideoMode.fIsWideScreen)
  135. {
  136. //Такое разрешение влезает в EDRAM без тайлинга...
  137. params.BackBufferWidth = 1280;
  138. params.BackBufferHeight = 720;
  139. params.MultiSampleType = D3DMULTISAMPLE_NONE;
  140. m_Screen3DInfo.dwWidth = 1040;
  141. m_Screen3DInfo.dwHeight = 585;
  142. } else
  143. {
  144. params.BackBufferWidth = 1024;
  145. params.BackBufferHeight = 768;
  146. params.MultiSampleType = D3DMULTISAMPLE_NONE;
  147. m_Screen3DInfo.dwWidth = 907;
  148. m_Screen3DInfo.dwHeight = 680;
  149. }
  150. //params.BackBufferFormat = D3DFMT_A8R8G8B8;
  151. params.BackBufferFormat = D3DFMT_LE_X8R8G8B8;
  152. params.FrontBufferFormat = D3DFMT_LE_X8R8G8B8;
  153. params.RingBufferParameters.Flags = 0;
  154. params.RingBufferParameters.PrimarySize = 128 * 1024; // default 32 kb
  155. params.RingBufferParameters.pPrimary = NULL;
  156. params.RingBufferParameters.SecondarySize = 4 * 1024 * 1024; // default 2Mb
  157. params.RingBufferParameters.pSecondary = NULL;
  158. params.RingBufferParameters.SegmentCount = 32; // default 32
  159. if (d3d9->CreateDevice(0, D3DDEVTYPE_HAL, NULL, D3DCREATE_BUFFER_2_FRAMES, &params, &d3d_device) != D3D_OK)
  160. {
  161. throw("Can't create Direct3D Device !");
  162. }
  163. /*
  164. WORD ConvertFloatTo16BitGammasRGB( FLOAT f ) { return ConvertFloatToNBitGamma<WORD, 16, GammaFuncsRGB>( f ); }
  165. WORD Convert10BitGammaPWLTo16BitGammasRGB( WORD i ) { return ConvertFloatTo16BitGammasRGB( Convert10BitGammaPWLToFloat( i ) ); }
  166. D3DPWLGAMMA PWLGamma;
  167. for( UINT j = 0; j < 128; ++j )
  168. {
  169. PWLGamma.red[j].Base = PWLGamma.green[j].Base = PWLGamma.blue[j].Base = Convert10BitGammaPWLTo16BitGammasRGB( (WORD) ( j << 3 ) );
  170. }
  171. for( UINT j = 0; j < 127; ++j )
  172. {
  173. PWLGamma.red[j].Delta = PWLGamma.red[j+1].Base - PWLGamma.red[j].Base;
  174. PWLGamma.green[j].Delta = PWLGamma.green[j+1].Base - PWLGamma.green[j].Base;
  175. PWLGamma.blue[j].Delta = PWLGamma.blue[j+1].Base - PWLGamma.blue[j].Base;
  176. }
  177. PWLGamma.red[127].Delta = 0xffff - PWLGamma.red[127].Base;
  178. PWLGamma.green[127].Delta = 0xffff - PWLGamma.green[127].Base;
  179. PWLGamma.blue[127].Delta = 0xffff - PWLGamma.blue[127].Base;
  180. d3d_device->SetPWLGamma( 0, &PWLGamma );
  181. */
  182. #if defined (_DEBUG) || defined(PROFILE)
  183. //Release - PIX profile
  184. //8Mb отжираем
  185. PIXEnableTextureTracking(0, 4 * 1024 * 1024, NULL);
  186. #endif
  187. #ifndef STOP_DEBUG
  188. d3d_device->SetBlockCallback(0, callBackFunctionLock);
  189. #endif
  190. CDX8IBuffer::pD3D8 = D3D();
  191. CDX8VBuffer::pD3D8 = D3D();
  192. CDX8Texture::pD3D8 = D3D();
  193. #if !(defined (_XBOX) && defined(_PRECOMPILED_COMMAND_BUFFER_BAKER))
  194. SHADER::pD3D8 = D3D();
  195. #endif
  196. StateFilter::pD3D8 = D3D();
  197. m_Screen2DInfo.dwWidth = params.BackBufferWidth;
  198. m_Screen2DInfo.dwHeight = params.BackBufferHeight;
  199. m_Screen2DInfo.BackBufferFormat = FormatFromDX(params.BackBufferFormat);
  200. m_Screen3DInfo.BackBufferFormat = m_Screen2DInfo.BackBufferFormat;
  201. m_Screen3DInfo.bWindowed = m_Screen2DInfo.bWindowed;
  202. m_Screen3DInfo.fFOV = m_Screen2DInfo.fFOV;
  203. m_Screen3DInfo.StencilFormat = m_Screen2DInfo.StencilFormat;
  204. //Сюда будем рисовать 3D сцену
  205. scene3d = NGRender::pRS->CreateRenderTarget(m_Screen3DInfo.dwWidth, m_Screen3DInfo.dwHeight, _FL_, FMT_LE_X8R8G8B8, MSTypeFromDX(D3DMULTISAMPLE_2_SAMPLES));
  206. scene3dDepth = NGRender::pRS->CreateDepthStencil(m_Screen3DInfo.dwWidth, m_Screen3DInfo.dwHeight, _FL_, FMT_D24S8, MSTypeFromDX(D3DMULTISAMPLE_2_SAMPLES));
  207. //Тут реальный бекбуффер 720p
  208. for (dword i = 0; i < DEVICE_BACK_BUFFERS_COUNT; i++)
  209. {
  210. screenBuffer[i] = NGRender::pRS->CreateRenderTarget(params.BackBufferWidth, params.BackBufferHeight, _FL_, FMT_LE_X8R8G8B8, MSTypeFromDX(D3DMULTISAMPLE_NONE));
  211. }
  212. screenDepth = NGRender::pRS->CreateDepthStencil(params.BackBufferWidth, params.BackBufferHeight, _FL_, FMT_D24S8, MSTypeFromDX(D3DMULTISAMPLE_NONE));
  213. //Чистим текстуры черным, начальные...
  214. for (int i = 0; i < DEVICE_BACK_BUFFERS_COUNT; i++)
  215. {
  216. IDirect3DTexture9* pScreenPixels = (IDirect3DTexture9*)screenBuffer[i]->AsTexture()->GetBaseTexture();
  217. D3DLOCKED_RECT lr;
  218. pScreenPixels->LockRect(0, &lr, NULL, 0);
  219. memset (lr.pBits, 0, lr.Pitch * params.BackBufferHeight);
  220. pScreenPixels->UnlockRect(0);
  221. }
  222. SetRenderTarget(RTO_DONTOCH_CONTEXT, scene3d, scene3dDepth);
  223. return true;
  224. }
  225. void X360RenderDevice::Present ()
  226. {
  227. dword dwId = NGRender::pRS->pixBeginEvent(_FL_, "::Present");
  228. /*
  229. IBaseTexture* buffTex0 = backBuffer0->AsTexture();
  230. //IDirect3DBaseTexture9* pDXTexture0 = NGRender::pRS->GetDXBaseTexture(buffTex0);
  231. IBaseTexture* buffTex1 = backBuffer1->AsTexture();
  232. IDirect3DBaseTexture9* pDXTexture1 = NGRender::pRS->GetDXBaseTexture(buffTex1);
  233. IDirect3DSurface9* rtColorOld = NULL;
  234. IDirect3DSurface9* rtDepthOld = NULL;
  235. D3D()->GetRenderTarget(0, &rtColorOld);
  236. D3D()->GetDepthStencilSurface(&rtDepthOld);
  237. D3D()->SetRenderTarget(0, finalRenderTarget);
  238. D3D()->SetDepthStencilSurface( NULL );
  239. pEDRAM_Color->SetTexture(buffTex0);
  240. pEDRAM_Depth->SetTexture(NULL);
  241. RENDERSCREEN rs = NGRender::pRS->GetScreenInfo();
  242. NGRender::pRS->DrawFullScreenQuad(rs.dwWidth, rs.dwHeight, X360RestoreEDRAM_ColorOnly_id);
  243. // Wait for the vertical blank before we resolve to the front buffer to avoid tearing.
  244. D3D()->SynchronizeToPresentationInterval();
  245. D3DVECTOR4 ClearColor;
  246. ClearColor.x = 0.0f;
  247. ClearColor.y = 0.0f;
  248. ClearColor.z = 0.0f;
  249. ClearColor.w = 0.0f;
  250. // Resolve the rendered scene back to the front buffer.
  251. D3D()->Resolve( D3DRESOLVE_RENDERTARGET0 | D3DRESOLVE_ALLFRAGMENTS,
  252. NULL,
  253. pDXTexture1,
  254. NULL,
  255. 0, 0,
  256. &ClearColor,
  257. 1.0f, 0, NULL );
  258. */
  259. D3D()->SetShaderGPRAllocation(0, 0, 0);
  260. EnableMSAA();
  261. IBaseTexture* buffTex1 = screenBuffer[curBackBufferIndex]->AsTexture();
  262. IDirect3DBaseTexture9* pDXTexture1 = (IDirect3DBaseTexture9*)buffTex1->GetBaseTexture();
  263. curBackBufferIndex = curBackBufferIndex + 1;
  264. if (curBackBufferIndex >= DEVICE_BACK_BUFFERS_COUNT)
  265. {
  266. curBackBufferIndex = 0;
  267. }
  268. //Меняем...
  269. rt_options = RTO_DONTOCH_CONTEXT;
  270. current_RT = scene3d;
  271. current_RT_depth = scene3dDepth;
  272. bHudRender = false;
  273. D3D()->SynchronizeToPresentationInterval();
  274. D3D()->Swap( pDXTexture1, NULL );
  275. //D3D()->Present(NULL, NULL, NULL, NULL);
  276. rt_options = RTO_DONTOCH_CONTEXT;
  277. /*
  278. D3D()->SetRenderTarget(0, rtColorOld);
  279. D3D()->SetDepthStencilSurface(rtDepthOld);
  280. */
  281. NGRender::pRS->pixEndEvent(_FL_, dwId);
  282. }
  283. void X360RenderDevice::BeginScene ()
  284. {
  285. static char tmp[8192];
  286. RENDERSURFACE_DESC dsc;
  287. if (current_RT)
  288. {
  289. current_RT->GetDesc(&dsc);
  290. } else
  291. {
  292. if (current_RT_depth)
  293. {
  294. current_RT_depth->GetDesc(&dsc);
  295. } else
  296. {
  297. dsc.Width = 0;
  298. dsc.Height = 0;
  299. }
  300. }
  301. dwBeginSceneId = NGRender::pRS->pixBeginEvent(_FL_, "::BeginScene(%dx%d)", dsc.Width, dsc.Height);
  302. if (rt_options == RTO_RESTORE_CONTEXT || rt_options == RTO_RESTORE_CONTEXT_COLORONLY)
  303. {
  304. if (current_RT)
  305. {
  306. IBaseTexture* SavedEDRAM = current_RT->AsTexture();
  307. pEDRAM_Color->SetTexture(SavedEDRAM);
  308. if (current_RT_depth && rt_options == RTO_RESTORE_CONTEXT)
  309. {
  310. RenderTargetDepth* rtd = (RenderTargetDepth*)current_RT_depth;
  311. pEDRAM_Depth->SetTexture(rtd->AsTexture());
  312. } else
  313. {
  314. pEDRAM_Depth->ResetTexture();
  315. }
  316. RENDERSCREEN rs;
  317. if (bHudRender)
  318. {
  319. rs = NGRender::pRS->GetScreenInfo2D();
  320. } else
  321. {
  322. rs = NGRender::pRS->GetScreenInfo3D();
  323. }
  324. if (current_RT_depth && rt_options == RTO_RESTORE_CONTEXT)
  325. {
  326. NGRender::pRS->DrawFullScreenQuad(rs.dwWidth, rs.dwHeight, X360RestoreEDRAM_id);
  327. } else
  328. {
  329. NGRender::pRS->DrawFullScreenQuad(rs.dwWidth, rs.dwHeight, X360RestoreEDRAM_ColorOnly_id);
  330. }
  331. }
  332. }
  333. NGRender::pRS->pixEndEvent(_FL_, dwBeginSceneId);
  334. bInsideTiling = true;
  335. }
  336. void X360RenderDevice::EndScene (IBaseTexture* pDestiantionTexture, bool bSkipAnyWork, bool bDontResolveDepthOnX360)
  337. {
  338. D3DVECTOR4 clearColor;
  339. clearColor.x = 0.0f;
  340. clearColor.y = 0.0f;
  341. clearColor.z = 0.0f;
  342. clearColor.w = 0.0f;
  343. if (bSkipAnyWork)
  344. {
  345. return;
  346. }
  347. if (current_RT_depth && bDontResolveDepthOnX360 == false)
  348. {
  349. IDirect3DTexture9* depth_tex = (IDirect3DTexture9*)((RenderTargetDepth*)current_RT_depth)->AsTexture()->GetBaseTexture();;
  350. D3D()->Resolve( D3DRESOLVE_DEPTHSTENCIL | D3DRESOLVE_FRAGMENT0, NULL, depth_tex, NULL, 0, 0, &clearColor, 1.0f, 0L, NULL);
  351. }
  352. if (current_RT)
  353. {
  354. IDirect3DTexture9* tex = (IDirect3DTexture9*)current_RT->AsTexture()->GetBaseTexture();
  355. if (pDestiantionTexture)
  356. {
  357. IDirect3DTexture9* tex2 = (IDirect3DTexture9*)pDestiantionTexture->GetBaseTexture();
  358. D3D()->Resolve( D3DRESOLVE_RENDERTARGET0 | D3DRESOLVE_ALLFRAGMENTS, NULL, tex2, NULL, 0, 0, &clearColor, 1.0f, 0L, NULL);
  359. }
  360. D3D()->Resolve(D3DRESOLVE_RENDERTARGET0 | D3DRESOLVE_ALLFRAGMENTS, NULL, tex, NULL, 0, 0, &clearColor, 1.0f, 0x0, NULL);
  361. //------------------------------------
  362. //надо построить мип цепоцки если это необходимо
  363. long mipCount = ((RenderTarget*)current_RT)->GetMipCount();
  364. if (mipCount > 1)
  365. {
  366. dword dwMipId = NGRender::pRS->pixBeginEvent(_FL_, "::Build render target mipmaps");
  367. RENDERSURFACE_DESC desc;
  368. current_RT->GetDesc(&desc);
  369. UINT mipWidth = desc.Width;
  370. UINT mipHeight = desc.Height;
  371. D3DVIEWPORT9 oldVp;
  372. D3D()->GetViewport(&oldVp);
  373. XGTEXTURE_DESC Desc;
  374. D3DVIEWPORT9 mipVp = oldVp;
  375. for (long i = 1; i < mipCount; i++)
  376. {
  377. //Рисум мип в EDRAM
  378. IBaseTexture* sourceMip = current_RT->AsTexture();
  379. IDirect3DTexture9* tex = (IDirect3DTexture9*)sourceMip->GetBaseTexture();
  380. XGGetTextureDesc( tex, i, &Desc );
  381. mipVp.Width = Desc.Width;
  382. mipVp.Height = Desc.Height;
  383. D3D()->SetViewport( &mipVp );
  384. D3D()->SetSamplerState( 0, D3DSAMP_MINMIPLEVEL, i - 1 );
  385. //D3D()->Clear(0, null, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0x30405060, 1.0, 0);
  386. pEDRAM_Color->SetTexture(sourceMip);
  387. pEDRAM_Depth->ResetTexture();
  388. NGRender::pRS->DrawFullScreenQuad(mipWidth, mipHeight, X360RestoreEDRAM_ColorOnly_id);
  389. //Ресолвим в мип текстуры из EDRAM
  390. D3D()->Resolve(D3DRESOLVE_RENDERTARGET0 | D3DRESOLVE_ALLFRAGMENTS, NULL, tex, NULL, i, 0, &clearColor, 1.0f, 0x0, NULL);
  391. }
  392. D3D()->SetViewport( &oldVp );
  393. D3D()->SetSamplerState( 0, D3DSAMP_MINMIPLEVEL, 13 );
  394. NGRender::pRS->pixEndEvent(_FL_, dwMipId);
  395. }
  396. }
  397. bInsideTiling = false;
  398. /*
  399. //Возвращаем старые...
  400. D3D()->SetRenderTarget(0, m_pPrevSurface);
  401. D3D()->SetDepthStencilSurface(m_pPrevDepthSurface);
  402. */
  403. }
  404. void X360RenderDevice::SetRenderTarget (RenderTartgetOptions options, IRenderTarget* renderTarget, IRenderTargetDepth* renderTargetDepth)
  405. {
  406. //
  407. dword dwId = NGRender::pRS->pixBeginEvent(_FL_, "SetRenderTarget (RenderTartgetOptions = %d)", options);
  408. rt_options = options;
  409. current_RT = renderTarget;
  410. current_RT_depth = renderTargetDepth;
  411. /*
  412. //Получаем старые с рендера
  413. D3D()->GetRenderTarget(0, &m_pPrevSurface);
  414. D3D()->GetDepthStencilSurface(&m_pPrevDepthSurface);
  415. */
  416. if (renderTarget && renderTargetDepth)
  417. {
  418. info = ((RenderTarget*)current_RT)->GetTileInfoWithDepth();
  419. } else
  420. {
  421. if (renderTarget && renderTargetDepth == NULL)
  422. {
  423. info = ((RenderTarget*)current_RT)->GetTileInfo();
  424. } else
  425. {
  426. if (renderTarget == NULL && renderTargetDepth)
  427. {
  428. info = ((RenderTargetDepth*)current_RT_depth)->GetTileInfoDepthOnly();
  429. } else
  430. {
  431. //Ничего делать не надо
  432. NGRender::pRS->pixEndEvent(_FL_, dwId);
  433. return;
  434. }
  435. }
  436. }
  437. if (bDisableMSAAForPostprocess == false)
  438. {
  439. D3D()->SetRenderTarget(0, info.pSurface);
  440. D3D()->SetDepthStencilSurface(info.pDepthSurface);
  441. } else
  442. {
  443. D3D()->SetRenderTarget(0, info.pSurface_withoutMSAA);
  444. D3D()->SetDepthStencilSurface(info.pDepthSurface_withoutMSAA);
  445. }
  446. pLastSettedSurface = info.pSurface;
  447. pLastSettedDepthSurface = info.pDepthSurface;
  448. NGRender::pRS->pixEndEvent(_FL_, dwId);
  449. }
  450. void X360RenderDevice::GetRenderTarget (IRenderTarget** renderTarget, IRenderTargetDepth** renderTargetDepth)
  451. {
  452. if (renderTarget)
  453. {
  454. *renderTarget = current_RT;
  455. }
  456. if (renderTargetDepth)
  457. {
  458. *renderTargetDepth = current_RT_depth;
  459. }
  460. }
  461. IDirect3DDevice9* X360RenderDevice::D3D()
  462. {
  463. return d3d_device;
  464. }
  465. bool X360RenderDevice::DisableStateManager ()
  466. {
  467. return false;
  468. }
  469. void X360RenderDevice::CalculateTiling(dword Width, dword Height, D3DFORMAT Format, D3DFORMAT DepthFormat, D3DMULTISAMPLE_TYPE MultiSample, PredicatedTilingInfo& info)
  470. {
  471. // Check need for predicated tiling
  472. info.dwTileWidth = Width;
  473. info.dwTileHeight = Height;
  474. DWORD dwNumBackBufferEDRAMTiles = 0;
  475. if (Format != D3DFMT_UNKNOWN)
  476. {
  477. dwNumBackBufferEDRAMTiles = XGSurfaceSize( info.dwTileWidth, info.dwTileHeight, Format, MultiSample );
  478. }
  479. DWORD dwNumZBufferEDRAMTiles = 0;
  480. if (DepthFormat != D3DFMT_UNKNOWN)
  481. {
  482. dwNumZBufferEDRAMTiles = XGSurfaceSize( info.dwTileWidth, info.dwTileHeight, DepthFormat, MultiSample );
  483. }
  484. //Такой RT лезет в нижную часть EDRAM, можно не портить основную EDRAM память...
  485. if (dwNumBackBufferEDRAMTiles + dwNumZBufferEDRAMTiles < 64)
  486. {
  487. info.bUseLastPartOfEDRAM = true;
  488. } else
  489. {
  490. info.bUseLastPartOfEDRAM = false;
  491. }
  492. //Обычный RT
  493. if (dwNumBackBufferEDRAMTiles + dwNumZBufferEDRAMTiles < GPU_EDRAM_TILES)
  494. {
  495. DWORD HyperZTiles = XGHierarchicalZSize(Width, Height, MultiSample);
  496. Assert (HyperZTiles <= GPU_HIERARCHICAL_Z_TILES);
  497. if (HyperZTiles <= GPU_HIERARCHICAL_Z_TILES)
  498. {
  499. info.hyperZDisable = false;
  500. } else
  501. {
  502. info.hyperZDisable = true;
  503. }
  504. return;
  505. }
  506. //Predicated tiling is prohibited by Joker due to march 2009 TCR
  507. Assert(false);
  508. }
  509. void X360RenderDevice::CreateEDRAMSurfaces (PredicatedTilingInfo& info, EdramRTMode rtmode, D3DFORMAT fmt, D3DMULTISAMPLE_TYPE msaa)
  510. {
  511. D3DSURFACE_PARAMETERS SurfaceParameters = {0};
  512. if (info.bUseLastPartOfEDRAM == false)
  513. {
  514. SurfaceParameters.Base = 0;
  515. } else
  516. {
  517. SurfaceParameters.Base = GPU_EDRAM_TILES - XGSurfaceSize( info.dwTileWidth, info.dwTileHeight, fmt, msaa );
  518. if (rtmode == EDRAM_COLOR_AND_DEPTH)
  519. {
  520. SurfaceParameters.Base = SurfaceParameters.Base - XGSurfaceSize( info.dwTileWidth, info.dwTileHeight, D3DFMT_D24X8, msaa );
  521. }
  522. }
  523. HRESULT hr = D3D_OK;
  524. if (rtmode == EDRAM_ONLY_COLOR || rtmode == EDRAM_COLOR_AND_DEPTH)
  525. {
  526. hr = D3D()->CreateRenderTarget(info.dwTileWidth, info.dwTileHeight, fmt, msaa, 0, FALSE, &info.pSurface, &SurfaceParameters);
  527. hr = D3D()->CreateRenderTarget(info.dwTileWidth, info.dwTileHeight, fmt, D3DMULTISAMPLE_NONE, 0, FALSE, &info.pSurface_withoutMSAA, &SurfaceParameters);
  528. } else
  529. {
  530. info.pSurface = NULL;
  531. info.pSurface_withoutMSAA = NULL;
  532. }
  533. if (rtmode == EDRAM_ONLY_DEPTH || rtmode == EDRAM_COLOR_AND_DEPTH)
  534. {
  535. if (rtmode == EDRAM_COLOR_AND_DEPTH)
  536. {
  537. SurfaceParameters.Base = SurfaceParameters.Base + XGSurfaceSize( info.dwTileWidth, info.dwTileHeight, fmt, msaa );
  538. //Assert (sizeOf_MSAAHACK == SurfaceParameters.Base);
  539. }
  540. if (info.hyperZDisable)
  541. {
  542. Assert (false);
  543. SurfaceParameters.HierarchicalZBase = 0;
  544. } else
  545. {
  546. SurfaceParameters.HierarchicalZBase = 0;
  547. }
  548. hr = D3D()->CreateDepthStencilSurface(info.dwTileWidth, info.dwTileHeight, D3DFMT_D24X8, msaa, 0, FALSE, &info.pDepthSurface, &SurfaceParameters);
  549. hr = D3D()->CreateDepthStencilSurface(info.dwTileWidth, info.dwTileHeight, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, FALSE, &info.pDepthSurface_withoutMSAA, &SurfaceParameters);
  550. } else
  551. {
  552. info.pDepthSurface = NULL;
  553. info.pDepthSurface_withoutMSAA = NULL;
  554. }
  555. }
  556. void X360RenderDevice::ResolveColor (IBaseTexture * dest)
  557. {
  558. if (current_RT && dest)
  559. {
  560. HRESULT hr;
  561. RENDERSURFACE_DESC desc1;
  562. current_RT_depth->GetDesc(&desc1);
  563. dword dwTexWidth = dest->GetWidth();
  564. dword dwTexHeight = dest->GetHeight();
  565. Assert(desc1.Width == dwTexWidth);
  566. Assert(desc1.Height == dwTexHeight);
  567. IDirect3DTexture9* color_tex = (IDirect3DTexture9*)dest->GetBaseTexture();
  568. D3D()->Resolve( D3DRESOLVE_RENDERTARGET0 | D3DRESOLVE_ALLFRAGMENTS, NULL, color_tex, NULL, 0, 0, NULL, 1.0f, 0L, NULL);
  569. }
  570. }
  571. void X360RenderDevice::ResolveDepth (IRenderTargetDepth* dest)
  572. {
  573. if (current_RT_depth && dest)
  574. {
  575. HRESULT hr;
  576. RENDERSURFACE_DESC desc1;
  577. RENDERSURFACE_DESC desc2;
  578. current_RT_depth->GetDesc(&desc1);
  579. dest->GetDesc(&desc2);
  580. if (desc1.Width != desc2.Width || desc1.Height != desc2.Height)
  581. {
  582. int a = 0;
  583. }
  584. Assert(desc1.Width == desc2.Width);
  585. Assert(desc1.Height == desc2.Height);
  586. //Assert(desc1.MultiSampleType == desc2.MultiSampleType);
  587. //Assert(desc1.MultiSampleQuality == desc2.MultiSampleQuality);
  588. IDirect3DTexture9* depth_tex = (IDirect3DTexture9*)((RenderTargetDepth*)dest)->AsTexture()->GetBaseTexture();
  589. D3D()->Resolve( D3DRESOLVE_DEPTHSTENCIL | D3DRESOLVE_FRAGMENT0, NULL, depth_tex, NULL, 0, 0, NULL, 1.0f, 0L, NULL);
  590. }
  591. }
  592. void X360RenderDevice::SwitchToHUDRenderLoadingScreenHack()
  593. {
  594. if (bHudRender)
  595. {
  596. return;
  597. }
  598. bHudRender = true;
  599. SetRenderTarget(RTO_DONTOCH_CONTEXT, screenBuffer[curBackBufferIndex], screenDepth);
  600. }
  601. void X360RenderDevice::SwitchToHUDRender(IRenderTarget* color, IRenderTargetDepth* depth, const RENDERVIEWPORT & viewPort)
  602. {
  603. if (bHudRender)
  604. {
  605. return;
  606. }
  607. bHudRender = true;
  608. //Надо поставить...
  609. //screenBuffer[curBackBufferIndex]
  610. NGRender::pRS->SetRenderTarget(RTO_DONTOCH_CONTEXT, screenBuffer[curBackBufferIndex], screenDepth);
  611. const RENDERSCREEN & rs = NGRender::pRS->GetScreenInfo2D();
  612. RENDERVIEWPORT vp2D;
  613. vp2D.X = 0;
  614. vp2D.Y = 0;
  615. vp2D.Width = rs.dwWidth;
  616. vp2D.Height = rs.dwHeight;
  617. vp2D.MinZ = 0.0f;
  618. vp2D.MaxZ = 1.0f;
  619. NGRender::pRS->SetViewport(vp2D);
  620. if (color)
  621. {
  622. IBaseTexture* textureToUpscale = color->AsTexture();
  623. pEDRAM_Color->SetTexture(textureToUpscale);
  624. pEDRAM_Depth->ResetTexture();
  625. NGRender::pRS->DrawFullScreenQuad(rs.dwWidth, rs.dwHeight, X360Upscale3DTo2D_id);
  626. }
  627. }
  628. #endif