Renderer.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Geometry.h"
  25. #include "IndexBuffer.h"
  26. #include "Log.h"
  27. #include "Matrix4x3.h"
  28. #include "PixelShader.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "RendererEvents.h"
  32. #include "RendererImpl.h"
  33. #include "StringUtils.h"
  34. #include "Texture2D.h"
  35. #include "TextureCube.h"
  36. #include "VertexBuffer.h"
  37. #include "VertexDeclaration.h"
  38. #include "VertexShader.h"
  39. #include <cstring>
  40. #include "DebugNew.h"
  41. #ifdef _MSC_VER
  42. #pragma warning(disable:4355)
  43. #endif
  44. static const DWORD windowStyle = WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
  45. static const D3DCMPFUNC d3dCmpFunc[] =
  46. {
  47. D3DCMP_ALWAYS,
  48. D3DCMP_EQUAL,
  49. D3DCMP_NOTEQUAL,
  50. D3DCMP_LESS,
  51. D3DCMP_LESSEQUAL,
  52. D3DCMP_GREATER,
  53. D3DCMP_GREATEREQUAL
  54. };
  55. // These are for optimizing the case where user supplies a single vertex buffer in setVertexBuffer()
  56. std::vector<VertexBuffer*> vertexBuffers;
  57. std::vector<unsigned> elementMasks;
  58. std::map<HWND, Renderer*> renderers;
  59. static LRESULT CALLBACK wndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
  60. Renderer::Renderer(const std::string& windowTitle) :
  61. mImpl(new RendererImpl()),
  62. mWindowTitle(windowTitle),
  63. mMode(RENDER_FORWARD),
  64. mWidth(0),
  65. mHeight(0),
  66. mMultiSample(0),
  67. mWindowPosX(0),
  68. mWindowPosY(0),
  69. mFullscreen(false),
  70. mVsync(false),
  71. mClosed(false),
  72. mDeviceLost(false),
  73. mQueryIssued(false),
  74. mSystemDepthStencil(false),
  75. mDeferredSupport(false),
  76. mPrepassSupport(false),
  77. mSM3Support(false),
  78. mHardwareDepthSupport(false),
  79. mHardwareShadowSupport(false),
  80. mHiresShadowSupport(false),
  81. mForceSM2(false),
  82. mNumPrimitives(0),
  83. mNumBatches(0),
  84. mImmediateBuffer(0),
  85. mDefaultTextureFilterMode(FILTER_BILINEAR)
  86. {
  87. LOGINFO("Renderer created");
  88. resetCachedState();
  89. vertexBuffers.resize(1);
  90. elementMasks.resize(1);
  91. elementMasks[0] = MASK_DEFAULT;
  92. subscribeToEvent(EVENT_WINDOWMESSAGE, EVENT_HANDLER(Renderer, handleWindowMessage));
  93. }
  94. Renderer::~Renderer()
  95. {
  96. mVertexDeclarations.clear();
  97. if (mImpl->mFrameQuery)
  98. {
  99. mImpl->mFrameQuery->Release();
  100. mImpl->mFrameQuery = 0;
  101. }
  102. if (mImpl->mDefaultColorSurface)
  103. {
  104. mImpl->mDefaultColorSurface->Release();
  105. mImpl->mDefaultColorSurface = 0;
  106. }
  107. if (mImpl->mDefaultDepthStencilSurface)
  108. {
  109. if (mSystemDepthStencil)
  110. mImpl->mDefaultDepthStencilSurface->Release();
  111. mImpl->mDefaultDepthStencilSurface = 0;
  112. }
  113. if (mImpl->mDevice)
  114. {
  115. mImpl->mDevice->Release();
  116. mImpl->mDevice = 0;
  117. }
  118. if (mImpl->mInterface)
  119. {
  120. mImpl->mInterface->Release();
  121. mImpl->mInterface = 0;
  122. }
  123. if (mImpl->mWindow)
  124. {
  125. DestroyWindow(mImpl->mWindow);
  126. renderers.erase(mImpl->mWindow);
  127. mImpl->mWindow = 0;
  128. }
  129. delete mImpl;
  130. mImpl = 0;
  131. LOGINFO("Renderer shut down");
  132. }
  133. void Renderer::messagePump()
  134. {
  135. MSG msg;
  136. while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  137. {
  138. TranslateMessage(&msg);
  139. DispatchMessage(&msg);
  140. if (msg.message == WM_QUIT)
  141. mClosed = true;
  142. }
  143. }
  144. void Renderer::setWindowTitle(const std::string& windowTitle)
  145. {
  146. mWindowTitle = windowTitle;
  147. if (mImpl->mWindow)
  148. SetWindowText(mImpl->mWindow, mWindowTitle.c_str());
  149. }
  150. void Renderer::setMode(RenderMode mode, int width, int height, bool fullscreen, bool vsync, int multiSample)
  151. {
  152. // Find out the full screen mode display format (match desktop color depth)
  153. D3DFORMAT fullscreenFormat = mImpl->getDesktopFormat();
  154. // If zero dimensions, use the desktop default
  155. if ((width <= 0) || (height <= 0))
  156. {
  157. if (fullscreen)
  158. {
  159. IntVector2 desktopResolution = mImpl->getDesktopResolution();
  160. width = desktopResolution.mX;
  161. height = desktopResolution.mY;
  162. }
  163. else
  164. {
  165. width = 800;
  166. height = 600;
  167. }
  168. }
  169. if ((mode == mMode) && (width == mWidth) && (height == mHeight) && (fullscreen == mFullscreen) && (vsync == mVsync)
  170. && (multiSample == mMultiSample))
  171. return;
  172. if (!mImpl->mWindow)
  173. createWindow(width, height);
  174. if (!mImpl->mInterface)
  175. createInterface();
  176. // Disable deferred / light prepass rendering if not supported
  177. // Note: we do not fall back from deferred to light prepass, because there might not be shaders / materials
  178. // defined for it. Instead fall back directly to forward rendering
  179. if ((mode == RENDER_DEFERRED) && (!mDeferredSupport))
  180. mode = RENDER_FORWARD;
  181. if ((mode == RENDER_PREPASS) && (!mPrepassSupport))
  182. mode = RENDER_FORWARD;
  183. if (multiSample >= (int)D3DMULTISAMPLE_2_SAMPLES)
  184. multiSample = clamp(multiSample, (int)D3DMULTISAMPLE_NONE, (int)D3DMULTISAMPLE_16_SAMPLES);
  185. else
  186. multiSample = 0;
  187. // Note: getMultiSample() will not reflect the actual hardware multisample mode, but rather what the caller wanted.
  188. // In deferred or light prepass mode, it is used to control the edge filter
  189. mMultiSample = multiSample;
  190. if (mode != RENDER_FORWARD)
  191. multiSample = 0;
  192. // Check fullscreen mode validity. If not valid, revert to windowed
  193. if (fullscreen)
  194. {
  195. std::vector<IntVector2> resolutions = getResolutions();
  196. fullscreen = false;
  197. for (unsigned i = 0; i < resolutions.size(); ++i)
  198. {
  199. if ((width == resolutions[i].mX) && (height == resolutions[i].mY))
  200. {
  201. fullscreen = true;
  202. break;
  203. }
  204. }
  205. }
  206. // Fall back to non-multisampled if unsupported multisampling mode
  207. if (multiSample)
  208. {
  209. if (FAILED(mImpl->mInterface->CheckDeviceMultiSampleType(mImpl->mAdapter, mImpl->mDeviceType, fullscreenFormat, FALSE,
  210. (D3DMULTISAMPLE_TYPE)multiSample, NULL)))
  211. multiSample = 0;
  212. }
  213. // Save window placement if currently windowed
  214. if (!mFullscreen)
  215. {
  216. WINDOWPLACEMENT wndpl;
  217. wndpl.length = sizeof wndpl;
  218. if (SUCCEEDED(GetWindowPlacement(mImpl->mWindow, &wndpl)))
  219. {
  220. mWindowPosX = wndpl.rcNormalPosition.left;
  221. mWindowPosY = wndpl.rcNormalPosition.top;
  222. }
  223. }
  224. if (fullscreen)
  225. {
  226. mImpl->mPresentParams.BackBufferFormat = fullscreenFormat;
  227. mImpl->mPresentParams.Windowed = false;
  228. }
  229. else
  230. {
  231. mImpl->mPresentParams.BackBufferFormat = D3DFMT_UNKNOWN;
  232. mImpl->mPresentParams.Windowed = true;
  233. }
  234. // Use autodepthstencil normally. However, if INTZ depth is available and deferred rendering is enabled,
  235. // create a depth texture instead
  236. bool autoDepthStencil = true;
  237. if ((mode != RENDER_FORWARD) && (mHardwareDepthSupport))
  238. autoDepthStencil = false;
  239. mImpl->mPresentParams.BackBufferWidth = width;
  240. mImpl->mPresentParams.BackBufferHeight = height;
  241. mImpl->mPresentParams.BackBufferCount = 1;
  242. mImpl->mPresentParams.MultiSampleType = (D3DMULTISAMPLE_TYPE)multiSample;
  243. mImpl->mPresentParams.MultiSampleQuality = 0;
  244. mImpl->mPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  245. mImpl->mPresentParams.hDeviceWindow = mImpl->mWindow;
  246. mImpl->mPresentParams.EnableAutoDepthStencil = autoDepthStencil;
  247. mImpl->mPresentParams.AutoDepthStencilFormat = D3DFMT_D24S8;
  248. mImpl->mPresentParams.Flags = 0;
  249. mImpl->mPresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  250. if (vsync)
  251. mImpl->mPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  252. else
  253. mImpl->mPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  254. mWidth = width;
  255. mHeight = height;
  256. mFullscreen = fullscreen;
  257. mVsync = vsync;
  258. mMode = mode;
  259. if (!mImpl->mDevice)
  260. {
  261. unsigned adapter = D3DADAPTER_DEFAULT;
  262. unsigned deviceType = D3DDEVTYPE_HAL;
  263. // Check for PerfHUD adapter
  264. for (unsigned i=0; i < mImpl->mInterface->GetAdapterCount(); ++i)
  265. {
  266. D3DADAPTER_IDENTIFIER9 identifier;
  267. mImpl->mInterface->GetAdapterIdentifier(i, 0, &identifier);
  268. if (strstr(identifier.Description, "PerfHUD") != 0)
  269. {
  270. adapter = i;
  271. deviceType = D3DDEVTYPE_REF;
  272. break;
  273. }
  274. }
  275. mImpl->mInterface->GetAdapterIdentifier(adapter, 0, &mImpl->mAdapterIdentifier);
  276. createDevice(adapter, deviceType);
  277. }
  278. else
  279. resetDevice();
  280. // Adjust window style/size now
  281. if (fullscreen)
  282. {
  283. SetWindowLongPtr(mImpl->mWindow, GWL_STYLE, WS_POPUP);
  284. SetWindowPos(mImpl->mWindow, HWND_TOP, 0, 0, width, height, SWP_NOZORDER | SWP_SHOWWINDOW);
  285. }
  286. else
  287. {
  288. RECT rect = {0, 0, width, height};
  289. AdjustWindowRect(&rect, windowStyle, false);
  290. SetWindowLongPtr(mImpl->mWindow, GWL_STYLE, windowStyle);
  291. SetWindowPos(mImpl->mWindow, HWND_TOP, mWindowPosX, mWindowPosY, rect.right - rect.left, rect.bottom - rect.top,
  292. SWP_NOZORDER | SWP_SHOWWINDOW);
  293. // Clean up the desktop of the old window contents
  294. InvalidateRect(0, 0, true);
  295. }
  296. using namespace ScreenMode;
  297. VariantMap eventData;
  298. eventData[P_WIDTH] = mWidth;
  299. eventData[P_HEIGHT] = mHeight;
  300. eventData[P_FULLSCREEN] = mFullscreen;
  301. sendEvent(EVENT_SCREENMODE, eventData);
  302. if (!multiSample)
  303. LOGINFO("Set screen mode " + toString(mWidth) + "x" + toString(mHeight) + " " + (mFullscreen ? "fullscreen" : "windowed"));
  304. else
  305. LOGINFO("Set screen mode " + toString(mWidth) + "x" + toString(mHeight) + " " + (mFullscreen ? "fullscreen" : "windowed") +
  306. " multisample " + toString(multiSample));
  307. }
  308. void Renderer::setMode(int width, int height)
  309. {
  310. setMode(mMode, width, height, mFullscreen, mVsync, mMultiSample);
  311. }
  312. void Renderer::setMode(RenderMode mode)
  313. {
  314. setMode(mode, mWidth, mHeight, mFullscreen, mVsync, mMultiSample);
  315. }
  316. void Renderer::toggleFullscreen()
  317. {
  318. setMode(mMode, mWidth, mHeight, !mFullscreen, mVsync, mMultiSample);
  319. }
  320. void Renderer::close()
  321. {
  322. if (mImpl->mWindow)
  323. {
  324. mDiffBuffer.reset();
  325. mNormalBuffer.reset();
  326. mDepthBuffer.reset();
  327. mImmediateVertexBuffers.clear();
  328. DestroyWindow(mImpl->mWindow);
  329. renderers.erase(mImpl->mWindow);
  330. mImpl->mWindow = 0;
  331. mClosed = true;
  332. }
  333. }
  334. bool Renderer::takeScreenShot(Image& destImage)
  335. {
  336. PROFILE(Renderer_TakeScreenShot);
  337. if (!mImpl->mDevice)
  338. return false;
  339. D3DSURFACE_DESC surfaceDesc;
  340. mImpl->mDefaultColorSurface->GetDesc(&surfaceDesc);
  341. // If possible, get the backbuffer data, because it is a lot faster.
  342. // However, if we are multisampled, need to use the front buffer
  343. bool useBackBuffer = true;
  344. if (mImpl->mPresentParams.MultiSampleType)
  345. {
  346. useBackBuffer = false;
  347. surfaceDesc.Format = D3DFMT_A8R8G8B8;
  348. }
  349. IDirect3DSurface9* surface = 0;
  350. mImpl->mDevice->CreateOffscreenPlainSurface(mWidth, mHeight, surfaceDesc.Format, D3DPOOL_SYSTEMMEM, &surface, 0);
  351. if (!surface)
  352. return false;
  353. if (useBackBuffer)
  354. mImpl->mDevice->GetRenderTargetData(mImpl->mDefaultColorSurface, surface);
  355. else
  356. mImpl->mDevice->GetFrontBufferData(0, surface);
  357. D3DLOCKED_RECT lockedRect;
  358. lockedRect.pBits = 0;
  359. surface->LockRect(&lockedRect, 0, D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
  360. if (!lockedRect.pBits)
  361. {
  362. surface->Release();
  363. return false;
  364. }
  365. destImage.setSize(mWidth, mHeight, 3);
  366. unsigned char* destData = destImage.getData();
  367. if (surfaceDesc.Format == D3DFMT_R5G6B5)
  368. {
  369. for (int y = 0; y < mHeight; ++y)
  370. {
  371. unsigned short* src = (unsigned short*)((unsigned char*)lockedRect.pBits + y * lockedRect.Pitch);
  372. unsigned char* dest = destData + y * mWidth * 3;
  373. for (int x = 0; x < mWidth; ++x)
  374. {
  375. unsigned short rgb = *src++;
  376. int b = rgb & 31;
  377. int g = (rgb >> 5) & 63;
  378. int r = (rgb >> 11);
  379. *dest++ = (int)(r * 255.0f / 31.0f);
  380. *dest++ = (int)(g * 255.0f / 63.0f);
  381. *dest++ = (int)(b * 255.0f / 31.0f);
  382. }
  383. }
  384. }
  385. else
  386. {
  387. for (int y = 0; y < mHeight; ++y)
  388. {
  389. unsigned char* src = (unsigned char*)lockedRect.pBits + y * lockedRect.Pitch;
  390. unsigned char* dest = destData + y * mWidth * 3;
  391. for (int x = 0; x < mWidth; ++x)
  392. {
  393. *dest++ = src[2];
  394. *dest++ = src[1];
  395. *dest++ = src[0];
  396. src += 4;
  397. }
  398. }
  399. }
  400. surface->UnlockRect();
  401. surface->Release();
  402. return true;
  403. }
  404. bool Renderer::beginFrame()
  405. {
  406. PROFILE(Renderer_BeginFrame);
  407. if (!mImpl->mDevice)
  408. EXCEPTION("Must have initial screen mode set before rendering");
  409. if (mClosed)
  410. return false;
  411. // Check for lost device before rendering
  412. HRESULT hr = mImpl->mDevice->TestCooperativeLevel();
  413. if (hr != D3D_OK)
  414. {
  415. mDeviceLost = true;
  416. // The device can not be reset yet, sleep and try again eventually
  417. if (hr == D3DERR_DEVICELOST)
  418. {
  419. Sleep(20);
  420. return false;
  421. }
  422. // The device is lost, but ready to be reset. Reset device but do not render on this frame yet
  423. if (hr == D3DERR_DEVICENOTRESET)
  424. {
  425. resetDevice();
  426. return false;
  427. }
  428. }
  429. mImpl->mDevice->BeginScene();
  430. // If a query was issued on the previous frame, wait for it to finish before beginning the next
  431. if ((mImpl->mFrameQuery) && (mQueryIssued))
  432. {
  433. while (mImpl->mFrameQuery->GetData(0, 0, D3DGETDATA_FLUSH) == S_FALSE)
  434. {
  435. }
  436. mQueryIssued = false;
  437. }
  438. // Set default rendertarget and depth buffer
  439. resetRenderTargets();
  440. mViewTexture = 0;
  441. // Cleanup textures from previous frame
  442. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  443. setTexture(i, 0);
  444. // Cleanup stream frequencies from previous frame
  445. resetStreamFrequencies();
  446. // Reset immediate mode vertex buffer positions
  447. for (std::map<unsigned, unsigned>::iterator i = mImmediateVertexBufferPos.begin(); i != mImmediateVertexBufferPos.end(); ++i)
  448. i->second = 0;
  449. mNumPrimitives = 0;
  450. mNumBatches = 0;
  451. sendEvent(EVENT_BEGINFRAME);
  452. return true;
  453. }
  454. void Renderer::endFrame(bool flushCommands)
  455. {
  456. PROFILE(Renderer_EndFrame);
  457. if (mClosed)
  458. return;
  459. sendEvent(EVENT_ENDFRAME);
  460. // Optionally flush GPU buffer to avoid control lag or framerate fluctuations due to pre-render
  461. if ((mImpl->mFrameQuery) && (flushCommands))
  462. {
  463. mImpl->mFrameQuery->Issue(D3DISSUE_END);
  464. mQueryIssued = true;
  465. }
  466. mImpl->mDevice->EndScene();
  467. mImpl->mDevice->Present(0, 0, 0, 0);
  468. }
  469. void Renderer::clear(unsigned flags, const Color& color, float depth, unsigned stencil)
  470. {
  471. DWORD d3dFlags = 0;
  472. if (flags & CLEAR_COLOR)
  473. d3dFlags |= D3DCLEAR_TARGET;
  474. if (flags & CLEAR_DEPTH)
  475. d3dFlags |= D3DCLEAR_ZBUFFER;
  476. if (flags & CLEAR_STENCIL)
  477. d3dFlags |= D3DCLEAR_STENCIL;
  478. mImpl->mDevice->Clear(0, 0, d3dFlags, getD3DColor(color), depth, stencil);
  479. }
  480. void Renderer::draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  481. unsigned instanceCount)
  482. {
  483. if (!indexCount)
  484. return;
  485. unsigned primitiveCount = 0;
  486. switch (type)
  487. {
  488. case TRIANGLE_LIST:
  489. primitiveCount = indexCount / 3;
  490. mImpl->mDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  491. break;
  492. case LINE_LIST:
  493. primitiveCount = indexCount / 2;
  494. mImpl->mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  495. break;
  496. }
  497. mNumPrimitives += primitiveCount * instanceCount;
  498. mNumBatches++;
  499. }
  500. void Renderer::draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount, unsigned instanceCount)
  501. {
  502. if (!vertexCount)
  503. return;
  504. unsigned primitiveCount;
  505. switch (type)
  506. {
  507. case TRIANGLE_LIST:
  508. primitiveCount = vertexCount / 3;
  509. mImpl->mDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vertexStart, primitiveCount);
  510. break;
  511. case LINE_LIST:
  512. primitiveCount = vertexCount / 2;
  513. mImpl->mDevice->DrawPrimitive(D3DPT_LINELIST, vertexStart, primitiveCount);
  514. break;
  515. }
  516. mNumPrimitives += primitiveCount * instanceCount;
  517. mNumBatches++;
  518. }
  519. void Renderer::setVertexBuffer(VertexBuffer* buffer)
  520. {
  521. vertexBuffers[0] = buffer;
  522. setVertexBuffers(vertexBuffers, elementMasks);
  523. }
  524. bool Renderer::setVertexBuffers(const std::vector<VertexBuffer*>& buffers, const std::vector<unsigned>& elementMasks)
  525. {
  526. if (buffers.size() > MAX_VERTEX_STREAMS)
  527. {
  528. LOGERROR("Too many vertex buffers");
  529. return false;
  530. }
  531. if (buffers.size() != elementMasks.size())
  532. {
  533. LOGERROR("Amount of element masks and vertex buffers does not match");
  534. return false;
  535. }
  536. // Build vertex declaration hash code out of the buffers & masks
  537. unsigned long long hash = 0;
  538. for (unsigned i = 0; i < buffers.size(); ++i)
  539. {
  540. if (!buffers[i])
  541. continue;
  542. hash |= buffers[i]->getHash(i, elementMasks[i]);
  543. }
  544. if (hash)
  545. {
  546. // If no previous vertex declaration for that hash, create new
  547. if (mVertexDeclarations.find(hash) == mVertexDeclarations.end())
  548. mVertexDeclarations[hash] = new VertexDeclaration(this, buffers, elementMasks);
  549. VertexDeclaration* declaration = mVertexDeclarations[hash];
  550. if (declaration != mVertexDeclaration)
  551. {
  552. mImpl->mDevice->SetVertexDeclaration(declaration->getDeclaration());
  553. mVertexDeclaration = declaration;
  554. }
  555. }
  556. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  557. {
  558. VertexBuffer* buffer = 0;
  559. if (i < buffers.size())
  560. buffer = buffers[i];
  561. if (buffer != mVertexBuffer[i])
  562. {
  563. if (buffer)
  564. mImpl->mDevice->SetStreamSource(i, (IDirect3DVertexBuffer9*)buffer->getObject(), 0, buffer->getVertexSize());
  565. else
  566. mImpl->mDevice->SetStreamSource(i, 0, 0, 0);
  567. mVertexBuffer[i] = buffer;
  568. }
  569. }
  570. return true;
  571. }
  572. bool Renderer::setVertexBuffers(const std::vector<SharedPtr<VertexBuffer> >& buffers, const std::vector<unsigned>&
  573. elementMasks)
  574. {
  575. if (buffers.size() > MAX_VERTEX_STREAMS)
  576. {
  577. LOGERROR("Too many vertex buffers");
  578. return false;
  579. }
  580. if (buffers.size() != elementMasks.size())
  581. {
  582. LOGERROR("Amount of element masks and vertex buffers does not match");
  583. return false;
  584. }
  585. // Build vertex declaration hash code out of the buffers & masks
  586. unsigned long long hash = 0;
  587. for (unsigned i = 0; i < buffers.size(); ++i)
  588. {
  589. if (!buffers[i])
  590. continue;
  591. hash |= buffers[i]->getHash(i, elementMasks[i]);
  592. }
  593. if (hash)
  594. {
  595. // If no previous vertex declaration for that hash, create new
  596. if (mVertexDeclarations.find(hash) == mVertexDeclarations.end())
  597. mVertexDeclarations[hash] = new VertexDeclaration(this, buffers, elementMasks);
  598. VertexDeclaration* declaration = mVertexDeclarations[hash];
  599. if (declaration != mVertexDeclaration)
  600. {
  601. mImpl->mDevice->SetVertexDeclaration(declaration->getDeclaration());
  602. mVertexDeclaration = declaration;
  603. }
  604. }
  605. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  606. {
  607. VertexBuffer* buffer = 0;
  608. if (i < buffers.size())
  609. buffer = buffers[i];
  610. if (buffer != mVertexBuffer[i])
  611. {
  612. if (buffer)
  613. mImpl->mDevice->SetStreamSource(i, (IDirect3DVertexBuffer9*)buffer->getObject(), 0, buffer->getVertexSize());
  614. else
  615. mImpl->mDevice->SetStreamSource(i, 0, 0, 0);
  616. mVertexBuffer[i] = buffer;
  617. }
  618. }
  619. return true;
  620. }
  621. void Renderer::setIndexBuffer(IndexBuffer* buffer)
  622. {
  623. if (buffer != mIndexBuffer)
  624. {
  625. if (buffer)
  626. mImpl->mDevice->SetIndices((IDirect3DIndexBuffer9*)buffer->getObject());
  627. else
  628. mImpl->mDevice->SetIndices(0);
  629. mIndexBuffer = buffer;
  630. }
  631. }
  632. void Renderer::setVertexShader(VertexShader* shader)
  633. {
  634. if (shader != mVertexShader)
  635. {
  636. if (shader)
  637. mImpl->mDevice->SetVertexShader((IDirect3DVertexShader9*)shader->getObject());
  638. else
  639. mImpl->mDevice->SetVertexShader(0);
  640. mVertexShader = shader;
  641. }
  642. }
  643. void Renderer::setVertexShaderConstant(unsigned index, const bool* data, unsigned count)
  644. {
  645. if (index >= MAX_CONSTANT_REGISTERS)
  646. return;
  647. mImpl->mDevice->SetVertexShaderConstantB(index, (const BOOL*)data, count);
  648. }
  649. void Renderer::setVertexShaderConstant(unsigned index, const float* data, unsigned count)
  650. {
  651. if (index >= MAX_CONSTANT_REGISTERS)
  652. return;
  653. mImpl->mDevice->SetVertexShaderConstantF(index, data, count / 4);
  654. }
  655. void Renderer::setVertexShaderConstant(unsigned index, const int* data, unsigned count)
  656. {
  657. if (index >= MAX_CONSTANT_REGISTERS)
  658. return;
  659. mImpl->mDevice->SetVertexShaderConstantI(index, data, count / 4);
  660. }
  661. void Renderer::setVertexShaderConstant(unsigned index, float value)
  662. {
  663. if (index >= MAX_CONSTANT_REGISTERS)
  664. return;
  665. static float data[4] =
  666. {
  667. 0.0f,
  668. 0.0f,
  669. 0.0f,
  670. 0.0f
  671. };
  672. data[0] = value;
  673. mImpl->mDevice->SetVertexShaderConstantF(index, &data[0], 1);
  674. }
  675. void Renderer::setVertexShaderConstant(unsigned index, const Color& color)
  676. {
  677. if (index >= MAX_CONSTANT_REGISTERS)
  678. return;
  679. mImpl->mDevice->SetVertexShaderConstantF(index, color.getData(), 1);
  680. }
  681. void Renderer::setVertexShaderConstant(unsigned index, const Matrix3& matrix)
  682. {
  683. if (index >= MAX_CONSTANT_REGISTERS)
  684. return;
  685. static float data[12];
  686. data[0] = matrix.m00;
  687. data[1] = matrix.m01;
  688. data[2] = matrix.m02;
  689. data[3] = 0.0f;
  690. data[4] = matrix.m10;
  691. data[5] = matrix.m11;
  692. data[6] = matrix.m12;
  693. data[7] = 0.0f;
  694. data[8] = matrix.m20;
  695. data[9] = matrix.m21;
  696. data[10] = matrix.m22;
  697. data[11] = 0.0f;
  698. mImpl->mDevice->SetVertexShaderConstantF(index, &data[0], 3);
  699. }
  700. void Renderer::setVertexShaderConstant(unsigned index, const Vector3& vector)
  701. {
  702. if (index >= MAX_CONSTANT_REGISTERS)
  703. return;
  704. static float data[4] =
  705. {
  706. 0.0f,
  707. 0.0f,
  708. 0.0f,
  709. 0.0f
  710. };
  711. data[0] = vector.mX;
  712. data[1] = vector.mY;
  713. data[2] = vector.mZ;
  714. mImpl->mDevice->SetVertexShaderConstantF(index, &data[0], 1);
  715. }
  716. void Renderer::setVertexShaderConstant(unsigned index, const Matrix4& matrix)
  717. {
  718. if (index >= MAX_CONSTANT_REGISTERS)
  719. return;
  720. mImpl->mDevice->SetVertexShaderConstantF(index, matrix.getData(), 4);
  721. }
  722. void Renderer::setVertexShaderConstant(unsigned index, const Vector4& vector)
  723. {
  724. if (index >= MAX_CONSTANT_REGISTERS)
  725. return;
  726. mImpl->mDevice->SetVertexShaderConstantF(index, vector.getData(), 1);
  727. }
  728. void Renderer::setVertexShaderConstant(unsigned index, const Matrix4x3& matrix)
  729. {
  730. if (index >= MAX_CONSTANT_REGISTERS)
  731. return;
  732. mImpl->mDevice->SetVertexShaderConstantF(index, matrix.getData(), 3);
  733. }
  734. void Renderer::setPixelShader(PixelShader* shader)
  735. {
  736. if (shader != mPixelShader)
  737. {
  738. if (shader)
  739. mImpl->mDevice->SetPixelShader((IDirect3DPixelShader9*)shader->getObject());
  740. else
  741. mImpl->mDevice->SetPixelShader(0);
  742. mPixelShader = shader;
  743. }
  744. }
  745. void Renderer::setPixelShaderConstant(unsigned index, const bool* data, unsigned count)
  746. {
  747. if (index >= MAX_CONSTANT_REGISTERS)
  748. return;
  749. mImpl->mDevice->SetPixelShaderConstantB(index, (const BOOL*)data, count);
  750. }
  751. void Renderer::setPixelShaderConstant(unsigned index, const float* data, unsigned count)
  752. {
  753. if (index >= MAX_CONSTANT_REGISTERS)
  754. return;
  755. mImpl->mDevice->SetPixelShaderConstantF(index, data, count / 4);
  756. }
  757. void Renderer::setPixelShaderConstant(unsigned index, const int* data, unsigned count)
  758. {
  759. if (index >= MAX_CONSTANT_REGISTERS)
  760. return;
  761. mImpl->mDevice->SetPixelShaderConstantI(index, data, count / 4);
  762. }
  763. void Renderer::setPixelShaderConstant(unsigned index, float value)
  764. {
  765. if (index >= MAX_CONSTANT_REGISTERS)
  766. return;
  767. static float data[4] =
  768. {
  769. 0.0f,
  770. 0.0f,
  771. 0.0f,
  772. 0.0f
  773. };
  774. data[0] = value;
  775. mImpl->mDevice->SetPixelShaderConstantF(index, &data[0], 1);
  776. }
  777. void Renderer::setPixelShaderConstant(unsigned index, const Color& color)
  778. {
  779. if (index >= MAX_CONSTANT_REGISTERS)
  780. return;
  781. mImpl->mDevice->SetPixelShaderConstantF(index, color.getData(), 1);
  782. }
  783. void Renderer::setPixelShaderConstant(unsigned index, const Matrix3& matrix)
  784. {
  785. if (index >= MAX_CONSTANT_REGISTERS)
  786. return;
  787. static float data[12];
  788. data[0] = matrix.m00;
  789. data[1] = matrix.m01;
  790. data[2] = matrix.m02;
  791. data[3] = 0.0f;
  792. data[4] = matrix.m10;
  793. data[5] = matrix.m11;
  794. data[6] = matrix.m12;
  795. data[7] = 0.0f;
  796. data[8] = matrix.m20;
  797. data[9] = matrix.m21;
  798. data[10] = matrix.m22;
  799. data[11] = 0.0f;
  800. mImpl->mDevice->SetPixelShaderConstantF(index, &data[0], 3);
  801. }
  802. void Renderer::setPixelShaderConstant(unsigned index, const Vector3& vector)
  803. {
  804. if (index >= MAX_CONSTANT_REGISTERS)
  805. return;
  806. static float data[4] =
  807. {
  808. 0.0f,
  809. 0.0f,
  810. 0.0f,
  811. 0.0f
  812. };
  813. data[0] = vector.mX;
  814. data[1] = vector.mY;
  815. data[2] = vector.mZ;
  816. mImpl->mDevice->SetPixelShaderConstantF(index, &data[0], 1);
  817. }
  818. void Renderer::setPixelShaderConstant(unsigned index, const Matrix4& matrix)
  819. {
  820. if (index >= MAX_CONSTANT_REGISTERS)
  821. return;
  822. mImpl->mDevice->SetPixelShaderConstantF(index, matrix.getData(), 4);
  823. }
  824. void Renderer::setPixelShaderConstant(unsigned index, const Vector4& vector)
  825. {
  826. if (index >= MAX_CONSTANT_REGISTERS)
  827. return;
  828. mImpl->mDevice->SetPixelShaderConstantF(index, vector.getData(), 1);
  829. }
  830. void Renderer::setPixelShaderConstant(unsigned index, const Matrix4x3& matrix)
  831. {
  832. if (index >= MAX_CONSTANT_REGISTERS)
  833. return;
  834. mImpl->mDevice->SetPixelShaderConstantF(index, matrix.getData(), 3);
  835. }
  836. void Renderer::setTexture(unsigned index, Texture* texture)
  837. {
  838. if (index >= MAX_TEXTURE_UNITS)
  839. return;
  840. // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
  841. if (texture)
  842. {
  843. if ((mRenderTarget[0]) && (mRenderTarget[0]->getParentTexture() == texture))
  844. texture = texture->getBackupTexture();
  845. // Check also for an user-specifiable view texture: this is to make forward & deferred rendering behave similarly
  846. // (during G-buffer rendering it would otherwise be valid to have the final rendertarget texture bound)
  847. else if (texture == mViewTexture)
  848. texture = texture->getBackupTexture();
  849. }
  850. if (texture != mTexture[index])
  851. {
  852. if (texture)
  853. mImpl->mDevice->SetTexture(index, (IDirect3DBaseTexture9*)texture->getObject());
  854. else
  855. mImpl->mDevice->SetTexture(index, 0);
  856. mTexture[index] = texture;
  857. }
  858. if (texture)
  859. {
  860. static const D3DTEXTUREFILTERTYPE d3dMinMagFilter[] =
  861. {
  862. D3DTEXF_POINT,
  863. D3DTEXF_LINEAR,
  864. D3DTEXF_LINEAR,
  865. D3DTEXF_ANISOTROPIC
  866. };
  867. static const D3DTEXTUREFILTERTYPE d3dMipFilter[] =
  868. {
  869. D3DTEXF_POINT,
  870. D3DTEXF_POINT,
  871. D3DTEXF_LINEAR,
  872. D3DTEXF_ANISOTROPIC
  873. };
  874. static const D3DTEXTUREADDRESS d3dAddressMode[] =
  875. {
  876. D3DTADDRESS_WRAP,
  877. D3DTADDRESS_MIRROR,
  878. D3DTADDRESS_CLAMP,
  879. D3DTADDRESS_BORDER
  880. };
  881. TextureFilterMode filterMode = texture->getFilterMode();
  882. if (filterMode == FILTER_DEFAULT)
  883. filterMode = mDefaultTextureFilterMode;
  884. D3DTEXTUREFILTERTYPE minMag, mip;
  885. minMag = d3dMinMagFilter[filterMode];
  886. if (minMag != mImpl->mMinMagFilter[index])
  887. {
  888. mImpl->mDevice->SetSamplerState(index, D3DSAMP_MAGFILTER, minMag);
  889. mImpl->mDevice->SetSamplerState(index, D3DSAMP_MINFILTER, minMag);
  890. mImpl->mMinMagFilter[index] = minMag;
  891. }
  892. mip = d3dMipFilter[filterMode];
  893. if (mip != mImpl->mMipFilter[index])
  894. {
  895. mImpl->mDevice->SetSamplerState(index, D3DSAMP_MIPFILTER, mip);
  896. mImpl->mMipFilter[index] = mip;
  897. }
  898. D3DTEXTUREADDRESS u, v;
  899. u = d3dAddressMode[texture->getAddressMode(COORD_U)];
  900. if (u != mImpl->mUAddressMode[index])
  901. {
  902. mImpl->mDevice->SetSamplerState(index, D3DSAMP_ADDRESSU, u);
  903. mImpl->mUAddressMode[index] = u;
  904. }
  905. v = d3dAddressMode[texture->getAddressMode(COORD_V)];
  906. if (v != mImpl->mVAddressMode[index])
  907. {
  908. mImpl->mDevice->SetSamplerState(index, D3DSAMP_ADDRESSV, v);
  909. mImpl->mVAddressMode[index] = v;
  910. }
  911. if ((u == D3DTADDRESS_BORDER) || (v == D3DTADDRESS_BORDER))
  912. {
  913. const Color& borderColor = texture->getBorderColor();
  914. if (borderColor != mImpl->mBorderColor[index])
  915. {
  916. mImpl->mDevice->SetSamplerState(index, D3DSAMP_BORDERCOLOR, getD3DColor(borderColor));
  917. mImpl->mBorderColor[index] = borderColor;
  918. }
  919. }
  920. }
  921. }
  922. void Renderer::setDefaultTextureFilterMode(TextureFilterMode mode)
  923. {
  924. mDefaultTextureFilterMode = mode;
  925. }
  926. void Renderer::resetRenderTargets()
  927. {
  928. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  929. setRenderTarget(i, (RenderSurface*)0);
  930. setDepthStencil((RenderSurface*)0);
  931. setViewport(IntRect(0, 0, mWidth, mHeight));
  932. }
  933. void Renderer::resetRenderTarget(unsigned index)
  934. {
  935. setRenderTarget(index, (RenderSurface*)0);
  936. }
  937. void Renderer::resetDepthStencil()
  938. {
  939. setDepthStencil((RenderSurface*)0);
  940. }
  941. void Renderer::setRenderTarget(unsigned index, RenderSurface* renderTarget)
  942. {
  943. if (index >= MAX_RENDERTARGETS)
  944. return;
  945. IDirect3DSurface9* newColorSurface = 0;
  946. if (renderTarget)
  947. {
  948. if (renderTarget->getUsage() != TEXTURE_RENDERTARGET)
  949. return;
  950. newColorSurface = (IDirect3DSurface9*)renderTarget->getSurface();
  951. }
  952. else
  953. {
  954. if (!index)
  955. newColorSurface = mImpl->mDefaultColorSurface;
  956. }
  957. mRenderTarget[index] = renderTarget;
  958. if (newColorSurface != mImpl->mColorSurface[index])
  959. {
  960. mImpl->mDevice->SetRenderTarget(index, newColorSurface);
  961. mImpl->mColorSurface[index] = newColorSurface;
  962. }
  963. // If the rendertarget is also bound as a texture, replace with backup texture or null
  964. if (renderTarget)
  965. {
  966. Texture* parentTexture = renderTarget->getParentTexture();
  967. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  968. {
  969. if (mTexture[i] == parentTexture)
  970. setTexture(i, mTexture[i]->getBackupTexture());
  971. }
  972. }
  973. if (!index)
  974. {
  975. // Viewport has been reset
  976. IntVector2 rtSize = getRenderTargetDimensions();
  977. mViewport = IntRect(0, 0, rtSize.mX, rtSize.mY);
  978. // Disable scissor test, needs to be re-enabled by the user
  979. setScissorTest(false);
  980. }
  981. }
  982. void Renderer::setRenderTarget(unsigned index, Texture2D* renderTexture)
  983. {
  984. RenderSurface* renderTarget = 0;
  985. if (renderTexture)
  986. renderTarget = renderTexture->getRenderSurface();
  987. setRenderTarget(index, renderTarget);
  988. }
  989. void Renderer::setDepthStencil(RenderSurface* depthStencil)
  990. {
  991. IDirect3DSurface9* newDepthStencilSurface = 0;
  992. if ((depthStencil) && (depthStencil->getUsage() == TEXTURE_DEPTHSTENCIL))
  993. {
  994. newDepthStencilSurface = (IDirect3DSurface9*)depthStencil->getSurface();
  995. mDepthStencil = depthStencil;
  996. }
  997. if (!newDepthStencilSurface)
  998. {
  999. newDepthStencilSurface = mImpl->mDefaultDepthStencilSurface;
  1000. mDepthStencil = 0;
  1001. }
  1002. if (newDepthStencilSurface != mImpl->mDepthStencilSurface)
  1003. {
  1004. mImpl->mDevice->SetDepthStencilSurface(newDepthStencilSurface);
  1005. mImpl->mDepthStencilSurface = newDepthStencilSurface;
  1006. }
  1007. }
  1008. void Renderer::setDepthStencil(Texture2D* depthTexture)
  1009. {
  1010. RenderSurface* depthStencil = 0;
  1011. if (depthTexture)
  1012. depthStencil = depthTexture->getRenderSurface();
  1013. setDepthStencil(depthStencil);
  1014. }
  1015. void Renderer::setViewport(const IntRect& rect)
  1016. {
  1017. IntVector2 size = getRenderTargetDimensions();
  1018. IntRect rectCopy = rect;
  1019. if (rectCopy.mRight <= rectCopy.mLeft)
  1020. rectCopy.mRight = rectCopy.mLeft + 1;
  1021. if (rectCopy.mBottom <= rectCopy.mTop)
  1022. rectCopy.mBottom = rectCopy.mTop + 1;
  1023. rectCopy.mLeft = clamp(rectCopy.mLeft, 0, size.mX);
  1024. rectCopy.mTop = clamp(rectCopy.mTop, 0, size.mY);
  1025. rectCopy.mRight = clamp(rectCopy.mRight, 0, size.mX);
  1026. rectCopy.mBottom = clamp(rectCopy.mBottom, 0, size.mY);
  1027. D3DVIEWPORT9 vp;
  1028. vp.MinZ = 0.0f;
  1029. vp.MaxZ = 1.0f;
  1030. vp.X = rectCopy.mLeft;
  1031. vp.Y = rectCopy.mTop;
  1032. vp.Width = rectCopy.mRight - rectCopy.mLeft;
  1033. vp.Height = rectCopy.mBottom - rectCopy.mTop;
  1034. mImpl->mDevice->SetViewport(&vp);
  1035. mViewport = rectCopy;
  1036. // Disable scissor test, needs to be re-enabled by the user
  1037. setScissorTest(false);
  1038. }
  1039. void Renderer::setViewTexture(Texture* texture)
  1040. {
  1041. mViewTexture = texture;
  1042. // Check for the view texture being currently bound
  1043. if (texture)
  1044. {
  1045. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1046. {
  1047. if (mTexture[i] == texture)
  1048. setTexture(i, mTexture[i]->getBackupTexture());
  1049. }
  1050. }
  1051. }
  1052. void Renderer::setAlphaTest(bool enable, CompareMode mode, float alphaRef)
  1053. {
  1054. if (enable != mAlphaTest)
  1055. {
  1056. mImpl->mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, enable ? TRUE : FALSE);
  1057. mAlphaTest = enable;
  1058. }
  1059. if (enable)
  1060. {
  1061. if (mode != mAlphaTestMode)
  1062. {
  1063. mImpl->mDevice->SetRenderState(D3DRS_ALPHAFUNC, d3dCmpFunc[mode]);
  1064. mAlphaTestMode = mode;
  1065. }
  1066. if (alphaRef < 0.0f)
  1067. alphaRef = 0.0f;
  1068. if (alphaRef > 1.0f)
  1069. alphaRef = 1.0f;
  1070. if (alphaRef != mAlphaRef)
  1071. {
  1072. mImpl->mDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)(alphaRef * 255.0f));
  1073. mAlphaRef = alphaRef;
  1074. }
  1075. }
  1076. }
  1077. void Renderer::setTextureAnisotropy(unsigned level)
  1078. {
  1079. if (level < 1)
  1080. level = 1;
  1081. if (level != mTextureAnisotropy)
  1082. {
  1083. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1084. mImpl->mDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, level);
  1085. mTextureAnisotropy = level;
  1086. }
  1087. }
  1088. void Renderer::setBlendMode(BlendMode mode)
  1089. {
  1090. if (mode != mBlendMode)
  1091. {
  1092. static const DWORD d3dBlendEnable[] =
  1093. {
  1094. FALSE,
  1095. TRUE,
  1096. TRUE,
  1097. TRUE,
  1098. TRUE,
  1099. TRUE,
  1100. TRUE
  1101. };
  1102. static const D3DBLEND d3dSrcBlend[] =
  1103. {
  1104. D3DBLEND_ONE,
  1105. D3DBLEND_ONE,
  1106. D3DBLEND_DESTCOLOR,
  1107. D3DBLEND_SRCALPHA,
  1108. D3DBLEND_SRCALPHA,
  1109. D3DBLEND_ONE,
  1110. D3DBLEND_INVDESTALPHA,
  1111. };
  1112. static const D3DBLEND d3dDestBlend[] =
  1113. {
  1114. D3DBLEND_ZERO,
  1115. D3DBLEND_ONE,
  1116. D3DBLEND_ZERO,
  1117. D3DBLEND_INVSRCALPHA,
  1118. D3DBLEND_ONE,
  1119. D3DBLEND_INVSRCALPHA,
  1120. D3DBLEND_DESTALPHA
  1121. };
  1122. if (d3dBlendEnable[mode] != mImpl->mBlendEnable)
  1123. {
  1124. mImpl->mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, d3dBlendEnable[mode]);
  1125. mImpl->mBlendEnable = d3dBlendEnable[mode];
  1126. }
  1127. if (mImpl->mBlendEnable)
  1128. {
  1129. if (d3dSrcBlend[mode] != mImpl->mSrcBlend)
  1130. {
  1131. mImpl->mDevice->SetRenderState(D3DRS_SRCBLEND, d3dSrcBlend[mode]);
  1132. mImpl->mSrcBlend = d3dSrcBlend[mode];
  1133. }
  1134. if (d3dDestBlend[mode] != mImpl->mDestBlend)
  1135. {
  1136. mImpl->mDevice->SetRenderState(D3DRS_DESTBLEND, d3dDestBlend[mode]);
  1137. mImpl->mDestBlend = d3dDestBlend[mode];
  1138. }
  1139. }
  1140. mBlendMode = mode;
  1141. }
  1142. }
  1143. void Renderer::setColorWrite(bool enable)
  1144. {
  1145. if (enable != mColorWrite)
  1146. {
  1147. mImpl->mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, enable ? D3DCOLORWRITEENABLE_RED |
  1148. D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA : 0);
  1149. mColorWrite = enable;
  1150. }
  1151. }
  1152. void Renderer::setCullMode(CullMode mode)
  1153. {
  1154. if (mode != mCullMode)
  1155. {
  1156. static const D3DCULL d3dCullMode[] =
  1157. {
  1158. D3DCULL_NONE,
  1159. D3DCULL_CCW,
  1160. D3DCULL_CW
  1161. };
  1162. mImpl->mDevice->SetRenderState(D3DRS_CULLMODE, d3dCullMode[mode]);
  1163. mCullMode = mode;
  1164. }
  1165. }
  1166. void Renderer::setDepthBias(float constantBias, float slopeScaledBias)
  1167. {
  1168. if (constantBias != mDepthConstantBias)
  1169. {
  1170. mImpl->mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&constantBias));
  1171. mDepthConstantBias = constantBias;
  1172. }
  1173. if (slopeScaledBias != mDepthSlopeScaledBias)
  1174. {
  1175. mImpl->mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&slopeScaledBias));
  1176. mDepthSlopeScaledBias = slopeScaledBias;
  1177. }
  1178. }
  1179. void Renderer::setDepthTest(CompareMode mode)
  1180. {
  1181. if (mode != mDepthTestMode)
  1182. {
  1183. mImpl->mDevice->SetRenderState(D3DRS_ZFUNC, d3dCmpFunc[mode]);
  1184. mDepthTestMode = mode;
  1185. }
  1186. }
  1187. void Renderer::setDepthWrite(bool enable)
  1188. {
  1189. if (enable != mDepthWrite)
  1190. {
  1191. mImpl->mDevice->SetRenderState(D3DRS_ZWRITEENABLE, enable ? TRUE : FALSE);
  1192. mDepthWrite = enable;
  1193. }
  1194. }
  1195. void Renderer::setFillMode(FillMode mode)
  1196. {
  1197. if (mode != mFillMode)
  1198. {
  1199. static const D3DFILLMODE d3dFillMode[] =
  1200. {
  1201. D3DFILL_SOLID,
  1202. D3DFILL_WIREFRAME
  1203. };
  1204. mImpl->mDevice->SetRenderState(D3DRS_FILLMODE, d3dFillMode[mode]);
  1205. mFillMode = mode;
  1206. }
  1207. }
  1208. void Renderer::setScissorTest(bool enable, const Rect& rect, bool borderInclusive)
  1209. {
  1210. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1211. // Disable scissor in that case to reduce state changes
  1212. if (rect == Rect::sFullRect)
  1213. enable = false;
  1214. // Check for illegal rect, disable in that case
  1215. if ((rect.mMax.mX < rect.mMin.mX) || (rect.mMax.mY < rect.mMin.mY))
  1216. enable = false;
  1217. if (enable)
  1218. {
  1219. IntVector2 rtSize(getRenderTargetDimensions());
  1220. IntVector2 viewSize(mViewport.mRight - mViewport.mLeft, mViewport.mBottom - mViewport.mTop);
  1221. IntVector2 viewPos(mViewport.mLeft, mViewport.mTop);
  1222. IntRect intRect;
  1223. int expand = borderInclusive ? 1 : 0;
  1224. intRect.mLeft = clamp((int)((rect.mMin.mX + 1.0f) * 0.5f * viewSize.mX) + viewPos.mX, 0, rtSize.mX - 1);
  1225. intRect.mTop = clamp((int)((-rect.mMax.mY + 1.0f) * 0.5f * viewSize.mY) + viewPos.mY, 0, rtSize.mY - 1);
  1226. intRect.mRight = clamp((int)((rect.mMax.mX + 1.0f) * 0.5f * viewSize.mX) + viewPos.mX + expand, 0, rtSize.mX);
  1227. intRect.mBottom = clamp((int)((-rect.mMin.mY + 1.0f) * 0.5f * viewSize.mY) + viewPos.mY + expand, 0, rtSize.mY);
  1228. if (intRect.mRight == intRect.mLeft)
  1229. intRect.mRight++;
  1230. if (intRect.mBottom == intRect.mTop)
  1231. intRect.mBottom++;
  1232. if ((intRect.mRight < intRect.mLeft) || (intRect.mBottom < intRect.mTop))
  1233. enable = false;
  1234. if ((enable) && (mScissorRect != intRect))
  1235. {
  1236. RECT d3dRect;
  1237. d3dRect.left = intRect.mLeft;
  1238. d3dRect.top = intRect.mTop;
  1239. d3dRect.right = intRect.mRight;
  1240. d3dRect.bottom = intRect.mBottom;
  1241. mImpl->mDevice->SetScissorRect(&d3dRect);
  1242. mScissorRect = intRect;
  1243. }
  1244. }
  1245. else
  1246. mScissorRect = IntRect::sZero;
  1247. if (enable != mScissorTest)
  1248. {
  1249. mImpl->mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enable ? TRUE : FALSE);
  1250. mScissorTest = enable;
  1251. }
  1252. }
  1253. void Renderer::setScissorTest(bool enable, const IntRect& rect)
  1254. {
  1255. IntVector2 rtSize(getRenderTargetDimensions());
  1256. IntVector2 viewSize(mViewport.mRight - mViewport.mLeft, mViewport.mBottom - mViewport.mTop);
  1257. IntVector2 viewPos(mViewport.mLeft, mViewport.mTop);
  1258. // Full scissor is same as disabling the test
  1259. if ((rect.mLeft <= 0) && (rect.mRight >= viewSize.mX) && (rect.mTop <= 0) && (rect.mBottom >= viewSize.mY))
  1260. enable = false;
  1261. // Check for illegal rect, disable in that case
  1262. if ((rect.mRight < rect.mLeft) || (rect.mBottom < rect.mTop))
  1263. enable = false;
  1264. if (enable)
  1265. {
  1266. IntRect intRect;
  1267. intRect.mLeft = clamp(rect.mLeft + viewPos.mX, 0, rtSize.mX - 1);
  1268. intRect.mTop = clamp(rect.mTop + viewPos.mY, 0, rtSize.mY - 1);
  1269. intRect.mRight = clamp(rect.mRight + viewPos.mX, 0, rtSize.mX);
  1270. intRect.mBottom = clamp(rect.mBottom + viewPos.mY, 0, rtSize.mY);
  1271. if (intRect.mRight == intRect.mLeft)
  1272. intRect.mRight++;
  1273. if (intRect.mBottom == intRect.mTop)
  1274. intRect.mBottom++;
  1275. if ((intRect.mRight < intRect.mLeft) || (intRect.mBottom < intRect.mTop))
  1276. enable = false;
  1277. if ((enable) && (mScissorRect != intRect))
  1278. {
  1279. RECT d3dRect;
  1280. d3dRect.left = intRect.mLeft;
  1281. d3dRect.top = intRect.mTop;
  1282. d3dRect.right = intRect.mRight;
  1283. d3dRect.bottom = intRect.mBottom;
  1284. mImpl->mDevice->SetScissorRect(&d3dRect);
  1285. mScissorRect = intRect;
  1286. }
  1287. }
  1288. else
  1289. mScissorRect = IntRect::sZero;
  1290. if (enable != mScissorTest)
  1291. {
  1292. mImpl->mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enable ? TRUE : FALSE);
  1293. mScissorTest = enable;
  1294. }
  1295. }
  1296. void Renderer::setStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned stencilMask)
  1297. {
  1298. if (enable != mStencilTest)
  1299. {
  1300. mImpl->mDevice->SetRenderState(D3DRS_STENCILENABLE, enable ? TRUE : FALSE);
  1301. mStencilTest = enable;
  1302. }
  1303. if (enable)
  1304. {
  1305. static const D3DSTENCILOP d3dStencilOp[] =
  1306. {
  1307. D3DSTENCILOP_KEEP,
  1308. D3DSTENCILOP_ZERO,
  1309. D3DSTENCILOP_REPLACE,
  1310. D3DSTENCILOP_INCR,
  1311. D3DSTENCILOP_DECR
  1312. };
  1313. if (mode != mStencilTestMode)
  1314. {
  1315. mImpl->mDevice->SetRenderState(D3DRS_STENCILFUNC, d3dCmpFunc[mode]);
  1316. mStencilTestMode = mode;
  1317. }
  1318. if (pass != mStencilPass)
  1319. {
  1320. mImpl->mDevice->SetRenderState(D3DRS_STENCILPASS, d3dStencilOp[pass]);
  1321. mStencilPass = pass;
  1322. }
  1323. if (fail != mStencilFail)
  1324. {
  1325. mImpl->mDevice->SetRenderState(D3DRS_STENCILFAIL, d3dStencilOp[fail]);
  1326. mStencilFail = fail;
  1327. }
  1328. if (zFail != mStencilZFail)
  1329. {
  1330. mImpl->mDevice->SetRenderState(D3DRS_STENCILZFAIL, d3dStencilOp[zFail]);
  1331. mStencilZFail = zFail;
  1332. }
  1333. if (stencilRef != mStencilRef)
  1334. {
  1335. mImpl->mDevice->SetRenderState(D3DRS_STENCILREF, stencilRef);
  1336. mStencilRef = stencilRef;
  1337. }
  1338. if (stencilMask != mStencilMask)
  1339. {
  1340. mImpl->mDevice->SetRenderState(D3DRS_STENCILMASK, stencilMask);
  1341. mStencilMask = stencilMask;
  1342. }
  1343. }
  1344. }
  1345. bool Renderer::setStreamFrequency(unsigned index, unsigned frequency)
  1346. {
  1347. if (index >= MAX_VERTEX_STREAMS)
  1348. {
  1349. LOGERROR("Illegal vertex stream index");
  1350. return false;
  1351. }
  1352. if (mStreamFrequency[index] != frequency)
  1353. {
  1354. mImpl->mDevice->SetStreamSourceFreq(index, frequency);
  1355. mStreamFrequency[index] = frequency;
  1356. }
  1357. return true;
  1358. }
  1359. void Renderer::resetStreamFrequencies()
  1360. {
  1361. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1362. {
  1363. if (mStreamFrequency[i] != 1)
  1364. {
  1365. mImpl->mDevice->SetStreamSourceFreq(i, 1);
  1366. mStreamFrequency[i] = 1;
  1367. }
  1368. }
  1369. }
  1370. bool Renderer::beginImmediate(PrimitiveType type, unsigned vertexCount, unsigned elementMask)
  1371. {
  1372. if (mImmediateBuffer)
  1373. {
  1374. LOGERROR("New immediate draw operation started before ending the last one");
  1375. return false;
  1376. }
  1377. if (!(elementMask & MASK_POSITION))
  1378. {
  1379. LOGERROR("Immediate draw operation must contain vertex positions");
  1380. return false;
  1381. }
  1382. if (!vertexCount)
  1383. return true;
  1384. // See if buffer exists for this vertex format. If not, create new
  1385. if (mImmediateVertexBuffers.find(elementMask) == mImmediateVertexBuffers.end())
  1386. {
  1387. VertexBuffer* newBuffer = new VertexBuffer(this, true);
  1388. unsigned size = vertexCount > IMMEDIATE_BUFFER_DEFAULT_SIZE ? vertexCount : IMMEDIATE_BUFFER_DEFAULT_SIZE;
  1389. newBuffer->setSize(size, elementMask);
  1390. mImmediateVertexBuffers[elementMask] = newBuffer;
  1391. mImmediateVertexBufferPos[elementMask] = 0;
  1392. }
  1393. // Resize buffer if it is too small
  1394. VertexBuffer* buffer = mImmediateVertexBuffers[elementMask];
  1395. if (buffer->getVertexCount() < vertexCount)
  1396. {
  1397. buffer->setSize(vertexCount, elementMask);
  1398. mImmediateVertexBufferPos[elementMask] = 0;
  1399. }
  1400. // Get the current lock position for the buffer
  1401. unsigned bufferPos = mImmediateVertexBufferPos[elementMask];
  1402. if (bufferPos + vertexCount >= buffer->getVertexCount())
  1403. bufferPos = 0;
  1404. LockMode lockMode = LOCK_DISCARD;
  1405. if (bufferPos != 0)
  1406. lockMode = LOCK_NOOVERWRITE;
  1407. // Note: the data pointer gets pre-decremented here, because the first call to defineVertex() will increment it
  1408. mImmediateDataPtr = ((unsigned char*)buffer->lock(bufferPos, vertexCount, lockMode)) - buffer->getVertexSize();
  1409. mImmediateBuffer = buffer;
  1410. mImmediateType= type;
  1411. mImmediateStartPos = bufferPos;
  1412. mImmediateVertexCount = vertexCount;
  1413. mImmediateCurrentVertex = 0;
  1414. // Store new buffer position for next lock into the same buffer
  1415. bufferPos += vertexCount;
  1416. if (bufferPos >= buffer->getVertexCount())
  1417. bufferPos = 0;
  1418. mImmediateVertexBufferPos[elementMask] = bufferPos;
  1419. return true;
  1420. }
  1421. bool Renderer::defineVertex(const Vector3& vertex)
  1422. {
  1423. if ((!mImmediateBuffer) || (mImmediateCurrentVertex >= mImmediateVertexCount))
  1424. return false;
  1425. mImmediateDataPtr += mImmediateBuffer->getVertexSize();
  1426. ++mImmediateCurrentVertex;
  1427. float* dest = (float*)(mImmediateDataPtr + mImmediateBuffer->getElementOffset(ELEMENT_POSITION));
  1428. const float* src = vertex.getData();
  1429. dest[0] = src[0];
  1430. dest[1] = src[1];
  1431. dest[2] = src[2];
  1432. return true;
  1433. }
  1434. bool Renderer::defineNormal(const Vector3& normal)
  1435. {
  1436. if ((!mImmediateBuffer) ||(!(mImmediateBuffer->getElementMask() & MASK_NORMAL)) || (!mImmediateCurrentVertex))
  1437. return false;
  1438. float* dest = (float*)(mImmediateDataPtr + mImmediateBuffer->getElementOffset(ELEMENT_NORMAL));
  1439. const float* src = normal.getData();
  1440. dest[0] = src[0];
  1441. dest[1] = src[1];
  1442. dest[2] = src[2];
  1443. return true;
  1444. }
  1445. bool Renderer::defineTexCoord(const Vector2& texCoord)
  1446. {
  1447. if ((!mImmediateBuffer) || (!(mImmediateBuffer->getElementMask() & MASK_TEXCOORD1)) || (!mImmediateCurrentVertex))
  1448. return false;
  1449. float* dest = (float*)(mImmediateDataPtr + mImmediateBuffer->getElementOffset(ELEMENT_TEXCOORD1));
  1450. const float* src = texCoord.getData();
  1451. dest[0] = src[0];
  1452. dest[1] = src[1];
  1453. return true;
  1454. }
  1455. bool Renderer::defineColor(const Color& color)
  1456. {
  1457. if ((!mImmediateBuffer) || (!(mImmediateBuffer->getElementMask() & MASK_COLOR)) || (!mImmediateCurrentVertex))
  1458. return false;
  1459. unsigned* dest = (unsigned*)(mImmediateDataPtr + mImmediateBuffer->getElementOffset(ELEMENT_COLOR));
  1460. *dest = getD3DColor(color);
  1461. return true;
  1462. }
  1463. bool Renderer::defineColor(unsigned color)
  1464. {
  1465. if ((!mImmediateBuffer) || (!(mImmediateBuffer->getElementMask() & MASK_COLOR)) || (!mImmediateCurrentVertex))
  1466. return false;
  1467. unsigned* dest = (unsigned*)(mImmediateDataPtr + mImmediateBuffer->getElementOffset(ELEMENT_COLOR));
  1468. *dest = color;
  1469. return true;
  1470. }
  1471. void Renderer::endImmediate()
  1472. {
  1473. if (mImmediateBuffer)
  1474. {
  1475. mImmediateBuffer->unlock();
  1476. setVertexBuffer(mImmediateBuffer);
  1477. draw(mImmediateType, mImmediateStartPos, mImmediateVertexCount);
  1478. mImmediateBuffer = 0;
  1479. }
  1480. }
  1481. void Renderer::setForceSM2(bool enable)
  1482. {
  1483. // Note: this only has effect before calling setMode() for the first time
  1484. mForceSM2 = enable;
  1485. }
  1486. unsigned char* Renderer::getImmediateDataPtr() const
  1487. {
  1488. if (!mImmediateBuffer)
  1489. {
  1490. LOGERROR("Immediate draw operation not started");
  1491. return 0;
  1492. }
  1493. // Pointer was pre-decremented in beginImmediate(). Undo that now
  1494. return mImmediateDataPtr + mImmediateBuffer->getVertexSize();
  1495. }
  1496. unsigned Renderer::getWindowHandle() const
  1497. {
  1498. return (unsigned)mImpl->mWindow;
  1499. }
  1500. std::vector<IntVector2> Renderer::getResolutions() const
  1501. {
  1502. std::vector<IntVector2> ret;
  1503. if (!mImpl->mInterface)
  1504. return ret;
  1505. D3DFORMAT fullscreenFormat = mImpl->getDesktopFormat();
  1506. unsigned numModes = mImpl->mInterface->GetAdapterModeCount(mImpl->mAdapter, fullscreenFormat);
  1507. D3DDISPLAYMODE displayMode;
  1508. for (unsigned i = 0; i < numModes; ++i)
  1509. {
  1510. if (FAILED(mImpl->mInterface->EnumAdapterModes(mImpl->mAdapter, fullscreenFormat, i, &displayMode)))
  1511. continue;
  1512. if (displayMode.Format != fullscreenFormat)
  1513. continue;
  1514. IntVector2 newMode(displayMode.Width, displayMode.Height);
  1515. // Check for duplicate before storing
  1516. bool unique = true;
  1517. for (unsigned j = 0; j < ret.size(); ++j)
  1518. {
  1519. if (ret[j] == newMode)
  1520. {
  1521. unique = false;
  1522. break;
  1523. }
  1524. }
  1525. if (unique)
  1526. ret.push_back(newMode);
  1527. }
  1528. return ret;
  1529. }
  1530. std::vector<int> Renderer::getMultiSampleLevels() const
  1531. {
  1532. std::vector<int> ret;
  1533. // No multisampling always supported
  1534. ret.push_back(0);
  1535. if (!mImpl->mInterface)
  1536. return ret;
  1537. for (unsigned i = (int)D3DMULTISAMPLE_2_SAMPLES; i < (int)D3DMULTISAMPLE_16_SAMPLES; ++i)
  1538. {
  1539. if (SUCCEEDED(mImpl->mInterface->CheckDeviceMultiSampleType(mImpl->mAdapter, mImpl->mDeviceType, D3DFMT_R8G8B8, FALSE,
  1540. (D3DMULTISAMPLE_TYPE)i, NULL)))
  1541. ret.push_back(i);
  1542. }
  1543. return ret;
  1544. }
  1545. VertexBuffer* Renderer::getVertexBuffer(unsigned index) const
  1546. {
  1547. return index < MAX_VERTEX_STREAMS ? mVertexBuffer[index] : 0;
  1548. }
  1549. Texture* Renderer::getTexture(unsigned index) const
  1550. {
  1551. return index < MAX_TEXTURE_UNITS ? mTexture[index] : 0;
  1552. }
  1553. RenderSurface* Renderer::getRenderTarget(unsigned index) const
  1554. {
  1555. return index < MAX_RENDERTARGETS ? mRenderTarget[index] : 0;
  1556. }
  1557. unsigned Renderer::getStreamFrequency(unsigned index) const
  1558. {
  1559. return index < MAX_VERTEX_STREAMS ? mStreamFrequency[index] : 0;
  1560. }
  1561. IntVector2 Renderer::getRenderTargetDimensions() const
  1562. {
  1563. int width, height;
  1564. if (mRenderTarget[0])
  1565. {
  1566. width = mRenderTarget[0]->getWidth();
  1567. height = mRenderTarget[0]->getHeight();
  1568. }
  1569. else
  1570. {
  1571. width = mWidth;
  1572. height = mHeight;
  1573. }
  1574. return IntVector2(width, height);
  1575. }
  1576. void Renderer::addGPUObject(GPUObject* object)
  1577. {
  1578. mGPUObjects.push_back(object);
  1579. }
  1580. void Renderer::removeGPUObject(GPUObject* object)
  1581. {
  1582. for (unsigned i = 0; i < mGPUObjects.size(); ++i)
  1583. {
  1584. if (mGPUObjects[i] == object)
  1585. {
  1586. mGPUObjects.erase(mGPUObjects.begin() + i);
  1587. return;
  1588. }
  1589. }
  1590. }
  1591. void Renderer::createWindow(int width, int height)
  1592. {
  1593. WNDCLASS wc;
  1594. wc.style = CS_HREDRAW | CS_VREDRAW;
  1595. wc.lpfnWndProc = wndProc;
  1596. wc.cbClsExtra = 0;
  1597. wc.cbWndExtra = 0;
  1598. wc.hInstance = mImpl->mInstance;
  1599. wc.hIcon = LoadIcon(0, IDI_APPLICATION);
  1600. wc.hCursor = LoadCursor(0, IDC_ARROW);
  1601. wc.hbrBackground = 0;
  1602. wc.lpszMenuName = 0;
  1603. wc.lpszClassName = "D3DWindow";
  1604. RegisterClass(&wc);
  1605. RECT rect = {0, 0, width, height};
  1606. AdjustWindowRect(&rect, windowStyle, false);
  1607. mImpl->mWindow = CreateWindow("D3DWindow", mWindowTitle.c_str(), windowStyle, CW_USEDEFAULT, CW_USEDEFAULT,
  1608. rect.right, rect.bottom, 0, 0, mImpl->mInstance, 0);
  1609. if (!mImpl->mWindow)
  1610. EXCEPTION("Could not create window");
  1611. // Save window placement
  1612. WINDOWPLACEMENT wndpl;
  1613. wndpl.length = sizeof wndpl;
  1614. if (SUCCEEDED(GetWindowPlacement(mImpl->mWindow, &wndpl)))
  1615. {
  1616. mWindowPosX = wndpl.rcNormalPosition.left;
  1617. mWindowPosY = wndpl.rcNormalPosition.top;
  1618. }
  1619. renderers[mImpl->mWindow] = this;
  1620. }
  1621. void Renderer::createInterface()
  1622. {
  1623. mImpl->mInterface = Direct3DCreate9(D3D9b_SDK_VERSION);
  1624. if (!mImpl->mInterface)
  1625. EXCEPTION("Could not create Direct3D9 interface");
  1626. if (FAILED(mImpl->mInterface->GetDeviceCaps(mImpl->mAdapter, mImpl->mDeviceType, &mImpl->mDeviceCaps)))
  1627. EXCEPTION("Could not get Direct3D capabilities");
  1628. if (FAILED(mImpl->mInterface->GetAdapterIdentifier(mImpl->mAdapter, 0, &mImpl->mAdapterIdentifier)))
  1629. EXCEPTION("Could not get Direct3D adapter identifier");
  1630. if (mImpl->mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  1631. EXCEPTION("Shader model 2.0 display adapter is required");
  1632. // Check supported features: Shader Model 3, deferred / light prepass rendering, hardware depth texture, shadow map,
  1633. // dummy color surface
  1634. if (!mForceSM2)
  1635. {
  1636. if ((mImpl->mDeviceCaps.VertexShaderVersion >= D3DVS_VERSION(3, 0)) && (mImpl->mDeviceCaps.PixelShaderVersion >=
  1637. D3DPS_VERSION(3, 0)))
  1638. mSM3Support = true;
  1639. }
  1640. if (mImpl->checkFormatSupport(D3DFMT_R32F, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1641. {
  1642. if (mImpl->mDeviceCaps.NumSimultaneousRTs >= 3)
  1643. mDeferredSupport = true;
  1644. if (mImpl->mDeviceCaps.NumSimultaneousRTs >= 2)
  1645. mPrepassSupport = true;
  1646. }
  1647. if (mImpl->checkFormatSupport((D3DFORMAT)MAKEFOURCC('I', 'N', 'T', 'Z'), D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1648. {
  1649. // Sampling INTZ buffer directly while also using it for depth test results in performance loss on ATI GPUs
  1650. // at least on Vista/Win7 (see http://aras-p.info/texts/D3D9GPUHacks.html). So, use INTZ buffer only with
  1651. // other vendors, or on Windows XP
  1652. unsigned windowsVersion = GetVersion() & 0xff;
  1653. if ((mImpl->mAdapterIdentifier.VendorId != 0x1002) || (windowsVersion < 6))
  1654. mHardwareDepthSupport = true;
  1655. }
  1656. // Prefer NVIDIA style hardware depth compared shadow maps if available
  1657. mShadowMapFormat = D3DFMT_D16;
  1658. if (mImpl->checkFormatSupport((D3DFORMAT)mShadowMapFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1659. {
  1660. mHardwareShadowSupport = true;
  1661. // Check for hires depth support
  1662. mHiresShadowMapFormat = D3DFMT_D24X8;
  1663. if (mImpl->checkFormatSupport((D3DFORMAT)mHiresShadowMapFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1664. mHiresShadowSupport = true;
  1665. else
  1666. mHiresShadowMapFormat = mShadowMapFormat;
  1667. }
  1668. else
  1669. {
  1670. // ATI DF16 format needs manual depth compare in the shader
  1671. mShadowMapFormat = MAKEFOURCC('D', 'F', '1', '6');
  1672. if (mImpl->checkFormatSupport((D3DFORMAT)mShadowMapFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1673. {
  1674. // Check for hires depth support
  1675. mHiresShadowMapFormat = MAKEFOURCC('D', 'F', '2', '4');
  1676. if (mImpl->checkFormatSupport((D3DFORMAT)mHiresShadowMapFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1677. mHiresShadowSupport = true;
  1678. else
  1679. mHiresShadowMapFormat = mShadowMapFormat;
  1680. }
  1681. else
  1682. {
  1683. // No depth texture shadow map support -> no shadows at all
  1684. mShadowMapFormat = D3DFMT_UNKNOWN;
  1685. mHiresShadowMapFormat = D3DFMT_UNKNOWN;
  1686. }
  1687. }
  1688. // Check for Intel 4 Series with an old driver, enable manual shadow map compare in that case
  1689. if (mShadowMapFormat == D3DFMT_D16)
  1690. {
  1691. if ((mImpl->mAdapterIdentifier.VendorId == 0x8086) && (mImpl->mAdapterIdentifier.DeviceId == 0x2a42) &&
  1692. (mImpl->mAdapterIdentifier.DriverVersion.QuadPart <= 0x0007000f000a05d0ULL))
  1693. mHardwareShadowSupport = false;
  1694. }
  1695. mDummyColorFormat = D3DFMT_A8R8G8B8;
  1696. D3DFORMAT nullFormat = (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L');
  1697. if (mImpl->checkFormatSupport(nullFormat, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1698. mDummyColorFormat = nullFormat;
  1699. else if (mImpl->checkFormatSupport(D3DFMT_R16F, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1700. mDummyColorFormat = D3DFMT_R16F;
  1701. else if (mImpl->checkFormatSupport(D3DFMT_R5G6B5, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1702. mDummyColorFormat = D3DFMT_R5G6B5;
  1703. else if (mImpl->checkFormatSupport(D3DFMT_A4R4G4B4, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1704. mDummyColorFormat = D3DFMT_A4R4G4B4;
  1705. }
  1706. void Renderer::createDevice(unsigned adapter, unsigned deviceType)
  1707. {
  1708. DWORD behaviorFlags = 0;
  1709. if (mImpl->mDeviceCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  1710. {
  1711. behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  1712. if (mImpl->mDeviceCaps.DevCaps & D3DDEVCAPS_PUREDEVICE)
  1713. behaviorFlags |= D3DCREATE_PUREDEVICE;
  1714. }
  1715. else
  1716. behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  1717. if (FAILED(mImpl->mInterface->CreateDevice(
  1718. adapter, // adapter
  1719. (D3DDEVTYPE)deviceType, // device type
  1720. mImpl->mWindow, // window associated with device
  1721. behaviorFlags, // vertex processing
  1722. &mImpl->mPresentParams, // present parameters
  1723. &mImpl->mDevice))) // return created device
  1724. EXCEPTION("Could not create Direct3D device");
  1725. mImpl->mAdapter = adapter;
  1726. mImpl->mDeviceType = (D3DDEVTYPE)deviceType;
  1727. onDeviceReset();
  1728. }
  1729. void Renderer::createRenderTargets()
  1730. {
  1731. if (mMode != RENDER_FORWARD)
  1732. {
  1733. // In deferred rendering, the diffuse buffer stores diffuse albedo. In light prepass, it is used for light accumulation
  1734. if (!mDiffBuffer)
  1735. {
  1736. mDiffBuffer = new Texture2D(this, TEXTURE_RENDERTARGET);
  1737. mDiffBuffer->setSize(0, 0, D3DFMT_A8R8G8B8);
  1738. }
  1739. if (!mNormalBuffer)
  1740. {
  1741. mNormalBuffer = new Texture2D(this, TEXTURE_RENDERTARGET);
  1742. mNormalBuffer->setSize(0, 0, D3DFMT_A8R8G8B8);
  1743. }
  1744. if (!mDepthBuffer)
  1745. {
  1746. if (!mHardwareDepthSupport)
  1747. {
  1748. mDepthBuffer = new Texture2D(this, TEXTURE_RENDERTARGET);
  1749. mDepthBuffer->setSize(0, 0, D3DFMT_R32F);
  1750. }
  1751. else
  1752. {
  1753. mDepthBuffer = new Texture2D(this, TEXTURE_DEPTHSTENCIL);
  1754. mDepthBuffer->setSize(0, 0, (D3DFORMAT)MAKEFOURCC('I', 'N', 'T', 'Z'));
  1755. }
  1756. }
  1757. // If edge filtering is used, reserve screen buffer
  1758. // (later we will probably want the screen buffer reserved in any case, to do for example distortion effects,
  1759. // which will also be useful in forward rendering)
  1760. if (mMultiSample)
  1761. {
  1762. mScreenBuffer = new Texture2D(this, TEXTURE_RENDERTARGET);
  1763. mScreenBuffer->setSize(0, 0, D3DFMT_X8R8G8B8);
  1764. mScreenBuffer->setFilterMode(FILTER_BILINEAR);
  1765. }
  1766. else
  1767. mScreenBuffer.reset();
  1768. }
  1769. else
  1770. {
  1771. mDiffBuffer.reset();
  1772. mNormalBuffer.reset();
  1773. mDepthBuffer.reset();
  1774. mScreenBuffer.reset();
  1775. }
  1776. }
  1777. void Renderer::resetDevice()
  1778. {
  1779. onDeviceLost();
  1780. if (SUCCEEDED(mImpl->mDevice->Reset(&mImpl->mPresentParams)))
  1781. {
  1782. mDeviceLost = false;
  1783. onDeviceReset();
  1784. }
  1785. }
  1786. void Renderer::onDeviceLost()
  1787. {
  1788. if (mImpl->mFrameQuery)
  1789. {
  1790. mImpl->mFrameQuery->Release();
  1791. mImpl->mFrameQuery = 0;
  1792. }
  1793. if (mImpl->mDefaultColorSurface)
  1794. {
  1795. mImpl->mDefaultColorSurface->Release();
  1796. mImpl->mDefaultColorSurface = 0;
  1797. }
  1798. if (mImpl->mDefaultDepthStencilSurface)
  1799. {
  1800. if (mSystemDepthStencil)
  1801. mImpl->mDefaultDepthStencilSurface->Release();
  1802. mImpl->mDefaultDepthStencilSurface = 0;
  1803. }
  1804. for (unsigned i = 0; i < mGPUObjects.size(); ++i)
  1805. mGPUObjects[i]->onDeviceLost();
  1806. }
  1807. void Renderer::onDeviceReset()
  1808. {
  1809. resetCachedState();
  1810. // Create frame query
  1811. mImpl->mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &mImpl->mFrameQuery);
  1812. // In case AutoDepthStencil is not used, depth buffering must be enabled manually
  1813. mImpl->mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  1814. // Create deferred rendering buffers now
  1815. createRenderTargets();
  1816. for (unsigned i = 0; i < mGPUObjects.size(); ++i)
  1817. mGPUObjects[i]->onDeviceReset();
  1818. // Get default surfaces
  1819. mImpl->mDevice->GetRenderTarget(0, &mImpl->mDefaultColorSurface);
  1820. if (mImpl->mPresentParams.EnableAutoDepthStencil)
  1821. {
  1822. mImpl->mDevice->GetDepthStencilSurface(&mImpl->mDefaultDepthStencilSurface);
  1823. mSystemDepthStencil = true;
  1824. }
  1825. else
  1826. {
  1827. mImpl->mDefaultDepthStencilSurface = (IDirect3DSurface9*)mDepthBuffer->getRenderSurface()->getSurface();
  1828. mSystemDepthStencil = false;
  1829. }
  1830. mImmediateBuffer = 0;
  1831. }
  1832. void Renderer::resetCachedState()
  1833. {
  1834. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1835. mVertexBuffer[i] = 0;
  1836. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1837. {
  1838. mTexture[i] = 0;
  1839. mImpl->mMinMagFilter[i] = D3DTEXF_POINT;
  1840. mImpl->mMipFilter[i] = D3DTEXF_NONE;
  1841. mImpl->mUAddressMode[i] = D3DTADDRESS_WRAP;
  1842. mImpl->mVAddressMode[i] = D3DTADDRESS_WRAP;
  1843. mImpl->mBorderColor[i] = Color(0.0f, 0.0f, 0.0f, 0.0f);
  1844. }
  1845. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1846. {
  1847. mRenderTarget[i] = 0;
  1848. mImpl->mColorSurface[i] = 0;
  1849. }
  1850. mDepthStencil = 0;
  1851. mImpl->mDepthStencilSurface = 0;
  1852. mViewport = IntRect(0, 0, mWidth, mHeight);
  1853. mViewTexture = 0;
  1854. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1855. mStreamFrequency[i] = 0;
  1856. mIndexBuffer = 0;
  1857. mVertexDeclaration = 0;
  1858. mVertexShader = 0;
  1859. mPixelShader = 0;
  1860. mBlendMode = BLEND_REPLACE;
  1861. mAlphaTest = false;
  1862. mAlphaTestMode = CMP_ALWAYS;
  1863. mAlphaRef = 0.0f;
  1864. mTextureAnisotropy = 1;
  1865. mColorWrite = true;
  1866. mCullMode = CULL_CCW;
  1867. mDepthConstantBias = 0.0f;
  1868. mDepthSlopeScaledBias = 0.0f;
  1869. mDepthTestMode = CMP_LESSEQUAL;
  1870. mDepthWrite = true;
  1871. mFillMode = FILL_SOLID;
  1872. mScissorTest = false;
  1873. mScissorRect = IntRect::sZero;
  1874. mStencilTest = false;
  1875. mStencilTestMode = CMP_ALWAYS;
  1876. mStencilPass = OP_KEEP;
  1877. mStencilFail = OP_KEEP;
  1878. mStencilZFail = OP_KEEP;
  1879. mStencilRef = 0;
  1880. mStencilMask = M_MAX_UNSIGNED;
  1881. mImpl->mBlendEnable = FALSE;
  1882. mImpl->mSrcBlend = D3DBLEND_ONE;
  1883. mImpl->mDestBlend = D3DBLEND_ZERO;
  1884. mQueryIssued = false;
  1885. }
  1886. void Renderer::handleWindowMessage(StringHash eventType, VariantMap& eventData)
  1887. {
  1888. using namespace WindowMessage;
  1889. if (eventData[P_WINDOW].getInt() != (int)mImpl->mWindow)
  1890. return;
  1891. switch (eventData[P_MSG].getInt())
  1892. {
  1893. case WM_CLOSE:
  1894. close();
  1895. eventData[P_HANDLED] = true;
  1896. break;
  1897. case WM_DESTROY:
  1898. mClosed = true;
  1899. eventData[P_HANDLED] = true;
  1900. break;
  1901. }
  1902. }
  1903. LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1904. {
  1905. using namespace WindowMessage;
  1906. std::map<HWND, Renderer*>::const_iterator i = renderers.find(hwnd);
  1907. if (i != renderers.end())
  1908. {
  1909. VariantMap eventData;
  1910. eventData[P_WINDOW] = (int)hwnd;
  1911. eventData[P_MSG] = (int)msg;
  1912. eventData[P_WPARAM] = (int)wParam;
  1913. eventData[P_LPARAM] = (int)lParam;
  1914. eventData[P_HANDLED] = false;
  1915. i->second->sendEvent(EVENT_WINDOWMESSAGE, eventData);
  1916. if (eventData[P_HANDLED].getBool())
  1917. return 0;
  1918. }
  1919. return DefWindowProc(hwnd, msg, wParam, lParam);
  1920. }