D3D9Graphics.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "AnimatedModel.h"
  24. #include "Animation.h"
  25. #include "AnimationController.h"
  26. #include "Camera.h"
  27. #include "Context.h"
  28. #include "CustomGeometry.h"
  29. #include "DebugRenderer.h"
  30. #include "DecalSet.h"
  31. #include "Graphics.h"
  32. #include "GraphicsEvents.h"
  33. #include "GraphicsImpl.h"
  34. #include "IndexBuffer.h"
  35. #include "Log.h"
  36. #include "Material.h"
  37. #include "Octree.h"
  38. #include "ParticleEmitter.h"
  39. #include "ProcessUtils.h"
  40. #include "Profiler.h"
  41. #include "Shader.h"
  42. #include "ShaderVariation.h"
  43. #include "Skybox.h"
  44. #include "StringUtils.h"
  45. #include "Technique.h"
  46. #include "Terrain.h"
  47. #include "TerrainPatch.h"
  48. #include "Texture2D.h"
  49. #include "TextureCube.h"
  50. #include "VertexBuffer.h"
  51. #include "VertexDeclaration.h"
  52. #include "Zone.h"
  53. #include "DebugNew.h"
  54. #ifdef _MSC_VER
  55. #pragma warning(disable:4355)
  56. #endif
  57. namespace Urho3D
  58. {
  59. extern "C" HWND WIN_GetWindowHandle(SDL_Window* window);
  60. static const D3DCMPFUNC d3dCmpFunc[] =
  61. {
  62. D3DCMP_ALWAYS,
  63. D3DCMP_EQUAL,
  64. D3DCMP_NOTEQUAL,
  65. D3DCMP_LESS,
  66. D3DCMP_LESSEQUAL,
  67. D3DCMP_GREATER,
  68. D3DCMP_GREATEREQUAL
  69. };
  70. static const D3DTEXTUREFILTERTYPE d3dMinMagFilter[] =
  71. {
  72. D3DTEXF_POINT,
  73. D3DTEXF_LINEAR,
  74. D3DTEXF_LINEAR,
  75. D3DTEXF_ANISOTROPIC
  76. };
  77. static const D3DTEXTUREFILTERTYPE d3dMipFilter[] =
  78. {
  79. D3DTEXF_POINT,
  80. D3DTEXF_POINT,
  81. D3DTEXF_LINEAR,
  82. D3DTEXF_ANISOTROPIC
  83. };
  84. static const D3DTEXTUREADDRESS d3dAddressMode[] =
  85. {
  86. D3DTADDRESS_WRAP,
  87. D3DTADDRESS_MIRROR,
  88. D3DTADDRESS_CLAMP,
  89. D3DTADDRESS_BORDER
  90. };
  91. static const DWORD d3dBlendEnable[] =
  92. {
  93. FALSE,
  94. TRUE,
  95. TRUE,
  96. TRUE,
  97. TRUE,
  98. TRUE,
  99. TRUE
  100. };
  101. static const D3DBLEND d3dSrcBlend[] =
  102. {
  103. D3DBLEND_ONE,
  104. D3DBLEND_ONE,
  105. D3DBLEND_DESTCOLOR,
  106. D3DBLEND_SRCALPHA,
  107. D3DBLEND_SRCALPHA,
  108. D3DBLEND_ONE,
  109. D3DBLEND_INVDESTALPHA,
  110. };
  111. static const D3DBLEND d3dDestBlend[] =
  112. {
  113. D3DBLEND_ZERO,
  114. D3DBLEND_ONE,
  115. D3DBLEND_ZERO,
  116. D3DBLEND_INVSRCALPHA,
  117. D3DBLEND_ONE,
  118. D3DBLEND_INVSRCALPHA,
  119. D3DBLEND_DESTALPHA
  120. };
  121. static const D3DCULL d3dCullMode[] =
  122. {
  123. D3DCULL_NONE,
  124. D3DCULL_CCW,
  125. D3DCULL_CW
  126. };
  127. static const D3DFILLMODE d3dFillMode[] =
  128. {
  129. D3DFILL_SOLID,
  130. D3DFILL_WIREFRAME,
  131. D3DFILL_POINT
  132. };
  133. static const D3DSTENCILOP d3dStencilOp[] =
  134. {
  135. D3DSTENCILOP_KEEP,
  136. D3DSTENCILOP_ZERO,
  137. D3DSTENCILOP_REPLACE,
  138. D3DSTENCILOP_INCR,
  139. D3DSTENCILOP_DECR
  140. };
  141. static unsigned GetD3DColor(const Color& color)
  142. {
  143. unsigned r = (unsigned)(Clamp(color.r_ * 255.0f, 0.0f, 255.0f));
  144. unsigned g = (unsigned)(Clamp(color.g_ * 255.0f, 0.0f, 255.0f));
  145. unsigned b = (unsigned)(Clamp(color.b_ * 255.0f, 0.0f, 255.0f));
  146. unsigned a = (unsigned)(Clamp(color.a_ * 255.0f, 0.0f, 255.0f));
  147. return (((a) & 0xff) << 24) | (((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff);
  148. }
  149. static unsigned depthStencilFormat = D3DFMT_D24S8;
  150. Graphics::Graphics(Context* context) :
  151. Object(context),
  152. impl_(new GraphicsImpl()),
  153. externalWindow_(0),
  154. width_(0),
  155. height_(0),
  156. multiSample_(1),
  157. windowPosX_(0),
  158. windowPosY_(0),
  159. fullscreen_(false),
  160. resizable_(false),
  161. vsync_(false),
  162. tripleBuffer_(false),
  163. sRGB_(false),
  164. deviceLost_(false),
  165. lightPrepassSupport_(false),
  166. deferredSupport_(false),
  167. hardwareShadowSupport_(false),
  168. streamOffsetSupport_(false),
  169. sRGBSupport_(false),
  170. sRGBWriteSupport_(false),
  171. hasSM3_(false),
  172. forceSM2_(false),
  173. numPrimitives_(0),
  174. numBatches_(0),
  175. maxScratchBufferRequest_(0),
  176. defaultTextureFilterMode_(FILTER_BILINEAR)
  177. {
  178. SetTextureUnitMappings();
  179. // Initialize SDL now. Graphics should be the first SDL-using subsystem to be created
  180. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE);
  181. // Register Graphics library object factories
  182. RegisterGraphicsLibrary(context_);
  183. }
  184. Graphics::~Graphics()
  185. {
  186. // Release all GPU objects that still exist
  187. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  188. (*i)->Release();
  189. gpuObjects_.Clear();
  190. vertexDeclarations_.Clear();
  191. if (impl_->defaultColorSurface_)
  192. {
  193. impl_->defaultColorSurface_->Release();
  194. impl_->defaultColorSurface_ = 0;
  195. }
  196. if (impl_->defaultDepthStencilSurface_)
  197. {
  198. impl_->defaultDepthStencilSurface_->Release();
  199. impl_->defaultDepthStencilSurface_ = 0;
  200. }
  201. if (impl_->device_)
  202. {
  203. impl_->device_->Release();
  204. impl_->device_ = 0;
  205. }
  206. if (impl_->interface_)
  207. {
  208. impl_->interface_->Release();
  209. impl_->interface_ = 0;
  210. }
  211. if (impl_->window_)
  212. {
  213. SDL_ShowCursor(SDL_TRUE);
  214. SDL_DestroyWindow(impl_->window_);
  215. impl_->window_ = 0;
  216. }
  217. delete impl_;
  218. impl_ = 0;
  219. // Shut down SDL now. Graphics should be the last SDL-using subsystem to be destroyed
  220. SDL_Quit();
  221. }
  222. void Graphics::SetExternalWindow(void* window)
  223. {
  224. if (!impl_->window_)
  225. externalWindow_ = window;
  226. else
  227. LOGERROR("Window already opened, can not set external window");
  228. }
  229. void Graphics::SetWindowTitle(const String& windowTitle)
  230. {
  231. windowTitle_ = windowTitle;
  232. if (impl_->window_)
  233. SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
  234. }
  235. bool Graphics::SetMode(int width, int height, bool fullscreen, bool resizable, bool vsync, bool tripleBuffer, int multiSample)
  236. {
  237. PROFILE(SetScreenMode);
  238. // Find out the full screen mode display format (match desktop color depth)
  239. SDL_DisplayMode mode;
  240. SDL_GetDesktopDisplayMode(0, &mode);
  241. D3DFORMAT fullscreenFormat = SDL_BITSPERPIXEL(mode.format) == 16 ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8;
  242. // If zero dimensions in windowed mode, set default. If zero in fullscreen, use desktop mode
  243. if (!width || !height)
  244. {
  245. if (!fullscreen)
  246. {
  247. width = 1024;
  248. height = 768;
  249. }
  250. else
  251. {
  252. width = mode.w;
  253. height = mode.h;
  254. }
  255. }
  256. // Fullscreen can not be resizable
  257. if (fullscreen)
  258. resizable = false;
  259. multiSample = Clamp(multiSample, 1, (int)D3DMULTISAMPLE_16_SAMPLES);
  260. // If nothing changes, do not reset the device
  261. if (width == width_ && height == height_ && fullscreen == fullscreen_ && resizable == resizable_ &&
  262. vsync == vsync_ && tripleBuffer == tripleBuffer_ && multiSample == multiSample_)
  263. return true;
  264. if (!impl_->window_)
  265. {
  266. if (!OpenWindow(width, height, resizable))
  267. return false;
  268. }
  269. if (!impl_->interface_)
  270. {
  271. if (!CreateInterface())
  272. return false;
  273. CheckFeatureSupport();
  274. }
  275. // Note: GetMultiSample() will not reflect the actual hardware multisample mode, but rather what the caller wanted.
  276. multiSample_ = multiSample;
  277. // Check fullscreen mode validity. If not valid, revert to windowed
  278. if (fullscreen)
  279. {
  280. PODVector<IntVector2> resolutions = GetResolutions();
  281. fullscreen = false;
  282. for (unsigned i = 0; i < resolutions.Size(); ++i)
  283. {
  284. if (width == resolutions[i].x_ && height == resolutions[i].y_)
  285. {
  286. fullscreen = true;
  287. break;
  288. }
  289. }
  290. }
  291. // Fall back to non-multisampled if unsupported multisampling mode
  292. if (multiSample > 1)
  293. {
  294. if (FAILED(impl_->interface_->CheckDeviceMultiSampleType(impl_->adapter_, impl_->deviceType_, fullscreenFormat, FALSE,
  295. (D3DMULTISAMPLE_TYPE)multiSample, NULL)))
  296. multiSample = 1;
  297. }
  298. AdjustWindow(width, height, fullscreen);
  299. if (fullscreen)
  300. {
  301. impl_->presentParams_.BackBufferFormat = fullscreenFormat;
  302. impl_->presentParams_.Windowed = false;
  303. }
  304. else
  305. {
  306. impl_->presentParams_.BackBufferFormat = D3DFMT_UNKNOWN;
  307. impl_->presentParams_.Windowed = true;
  308. }
  309. impl_->presentParams_.BackBufferWidth = width;
  310. impl_->presentParams_.BackBufferHeight = height;
  311. impl_->presentParams_.BackBufferCount = tripleBuffer ? 2 : 1;
  312. impl_->presentParams_.MultiSampleType = multiSample > 1 ? (D3DMULTISAMPLE_TYPE)multiSample : D3DMULTISAMPLE_NONE;
  313. impl_->presentParams_.MultiSampleQuality = 0;
  314. impl_->presentParams_.SwapEffect = D3DSWAPEFFECT_DISCARD;
  315. impl_->presentParams_.hDeviceWindow = WIN_GetWindowHandle(impl_->window_);
  316. impl_->presentParams_.EnableAutoDepthStencil = TRUE;
  317. impl_->presentParams_.AutoDepthStencilFormat = D3DFMT_D24S8;
  318. impl_->presentParams_.Flags = D3DPRESENT_LINEAR_CONTENT;
  319. impl_->presentParams_.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  320. if (vsync)
  321. impl_->presentParams_.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  322. else
  323. impl_->presentParams_.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  324. width_ = width;
  325. height_ = height;
  326. fullscreen_ = fullscreen;
  327. resizable_ = resizable;
  328. vsync_ = vsync;
  329. tripleBuffer_ = tripleBuffer;
  330. if (!impl_->device_)
  331. {
  332. unsigned adapter = D3DADAPTER_DEFAULT;
  333. unsigned deviceType = D3DDEVTYPE_HAL;
  334. // Check for PerfHUD adapter
  335. for (unsigned i=0; i < impl_->interface_->GetAdapterCount(); ++i)
  336. {
  337. D3DADAPTER_IDENTIFIER9 identifier;
  338. impl_->interface_->GetAdapterIdentifier(i, 0, &identifier);
  339. if (strstr(identifier.Description, "PerfHUD") != 0)
  340. {
  341. adapter = i;
  342. deviceType = D3DDEVTYPE_REF;
  343. break;
  344. }
  345. }
  346. impl_->interface_->GetAdapterIdentifier(adapter, 0, &impl_->adapterIdentifier_);
  347. if (!CreateDevice(adapter, deviceType))
  348. return false;
  349. }
  350. else
  351. ResetDevice();
  352. // Clear the initial window contents to black
  353. impl_->device_->BeginScene();
  354. Clear(CLEAR_COLOR);
  355. impl_->device_->EndScene();
  356. impl_->device_->Present(0, 0, 0, 0);
  357. #ifdef ENABLE_LOGGING
  358. String msg;
  359. msg.AppendWithFormat("Set screen mode %dx%d %s", width_, height_, (fullscreen_ ? "fullscreen" : "windowed"));
  360. if (resizable_)
  361. msg.Append(" resizable");
  362. if (multiSample > 1)
  363. msg.AppendWithFormat(" multisample %d", multiSample);
  364. LOGINFO(msg);
  365. #endif
  366. using namespace ScreenMode;
  367. VariantMap eventData;
  368. eventData[P_WIDTH] = width_;
  369. eventData[P_HEIGHT] = height_;
  370. eventData[P_FULLSCREEN] = fullscreen_;
  371. eventData[P_RESIZABLE] = resizable_;
  372. SendEvent(E_SCREENMODE, eventData);
  373. return true;
  374. }
  375. bool Graphics::SetMode(int width, int height)
  376. {
  377. return SetMode(width, height, fullscreen_, resizable_, vsync_, tripleBuffer_, multiSample_);
  378. }
  379. void Graphics::SetSRGB(bool enable)
  380. {
  381. sRGB_ = enable && sRGBWriteSupport_;
  382. }
  383. bool Graphics::ToggleFullscreen()
  384. {
  385. return SetMode(width_, height_, !fullscreen_, resizable_, vsync_, tripleBuffer_, multiSample_);
  386. }
  387. void Graphics::Close()
  388. {
  389. if (impl_->window_)
  390. {
  391. SDL_ShowCursor(SDL_TRUE);
  392. SDL_DestroyWindow(impl_->window_);
  393. impl_->window_ = 0;
  394. }
  395. }
  396. bool Graphics::TakeScreenShot(Image& destImage)
  397. {
  398. PROFILE(TakeScreenShot);
  399. if (!impl_->device_)
  400. return false;
  401. D3DSURFACE_DESC surfaceDesc;
  402. impl_->defaultColorSurface_->GetDesc(&surfaceDesc);
  403. // If possible, get the backbuffer data, because it is a lot faster.
  404. // However, if we are multisampled, need to use the front buffer
  405. bool useBackBuffer = true;
  406. if (impl_->presentParams_.MultiSampleType)
  407. {
  408. useBackBuffer = false;
  409. surfaceDesc.Format = D3DFMT_A8R8G8B8;
  410. }
  411. IDirect3DSurface9* surface = 0;
  412. impl_->device_->CreateOffscreenPlainSurface(width_, height_, surfaceDesc.Format, D3DPOOL_SYSTEMMEM, &surface, 0);
  413. if (!surface)
  414. return false;
  415. if (useBackBuffer)
  416. impl_->device_->GetRenderTargetData(impl_->defaultColorSurface_, surface);
  417. else
  418. impl_->device_->GetFrontBufferData(0, surface);
  419. D3DLOCKED_RECT lockedRect;
  420. lockedRect.pBits = 0;
  421. surface->LockRect(&lockedRect, 0, D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
  422. if (!lockedRect.pBits)
  423. {
  424. surface->Release();
  425. return false;
  426. }
  427. destImage.SetSize(width_, height_, 3);
  428. unsigned char* destData = destImage.GetData();
  429. if (surfaceDesc.Format == D3DFMT_R5G6B5)
  430. {
  431. for (int y = 0; y < height_; ++y)
  432. {
  433. unsigned short* src = (unsigned short*)((unsigned char*)lockedRect.pBits + y * lockedRect.Pitch);
  434. unsigned char* dest = destData + y * width_ * 3;
  435. for (int x = 0; x < width_; ++x)
  436. {
  437. unsigned short rgb = *src++;
  438. int b = rgb & 31;
  439. int g = (rgb >> 5) & 63;
  440. int r = (rgb >> 11);
  441. *dest++ = (int)(r * 255.0f / 31.0f);
  442. *dest++ = (int)(g * 255.0f / 63.0f);
  443. *dest++ = (int)(b * 255.0f / 31.0f);
  444. }
  445. }
  446. }
  447. else
  448. {
  449. for (int y = 0; y < height_; ++y)
  450. {
  451. unsigned char* src = (unsigned char*)lockedRect.pBits + y * lockedRect.Pitch;
  452. unsigned char* dest = destData + y * width_ * 3;
  453. for (int x = 0; x < width_; ++x)
  454. {
  455. *dest++ = src[2];
  456. *dest++ = src[1];
  457. *dest++ = src[0];
  458. src += 4;
  459. }
  460. }
  461. }
  462. surface->UnlockRect();
  463. surface->Release();
  464. return true;
  465. }
  466. bool Graphics::BeginFrame()
  467. {
  468. if (!IsInitialized())
  469. return false;
  470. // If using an external window, check it for size changes, and reset screen mode if necessary
  471. if (externalWindow_)
  472. {
  473. int width, height;
  474. SDL_GetWindowSize(impl_->window_, &width, &height);
  475. if (width != width_ || height != height_)
  476. SetMode(width, height);
  477. }
  478. // Check for lost device before rendering
  479. HRESULT hr = impl_->device_->TestCooperativeLevel();
  480. if (hr != D3D_OK)
  481. {
  482. PROFILE(DeviceLost);
  483. deviceLost_ = true;
  484. // The device can not be reset yet, sleep and try again eventually
  485. if (hr == D3DERR_DEVICELOST)
  486. {
  487. Sleep(20);
  488. return false;
  489. }
  490. // The device is lost, but ready to be reset. Reset device but do not render on this frame yet
  491. if (hr == D3DERR_DEVICENOTRESET)
  492. {
  493. ResetDevice();
  494. return false;
  495. }
  496. }
  497. impl_->device_->BeginScene();
  498. // Set default rendertarget and depth buffer
  499. ResetRenderTargets();
  500. // Cleanup textures from previous frame
  501. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  502. SetTexture(i, 0);
  503. // Cleanup stream frequencies from previous frame
  504. ResetStreamFrequencies();
  505. numPrimitives_ = 0;
  506. numBatches_ = 0;
  507. SendEvent(E_BEGINRENDERING);
  508. return true;
  509. }
  510. void Graphics::EndFrame()
  511. {
  512. if (!IsInitialized())
  513. return;
  514. PROFILE(Present);
  515. SendEvent(E_ENDRENDERING);
  516. impl_->device_->EndScene();
  517. impl_->device_->Present(0, 0, 0, 0);
  518. // Clean up too large scratch buffers
  519. CleanupScratchBuffers();
  520. }
  521. void Graphics::Clear(unsigned flags, const Color& color, float depth, unsigned stencil)
  522. {
  523. DWORD d3dFlags = 0;
  524. if (flags & CLEAR_COLOR)
  525. d3dFlags |= D3DCLEAR_TARGET;
  526. if (flags & CLEAR_DEPTH)
  527. d3dFlags |= D3DCLEAR_ZBUFFER;
  528. if (flags & CLEAR_STENCIL)
  529. d3dFlags |= D3DCLEAR_STENCIL;
  530. impl_->device_->Clear(0, 0, d3dFlags, GetD3DColor(color), depth, stencil);
  531. }
  532. bool Graphics::ResolveToTexture(Texture2D* destination, const IntRect& viewport)
  533. {
  534. if (!destination || !destination->GetRenderSurface() || destination->GetWidth() != width_ ||
  535. destination->GetHeight() != height_)
  536. return false;
  537. PROFILE(ResolveToTexture);
  538. IntRect vpCopy = viewport;
  539. if (vpCopy.right_ <= vpCopy.left_)
  540. vpCopy.right_ = vpCopy.left_ + 1;
  541. if (vpCopy.bottom_ <= vpCopy.top_)
  542. vpCopy.bottom_ = vpCopy.top_ + 1;
  543. RECT rect;
  544. rect.left = Clamp(vpCopy.left_, 0, width_);
  545. rect.top = Clamp(vpCopy.top_, 0, height_);
  546. rect.right = Clamp(vpCopy.right_, 0, width_);
  547. rect.bottom = Clamp(vpCopy.bottom_, 0, height_);
  548. return SUCCEEDED(impl_->device_->StretchRect(impl_->defaultColorSurface_, &rect,
  549. (IDirect3DSurface9*)destination->GetRenderSurface()->GetSurface(), &rect, D3DTEXF_NONE));
  550. }
  551. void Graphics::Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount)
  552. {
  553. if (!vertexCount)
  554. return;
  555. ResetStreamFrequencies();
  556. unsigned primitiveCount = 0;
  557. switch (type)
  558. {
  559. case TRIANGLE_LIST:
  560. primitiveCount = vertexCount / 3;
  561. impl_->device_->DrawPrimitive(D3DPT_TRIANGLELIST, vertexStart, primitiveCount);
  562. break;
  563. case LINE_LIST:
  564. primitiveCount = vertexCount / 2;
  565. impl_->device_->DrawPrimitive(D3DPT_LINELIST, vertexStart, primitiveCount);
  566. break;
  567. }
  568. numPrimitives_ += primitiveCount;
  569. ++numBatches_;
  570. }
  571. void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  572. {
  573. if (!indexCount)
  574. return;
  575. ResetStreamFrequencies();
  576. unsigned primitiveCount = 0;
  577. switch (type)
  578. {
  579. case TRIANGLE_LIST:
  580. primitiveCount = indexCount / 3;
  581. impl_->device_->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  582. break;
  583. case LINE_LIST:
  584. primitiveCount = indexCount / 2;
  585. impl_->device_->DrawIndexedPrimitive(D3DPT_LINELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  586. break;
  587. }
  588. numPrimitives_ += primitiveCount;
  589. ++numBatches_;
  590. }
  591. void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  592. unsigned instanceCount)
  593. {
  594. if (!indexCount || !instanceCount)
  595. return;
  596. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  597. {
  598. VertexBuffer* buffer = vertexBuffers_[i];
  599. if (buffer)
  600. {
  601. if (buffer->GetElementMask() & MASK_INSTANCEMATRIX1)
  602. SetStreamFrequency(i, D3DSTREAMSOURCE_INSTANCEDATA | 1);
  603. else
  604. SetStreamFrequency(i, D3DSTREAMSOURCE_INDEXEDDATA | instanceCount);
  605. }
  606. }
  607. unsigned primitiveCount = 0;
  608. switch (type)
  609. {
  610. case TRIANGLE_LIST:
  611. primitiveCount = indexCount / 3;
  612. impl_->device_->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  613. break;
  614. case LINE_LIST:
  615. primitiveCount = indexCount / 2;
  616. impl_->device_->DrawIndexedPrimitive(D3DPT_LINELIST, 0, minVertex, vertexCount, indexStart, primitiveCount);
  617. break;
  618. }
  619. numPrimitives_ += instanceCount * primitiveCount;
  620. ++numBatches_;
  621. }
  622. void Graphics::SetVertexBuffer(VertexBuffer* buffer)
  623. {
  624. Vector<VertexBuffer*> vertexBuffers(1);
  625. PODVector<unsigned> elementMasks(1);
  626. vertexBuffers[0] = buffer;
  627. elementMasks[0] = MASK_DEFAULT;
  628. SetVertexBuffers(vertexBuffers, elementMasks);
  629. }
  630. bool Graphics::SetVertexBuffers(const Vector<VertexBuffer*>& buffers, const PODVector<unsigned>&
  631. elementMasks, unsigned instanceOffset)
  632. {
  633. if (buffers.Size() > MAX_VERTEX_STREAMS)
  634. {
  635. LOGERROR("Too many vertex buffers");
  636. return false;
  637. }
  638. if (buffers.Size() != elementMasks.Size())
  639. {
  640. LOGERROR("Amount of element masks and vertex buffers does not match");
  641. return false;
  642. }
  643. // Build vertex declaration hash code out of the buffers & masks
  644. unsigned long long hash = 0;
  645. for (unsigned i = 0; i < buffers.Size(); ++i)
  646. {
  647. if (!buffers[i])
  648. continue;
  649. hash |= buffers[i]->GetBufferHash(i, elementMasks[i]);
  650. }
  651. if (hash)
  652. {
  653. // If no previous vertex declaration for that hash, create new
  654. if (!vertexDeclarations_.Contains(hash))
  655. {
  656. SharedPtr<VertexDeclaration> newDeclaration(new VertexDeclaration(this, buffers, elementMasks));
  657. if (!newDeclaration->GetDeclaration())
  658. {
  659. LOGERROR("Failed to create vertex declaration");
  660. return false;
  661. }
  662. vertexDeclarations_[hash] = newDeclaration;
  663. }
  664. VertexDeclaration* declaration = vertexDeclarations_[hash];
  665. if (declaration != vertexDeclaration_)
  666. {
  667. impl_->device_->SetVertexDeclaration(declaration->GetDeclaration());
  668. vertexDeclaration_ = declaration;
  669. }
  670. }
  671. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  672. {
  673. VertexBuffer* buffer = 0;
  674. unsigned offset = 0;
  675. if (i < buffers.Size())
  676. {
  677. buffer = buffers[i];
  678. if (buffer && buffer->GetElementMask() & MASK_INSTANCEMATRIX1)
  679. offset = instanceOffset * buffer->GetVertexSize();
  680. }
  681. if (buffer != vertexBuffers_[i] || offset != streamOffsets_[i])
  682. {
  683. if (buffer)
  684. impl_->device_->SetStreamSource(i, (IDirect3DVertexBuffer9*)buffer->GetGPUObject(), offset, buffer->GetVertexSize());
  685. else
  686. impl_->device_->SetStreamSource(i, 0, 0, 0);
  687. vertexBuffers_[i] = buffer;
  688. streamOffsets_[i] = offset;
  689. }
  690. }
  691. return true;
  692. }
  693. bool Graphics::SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>&
  694. elementMasks, unsigned instanceOffset)
  695. {
  696. if (buffers.Size() > MAX_VERTEX_STREAMS)
  697. {
  698. LOGERROR("Too many vertex buffers");
  699. return false;
  700. }
  701. if (buffers.Size() != elementMasks.Size())
  702. {
  703. LOGERROR("Amount of element masks and vertex buffers does not match");
  704. return false;
  705. }
  706. unsigned long long hash = 0;
  707. for (unsigned i = 0; i < buffers.Size(); ++i)
  708. {
  709. if (!buffers[i])
  710. continue;
  711. hash |= buffers[i]->GetBufferHash(i, elementMasks[i]);
  712. }
  713. if (hash)
  714. {
  715. if (!vertexDeclarations_.Contains(hash))
  716. {
  717. SharedPtr<VertexDeclaration> newDeclaration(new VertexDeclaration(this, buffers, elementMasks));
  718. if (!newDeclaration->GetDeclaration())
  719. {
  720. LOGERROR("Failed to create vertex declaration");
  721. return false;
  722. }
  723. vertexDeclarations_[hash] = newDeclaration;
  724. }
  725. VertexDeclaration* declaration = vertexDeclarations_[hash];
  726. if (declaration != vertexDeclaration_)
  727. {
  728. impl_->device_->SetVertexDeclaration(declaration->GetDeclaration());
  729. vertexDeclaration_ = declaration;
  730. }
  731. }
  732. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  733. {
  734. VertexBuffer* buffer = 0;
  735. unsigned offset = 0;
  736. if (i < buffers.Size())
  737. {
  738. buffer = buffers[i];
  739. if (buffer && buffer->GetElementMask() & MASK_INSTANCEMATRIX1)
  740. offset = instanceOffset * buffer->GetVertexSize();
  741. }
  742. if (buffer != vertexBuffers_[i] || offset != streamOffsets_[i])
  743. {
  744. if (buffer)
  745. impl_->device_->SetStreamSource(i, (IDirect3DVertexBuffer9*)buffer->GetGPUObject(), offset, buffer->GetVertexSize());
  746. else
  747. impl_->device_->SetStreamSource(i, 0, 0, 0);
  748. vertexBuffers_[i] = buffer;
  749. streamOffsets_[i] = offset;
  750. }
  751. }
  752. return true;
  753. }
  754. void Graphics::SetIndexBuffer(IndexBuffer* buffer)
  755. {
  756. if (buffer != indexBuffer_)
  757. {
  758. if (buffer)
  759. impl_->device_->SetIndices((IDirect3DIndexBuffer9*)buffer->GetGPUObject());
  760. else
  761. impl_->device_->SetIndices(0);
  762. indexBuffer_ = buffer;
  763. }
  764. }
  765. void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
  766. {
  767. if (vs == vertexShader_ && ps == pixelShader_)
  768. return;
  769. ClearParameterSources();
  770. if (vs != vertexShader_)
  771. {
  772. // Clear all previous vertex shader register mappings
  773. for (HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Begin(); i != shaderParameters_.End(); ++i)
  774. {
  775. if (i->second_.type_ == VS)
  776. i->second_.register_ = M_MAX_UNSIGNED;
  777. }
  778. // Create the shader now if not yet created. If already attempted, do not retry
  779. if (vs && !vs->IsCreated())
  780. {
  781. if (!vs->IsFailed())
  782. {
  783. PROFILE(CreateVertexShader);
  784. bool success = vs->Create();
  785. if (success)
  786. LOGDEBUG("Created vertex shader " + vs->GetName());
  787. else
  788. {
  789. LOGERROR("Failed to create vertex shader " + vs->GetName());
  790. vs = 0;
  791. }
  792. }
  793. else
  794. vs = 0;
  795. }
  796. if (vs && vs->GetShaderType() == VS)
  797. {
  798. impl_->device_->SetVertexShader((IDirect3DVertexShader9*)vs->GetGPUObject());
  799. // Update the parameter-to-register mappings
  800. const HashMap<StringHash, ShaderParameter>& parameters = vs->GetParameters();
  801. for (HashMap<StringHash, ShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  802. shaderParameters_[i->first_].register_ = i->second_.register_;
  803. }
  804. else
  805. {
  806. impl_->device_->SetVertexShader(0);
  807. vs = 0;
  808. }
  809. vertexShader_ = vs;
  810. }
  811. if (ps != pixelShader_)
  812. {
  813. for (HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Begin(); i != shaderParameters_.End(); ++i)
  814. {
  815. if (i->second_.type_ == PS)
  816. i->second_.register_ = M_MAX_UNSIGNED;
  817. }
  818. if (ps && !ps->IsCreated())
  819. {
  820. if (!ps->IsFailed())
  821. {
  822. PROFILE(CreatePixelShader);
  823. bool success = ps->Create();
  824. if (success)
  825. LOGDEBUG("Created pixel shader " + ps->GetName());
  826. else
  827. {
  828. LOGERROR("Failed to create pixel shader " + ps->GetName());
  829. ps = 0;
  830. }
  831. }
  832. else
  833. ps = 0;
  834. }
  835. if (ps && ps->GetShaderType() == PS)
  836. {
  837. impl_->device_->SetPixelShader((IDirect3DPixelShader9*)ps->GetGPUObject());
  838. const HashMap<StringHash, ShaderParameter>& parameters = ps->GetParameters();
  839. for (HashMap<StringHash, ShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  840. shaderParameters_[i->first_].register_ = i->second_.register_;
  841. }
  842. else
  843. {
  844. impl_->device_->SetPixelShader(0);
  845. ps = 0;
  846. }
  847. pixelShader_ = ps;
  848. }
  849. }
  850. void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
  851. {
  852. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  853. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  854. return;
  855. if (i->second_.type_ == VS)
  856. impl_->device_->SetVertexShaderConstantF(i->second_.register_, data, count / 4);
  857. else
  858. impl_->device_->SetPixelShaderConstantF(i->second_.register_, data, count / 4);
  859. }
  860. void Graphics::SetShaderParameter(StringHash param, float value)
  861. {
  862. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  863. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  864. return;
  865. float data[4];
  866. data[0] = value;
  867. data[1] = 0.0f;
  868. data[2] = 0.0f;
  869. data[3] = 0.0f;
  870. if (i->second_.type_ == VS)
  871. impl_->device_->SetVertexShaderConstantF(i->second_.register_, &data[0], 1);
  872. else
  873. impl_->device_->SetPixelShaderConstantF(i->second_.register_, &data[0], 1);
  874. }
  875. void Graphics::SetShaderParameter(StringHash param, const Color& color)
  876. {
  877. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  878. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  879. return;
  880. if (i->second_.type_ == VS)
  881. impl_->device_->SetVertexShaderConstantF(i->second_.register_, color.Data(), 1);
  882. else
  883. impl_->device_->SetPixelShaderConstantF(i->second_.register_, color.Data(), 1);
  884. }
  885. void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
  886. {
  887. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  888. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  889. return;
  890. float data[12];
  891. data[0] = matrix.m00_;
  892. data[1] = matrix.m01_;
  893. data[2] = matrix.m02_;
  894. data[3] = 0.0f;
  895. data[4] = matrix.m10_;
  896. data[5] = matrix.m11_;
  897. data[6] = matrix.m12_;
  898. data[7] = 0.0f;
  899. data[8] = matrix.m20_;
  900. data[9] = matrix.m21_;
  901. data[10] = matrix.m22_;
  902. data[11] = 0.0f;
  903. if (i->second_.type_ == VS)
  904. impl_->device_->SetVertexShaderConstantF(i->second_.register_, &data[0], 3);
  905. else
  906. impl_->device_->SetPixelShaderConstantF(i->second_.register_, &data[0], 3);
  907. }
  908. void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
  909. {
  910. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  911. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  912. return;
  913. float data[4];
  914. data[0] = vector.x_;
  915. data[1] = vector.y_;
  916. data[2] = vector.z_;
  917. data[3] = 0.0f;
  918. if (i->second_.type_ == VS)
  919. impl_->device_->SetVertexShaderConstantF(i->second_.register_, &data[0], 1);
  920. else
  921. impl_->device_->SetPixelShaderConstantF(i->second_.register_, &data[0], 1);
  922. }
  923. void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
  924. {
  925. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  926. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  927. return;
  928. if (i->second_.type_ == VS)
  929. impl_->device_->SetVertexShaderConstantF(i->second_.register_, matrix.Data(), 4);
  930. else
  931. impl_->device_->SetPixelShaderConstantF(i->second_.register_, matrix.Data(), 4);
  932. }
  933. void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
  934. {
  935. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  936. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  937. return;
  938. if (i->second_.type_ == VS)
  939. impl_->device_->SetVertexShaderConstantF(i->second_.register_, vector.Data(), 1);
  940. else
  941. impl_->device_->SetPixelShaderConstantF(i->second_.register_, vector.Data(), 1);
  942. }
  943. void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
  944. {
  945. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  946. if (i == shaderParameters_.End() || i->second_.register_ >= MAX_CONSTANT_REGISTERS)
  947. return;
  948. if (i->second_.type_ == VS)
  949. impl_->device_->SetVertexShaderConstantF(i->second_.register_, matrix.Data(), 3);
  950. else
  951. impl_->device_->SetPixelShaderConstantF(i->second_.register_, matrix.Data(), 3);
  952. }
  953. void Graphics::RegisterShaderParameter(StringHash param, const ShaderParameter& definition)
  954. {
  955. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  956. if (i == shaderParameters_.End())
  957. {
  958. // Define new parameter
  959. i = shaderParameters_.Insert(MakePair(param, definition));
  960. i->second_.register_ = M_MAX_UNSIGNED;
  961. // Rehash the parameters to ensure minimum load factor and fast queries
  962. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  963. }
  964. else
  965. {
  966. // Existing parameter: check that there is no conflict
  967. if (i->second_.type_ != definition.type_)
  968. LOGWARNING("Shader type mismatch on shader parameter " + String(param));
  969. // The same parameter is possibly defined with different sizes in different shaders. Use the highest size
  970. if (i->second_.regCount_ < definition.regCount_)
  971. i->second_.regCount_ = definition.regCount_;
  972. }
  973. }
  974. bool Graphics::NeedParameterUpdate(ShaderParameterGroup group, const void* source)
  975. {
  976. if ((unsigned)(size_t)shaderParameterSources_[group] == M_MAX_UNSIGNED || shaderParameterSources_[group] != source)
  977. {
  978. shaderParameterSources_[group] = source;
  979. return true;
  980. }
  981. else
  982. return false;
  983. }
  984. bool Graphics::HasShaderParameter(ShaderType type, StringHash param)
  985. {
  986. if (type == VS)
  987. return vertexShader_ && vertexShader_->HasParameter(param);
  988. else
  989. return pixelShader_ && pixelShader_->HasParameter(param);
  990. }
  991. bool Graphics::HasTextureUnit(TextureUnit unit)
  992. {
  993. return pixelShader_ && pixelShader_->HasTextureUnit(unit);
  994. }
  995. void Graphics::ClearParameterSource(ShaderParameterGroup group)
  996. {
  997. shaderParameterSources_[group] = (const void*)M_MAX_UNSIGNED;
  998. }
  999. void Graphics::ClearParameterSources()
  1000. {
  1001. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  1002. shaderParameterSources_[i] = (const void*)M_MAX_UNSIGNED;
  1003. }
  1004. void Graphics::ClearTransformSources()
  1005. {
  1006. shaderParameterSources_[SP_CAMERA] = (const void*)M_MAX_UNSIGNED;
  1007. shaderParameterSources_[SP_OBJECTTRANSFORM] = (const void*)M_MAX_UNSIGNED;
  1008. }
  1009. void Graphics::SetTexture(unsigned index, Texture* texture)
  1010. {
  1011. if (index >= MAX_TEXTURE_UNITS)
  1012. return;
  1013. // Check if texture is currently bound as a rendertarget. In that case, use its backup texture, or blank if not defined
  1014. if (texture)
  1015. {
  1016. if (texture == viewTexture_ || (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture))
  1017. texture = texture->GetBackupTexture();
  1018. }
  1019. if (texture != textures_[index])
  1020. {
  1021. if (texture)
  1022. impl_->device_->SetTexture(index, (IDirect3DBaseTexture9*)texture->GetGPUObject());
  1023. else
  1024. impl_->device_->SetTexture(index, 0);
  1025. textures_[index] = texture;
  1026. }
  1027. if (texture)
  1028. {
  1029. TextureFilterMode filterMode = texture->GetFilterMode();
  1030. if (filterMode == FILTER_DEFAULT)
  1031. filterMode = defaultTextureFilterMode_;
  1032. D3DTEXTUREFILTERTYPE minMag, mip;
  1033. minMag = d3dMinMagFilter[filterMode];
  1034. if (minMag != impl_->minMagFilters_[index])
  1035. {
  1036. impl_->device_->SetSamplerState(index, D3DSAMP_MAGFILTER, minMag);
  1037. impl_->device_->SetSamplerState(index, D3DSAMP_MINFILTER, minMag);
  1038. impl_->minMagFilters_[index] = minMag;
  1039. }
  1040. mip = d3dMipFilter[filterMode];
  1041. if (mip != impl_->mipFilters_[index])
  1042. {
  1043. impl_->device_->SetSamplerState(index, D3DSAMP_MIPFILTER, mip);
  1044. impl_->mipFilters_[index] = mip;
  1045. }
  1046. D3DTEXTUREADDRESS u, v;
  1047. u = d3dAddressMode[texture->GetAddressMode(COORD_U)];
  1048. if (u != impl_->uAddressModes_[index])
  1049. {
  1050. impl_->device_->SetSamplerState(index, D3DSAMP_ADDRESSU, u);
  1051. impl_->uAddressModes_[index] = u;
  1052. }
  1053. v = d3dAddressMode[texture->GetAddressMode(COORD_V)];
  1054. if (v != impl_->vAddressModes_[index])
  1055. {
  1056. impl_->device_->SetSamplerState(index, D3DSAMP_ADDRESSV, v);
  1057. impl_->vAddressModes_[index] = v;
  1058. }
  1059. if (texture->GetType() == TextureCube::GetTypeStatic())
  1060. {
  1061. D3DTEXTUREADDRESS w = d3dAddressMode[texture->GetAddressMode(COORD_W)];
  1062. if (w != impl_->wAddressModes_[index])
  1063. {
  1064. impl_->device_->SetSamplerState(index, D3DSAMP_ADDRESSW, w);
  1065. impl_->wAddressModes_[index] = w;
  1066. }
  1067. }
  1068. if (u == D3DTADDRESS_BORDER || v == D3DTADDRESS_BORDER)
  1069. {
  1070. const Color& borderColor = texture->GetBorderColor();
  1071. if (borderColor != impl_->borderColors_[index])
  1072. {
  1073. impl_->device_->SetSamplerState(index, D3DSAMP_BORDERCOLOR, GetD3DColor(borderColor));
  1074. impl_->borderColors_[index] = borderColor;
  1075. }
  1076. }
  1077. if (sRGBSupport_)
  1078. {
  1079. bool sRGB = texture->GetSRGB();
  1080. if (sRGB != impl_->sRGBModes_[index])
  1081. {
  1082. impl_->device_->SetSamplerState(index, D3DSAMP_SRGBTEXTURE, sRGB ? TRUE : FALSE);
  1083. impl_->sRGBModes_[index] = sRGB;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
  1089. {
  1090. defaultTextureFilterMode_ = mode;
  1091. }
  1092. void Graphics::ResetRenderTargets()
  1093. {
  1094. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1095. SetRenderTarget(i, (RenderSurface*)0);
  1096. SetDepthStencil((RenderSurface*)0);
  1097. SetViewport(IntRect(0, 0, width_, height_));
  1098. }
  1099. void Graphics::ResetRenderTarget(unsigned index)
  1100. {
  1101. SetRenderTarget(index, (RenderSurface*)0);
  1102. }
  1103. void Graphics::ResetDepthStencil()
  1104. {
  1105. SetDepthStencil((RenderSurface*)0);
  1106. }
  1107. void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
  1108. {
  1109. if (index >= MAX_RENDERTARGETS)
  1110. return;
  1111. IDirect3DSurface9* newColorSurface = 0;
  1112. if (renderTarget)
  1113. {
  1114. if (renderTarget->GetUsage() != TEXTURE_RENDERTARGET)
  1115. return;
  1116. newColorSurface = (IDirect3DSurface9*)renderTarget->GetSurface();
  1117. }
  1118. else
  1119. {
  1120. if (!index)
  1121. newColorSurface = impl_->defaultColorSurface_;
  1122. }
  1123. renderTargets_[index] = renderTarget;
  1124. if (newColorSurface != impl_->colorSurfaces_[index])
  1125. {
  1126. impl_->device_->SetRenderTarget(index, newColorSurface);
  1127. impl_->colorSurfaces_[index] = newColorSurface;
  1128. // Setting the first rendertarget causes viewport to be reset
  1129. if (!index)
  1130. {
  1131. IntVector2 rtSize = GetRenderTargetDimensions();
  1132. viewport_ = IntRect(0, 0, rtSize.x_, rtSize.y_);
  1133. }
  1134. }
  1135. // If the rendertarget is also bound as a texture, replace with backup texture or null
  1136. if (renderTarget)
  1137. {
  1138. Texture* parentTexture = renderTarget->GetParentTexture();
  1139. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1140. {
  1141. if (textures_[i] == parentTexture)
  1142. SetTexture(i, textures_[i]->GetBackupTexture());
  1143. }
  1144. }
  1145. // First rendertarget controls sRGB write mode
  1146. if (!index && sRGBWriteSupport_)
  1147. {
  1148. bool sRGBWrite = renderTarget ? renderTarget->GetParentTexture()->GetSRGB() : sRGB_;
  1149. if (sRGBWrite != impl_->sRGBWrite_)
  1150. {
  1151. impl_->device_->SetRenderState(D3DRS_SRGBWRITEENABLE, sRGBWrite ? TRUE : FALSE);
  1152. impl_->sRGBWrite_ = sRGBWrite;
  1153. }
  1154. }
  1155. }
  1156. void Graphics::SetRenderTarget(unsigned index, Texture2D* texture)
  1157. {
  1158. RenderSurface* renderTarget = 0;
  1159. if (texture)
  1160. renderTarget = texture->GetRenderSurface();
  1161. SetRenderTarget(index, renderTarget);
  1162. }
  1163. void Graphics::SetDepthStencil(RenderSurface* depthStencil)
  1164. {
  1165. IDirect3DSurface9* newDepthStencilSurface = 0;
  1166. if (depthStencil && depthStencil->GetUsage() == TEXTURE_DEPTHSTENCIL)
  1167. {
  1168. newDepthStencilSurface = (IDirect3DSurface9*)depthStencil->GetSurface();
  1169. depthStencil_ = depthStencil;
  1170. }
  1171. if (!newDepthStencilSurface)
  1172. {
  1173. newDepthStencilSurface = impl_->defaultDepthStencilSurface_;
  1174. depthStencil_ = 0;
  1175. }
  1176. if (newDepthStencilSurface != impl_->depthStencilSurface_)
  1177. {
  1178. impl_->device_->SetDepthStencilSurface(newDepthStencilSurface);
  1179. impl_->depthStencilSurface_ = newDepthStencilSurface;
  1180. }
  1181. }
  1182. void Graphics::SetDepthStencil(Texture2D* texture)
  1183. {
  1184. RenderSurface* depthStencil = 0;
  1185. if (texture)
  1186. depthStencil = texture->GetRenderSurface();
  1187. SetDepthStencil(depthStencil);
  1188. }
  1189. void Graphics::SetViewTexture(Texture* texture)
  1190. {
  1191. viewTexture_ = texture;
  1192. if (viewTexture_)
  1193. {
  1194. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1195. {
  1196. if (textures_[i] == viewTexture_)
  1197. SetTexture(i, textures_[i]->GetBackupTexture());
  1198. }
  1199. }
  1200. }
  1201. void Graphics::SetViewport(const IntRect& rect)
  1202. {
  1203. IntVector2 size = GetRenderTargetDimensions();
  1204. IntRect rectCopy = rect;
  1205. if (rectCopy.right_ <= rectCopy.left_)
  1206. rectCopy.right_ = rectCopy.left_ + 1;
  1207. if (rectCopy.bottom_ <= rectCopy.top_)
  1208. rectCopy.bottom_ = rectCopy.top_ + 1;
  1209. rectCopy.left_ = Clamp(rectCopy.left_, 0, size.x_);
  1210. rectCopy.top_ = Clamp(rectCopy.top_, 0, size.y_);
  1211. rectCopy.right_ = Clamp(rectCopy.right_, 0, size.x_);
  1212. rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, size.y_);
  1213. D3DVIEWPORT9 vp;
  1214. vp.MinZ = 0.0f;
  1215. vp.MaxZ = 1.0f;
  1216. vp.X = rectCopy.left_;
  1217. vp.Y = rectCopy.top_;
  1218. vp.Width = rectCopy.Width();
  1219. vp.Height = rectCopy.Height();
  1220. impl_->device_->SetViewport(&vp);
  1221. viewport_ = rectCopy;
  1222. // Disable scissor test, needs to be re-enabled by the user
  1223. SetScissorTest(false);
  1224. }
  1225. void Graphics::SetTextureAnisotropy(unsigned level)
  1226. {
  1227. if (level < 1)
  1228. level = 1;
  1229. if (level != textureAnisotropy_)
  1230. {
  1231. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1232. impl_->device_->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, level);
  1233. textureAnisotropy_ = level;
  1234. }
  1235. }
  1236. void Graphics::SetBlendMode(BlendMode mode)
  1237. {
  1238. if (mode != blendMode_)
  1239. {
  1240. if (d3dBlendEnable[mode] != impl_->blendEnable_)
  1241. {
  1242. impl_->device_->SetRenderState(D3DRS_ALPHABLENDENABLE, d3dBlendEnable[mode]);
  1243. impl_->blendEnable_ = d3dBlendEnable[mode];
  1244. }
  1245. if (impl_->blendEnable_)
  1246. {
  1247. if (d3dSrcBlend[mode] != impl_->srcBlend_)
  1248. {
  1249. impl_->device_->SetRenderState(D3DRS_SRCBLEND, d3dSrcBlend[mode]);
  1250. impl_->srcBlend_ = d3dSrcBlend[mode];
  1251. }
  1252. if (d3dDestBlend[mode] != impl_->destBlend_)
  1253. {
  1254. impl_->device_->SetRenderState(D3DRS_DESTBLEND, d3dDestBlend[mode]);
  1255. impl_->destBlend_ = d3dDestBlend[mode];
  1256. }
  1257. }
  1258. blendMode_ = mode;
  1259. }
  1260. }
  1261. void Graphics::SetColorWrite(bool enable)
  1262. {
  1263. if (enable != colorWrite_)
  1264. {
  1265. impl_->device_->SetRenderState(D3DRS_COLORWRITEENABLE, enable ? D3DCOLORWRITEENABLE_RED |
  1266. D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA : 0);
  1267. colorWrite_ = enable;
  1268. }
  1269. }
  1270. void Graphics::SetCullMode(CullMode mode)
  1271. {
  1272. if (mode != cullMode_)
  1273. {
  1274. impl_->device_->SetRenderState(D3DRS_CULLMODE, d3dCullMode[mode]);
  1275. cullMode_ = mode;
  1276. }
  1277. }
  1278. void Graphics::SetDepthBias(float constantBias, float slopeScaledBias)
  1279. {
  1280. if (constantBias != constantDepthBias_)
  1281. {
  1282. impl_->device_->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&constantBias));
  1283. constantDepthBias_ = constantBias;
  1284. }
  1285. if (slopeScaledBias != slopeScaledDepthBias_)
  1286. {
  1287. impl_->device_->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&slopeScaledBias));
  1288. slopeScaledDepthBias_ = slopeScaledBias;
  1289. }
  1290. }
  1291. void Graphics::SetDepthTest(CompareMode mode)
  1292. {
  1293. if (mode != depthTestMode_)
  1294. {
  1295. impl_->device_->SetRenderState(D3DRS_ZFUNC, d3dCmpFunc[mode]);
  1296. depthTestMode_ = mode;
  1297. }
  1298. }
  1299. void Graphics::SetDepthWrite(bool enable)
  1300. {
  1301. if (enable != depthWrite_)
  1302. {
  1303. impl_->device_->SetRenderState(D3DRS_ZWRITEENABLE, enable ? TRUE : FALSE);
  1304. depthWrite_ = enable;
  1305. }
  1306. }
  1307. void Graphics::SetFillMode(FillMode mode)
  1308. {
  1309. if (mode != fillMode_)
  1310. {
  1311. impl_->device_->SetRenderState(D3DRS_FILLMODE, d3dFillMode[mode]);
  1312. fillMode_ = mode;
  1313. }
  1314. }
  1315. void Graphics::SetScissorTest(bool enable, const Rect& rect, bool borderInclusive)
  1316. {
  1317. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1318. // Disable scissor in that case to reduce state changes
  1319. if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.x_ >= 1.0f && rect.max_.y_ >= 1.0f)
  1320. enable = false;
  1321. if (enable)
  1322. {
  1323. IntVector2 rtSize(GetRenderTargetDimensions());
  1324. IntVector2 viewSize(viewport_.Size());
  1325. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1326. IntRect intRect;
  1327. int expand = borderInclusive ? 1 : 0;
  1328. intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
  1329. intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
  1330. intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
  1331. intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
  1332. if (intRect.right_ == intRect.left_)
  1333. intRect.right_++;
  1334. if (intRect.bottom_ == intRect.top_)
  1335. intRect.bottom_++;
  1336. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1337. enable = false;
  1338. if (enable && scissorRect_ != intRect)
  1339. {
  1340. RECT d3dRect;
  1341. d3dRect.left = intRect.left_;
  1342. d3dRect.top = intRect.top_;
  1343. d3dRect.right = intRect.right_;
  1344. d3dRect.bottom = intRect.bottom_;
  1345. impl_->device_->SetScissorRect(&d3dRect);
  1346. scissorRect_ = intRect;
  1347. }
  1348. }
  1349. else
  1350. scissorRect_ = IntRect::ZERO;
  1351. if (enable != scissorTest_)
  1352. {
  1353. impl_->device_->SetRenderState(D3DRS_SCISSORTESTENABLE, enable ? TRUE : FALSE);
  1354. scissorTest_ = enable;
  1355. }
  1356. }
  1357. void Graphics::SetScissorTest(bool enable, const IntRect& rect)
  1358. {
  1359. IntVector2 rtSize(GetRenderTargetDimensions());
  1360. IntVector2 viewSize(viewport_.Size());
  1361. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1362. if (enable)
  1363. {
  1364. IntRect intRect;
  1365. intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
  1366. intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
  1367. intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
  1368. intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
  1369. if (intRect.right_ == intRect.left_)
  1370. intRect.right_++;
  1371. if (intRect.bottom_ == intRect.top_)
  1372. intRect.bottom_++;
  1373. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1374. enable = false;
  1375. if (enable && scissorRect_ != intRect)
  1376. {
  1377. RECT d3dRect;
  1378. d3dRect.left = intRect.left_;
  1379. d3dRect.top = intRect.top_;
  1380. d3dRect.right = intRect.right_;
  1381. d3dRect.bottom = intRect.bottom_;
  1382. impl_->device_->SetScissorRect(&d3dRect);
  1383. scissorRect_ = intRect;
  1384. }
  1385. }
  1386. else
  1387. scissorRect_ = IntRect::ZERO;
  1388. if (enable != scissorTest_)
  1389. {
  1390. impl_->device_->SetRenderState(D3DRS_SCISSORTESTENABLE, enable ? TRUE : FALSE);
  1391. scissorTest_ = enable;
  1392. }
  1393. }
  1394. void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned compareMask, unsigned writeMask)
  1395. {
  1396. if (enable != stencilTest_)
  1397. {
  1398. impl_->device_->SetRenderState(D3DRS_STENCILENABLE, enable ? TRUE : FALSE);
  1399. stencilTest_ = enable;
  1400. }
  1401. if (enable)
  1402. {
  1403. if (mode != stencilTestMode_)
  1404. {
  1405. impl_->device_->SetRenderState(D3DRS_STENCILFUNC, d3dCmpFunc[mode]);
  1406. stencilTestMode_ = mode;
  1407. }
  1408. if (pass != stencilPass_)
  1409. {
  1410. impl_->device_->SetRenderState(D3DRS_STENCILPASS, d3dStencilOp[pass]);
  1411. stencilPass_ = pass;
  1412. }
  1413. if (fail != stencilFail_)
  1414. {
  1415. impl_->device_->SetRenderState(D3DRS_STENCILFAIL, d3dStencilOp[fail]);
  1416. stencilFail_ = fail;
  1417. }
  1418. if (zFail != stencilZFail_)
  1419. {
  1420. impl_->device_->SetRenderState(D3DRS_STENCILZFAIL, d3dStencilOp[zFail]);
  1421. stencilZFail_ = zFail;
  1422. }
  1423. if (stencilRef != stencilRef_)
  1424. {
  1425. impl_->device_->SetRenderState(D3DRS_STENCILREF, stencilRef);
  1426. stencilRef_ = stencilRef;
  1427. }
  1428. if (compareMask != stencilCompareMask_)
  1429. {
  1430. impl_->device_->SetRenderState(D3DRS_STENCILMASK, compareMask);
  1431. stencilCompareMask_ = compareMask;
  1432. }
  1433. if (writeMask != stencilWriteMask_)
  1434. {
  1435. impl_->device_->SetRenderState(D3DRS_STENCILWRITEMASK, writeMask);
  1436. stencilWriteMask_ = writeMask;
  1437. }
  1438. }
  1439. }
  1440. void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
  1441. {
  1442. if (index < MAX_VERTEX_STREAMS && streamFrequencies_[index] != frequency)
  1443. {
  1444. impl_->device_->SetStreamSourceFreq(index, frequency);
  1445. streamFrequencies_[index] = frequency;
  1446. }
  1447. }
  1448. void Graphics::ResetStreamFrequencies()
  1449. {
  1450. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1451. {
  1452. if (streamFrequencies_[i] != 1)
  1453. {
  1454. impl_->device_->SetStreamSourceFreq(i, 1);
  1455. streamFrequencies_[i] = 1;
  1456. }
  1457. }
  1458. }
  1459. void Graphics::SetForceSM2(bool enable)
  1460. {
  1461. if (!IsInitialized())
  1462. forceSM2_ = enable;
  1463. else
  1464. LOGERROR("Force Shader Model 2 can not be changed after setting the initial screen mode");
  1465. }
  1466. bool Graphics::IsInitialized() const
  1467. {
  1468. return impl_->window_ != 0 && impl_->GetDevice() != 0;
  1469. }
  1470. PODVector<IntVector2> Graphics::GetResolutions() const
  1471. {
  1472. PODVector<IntVector2> ret;
  1473. unsigned numModes = SDL_GetNumDisplayModes(0);
  1474. for (unsigned i = 0; i < numModes; ++i)
  1475. {
  1476. SDL_DisplayMode mode;
  1477. SDL_GetDisplayMode(0, i, &mode);
  1478. int width = mode.w;
  1479. int height = mode.h;
  1480. // Store mode if unique
  1481. bool unique = true;
  1482. for (unsigned j = 0; j < ret.Size(); ++j)
  1483. {
  1484. if (ret[j].x_ == width && ret[j].y_ == height)
  1485. {
  1486. unique = false;
  1487. break;
  1488. }
  1489. }
  1490. if (unique)
  1491. ret.Push(IntVector2(width, height));
  1492. }
  1493. return ret;
  1494. }
  1495. PODVector<int> Graphics::GetMultiSampleLevels() const
  1496. {
  1497. PODVector<int> ret;
  1498. // No multisampling always supported
  1499. ret.Push(1);
  1500. if (!impl_->interface_)
  1501. return ret;
  1502. SDL_DisplayMode mode;
  1503. SDL_GetDesktopDisplayMode(0, &mode);
  1504. D3DFORMAT fullscreenFormat = SDL_BITSPERPIXEL(mode.format) == 16 ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8;
  1505. for (unsigned i = (int)D3DMULTISAMPLE_2_SAMPLES; i < (int)D3DMULTISAMPLE_16_SAMPLES; ++i)
  1506. {
  1507. if (SUCCEEDED(impl_->interface_->CheckDeviceMultiSampleType(impl_->adapter_, impl_->deviceType_, fullscreenFormat, FALSE,
  1508. (D3DMULTISAMPLE_TYPE)i, NULL)))
  1509. ret.Push(i);
  1510. }
  1511. return ret;
  1512. }
  1513. unsigned Graphics::GetFormat(CompressedFormat format) const
  1514. {
  1515. switch (format)
  1516. {
  1517. case CF_DXT1:
  1518. return D3DFMT_DXT1;
  1519. case CF_DXT3:
  1520. return D3DFMT_DXT3;
  1521. case CF_DXT5:
  1522. return D3DFMT_DXT5;
  1523. }
  1524. return 0;
  1525. }
  1526. VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
  1527. {
  1528. return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
  1529. }
  1530. TextureUnit Graphics::GetTextureUnit(const String& name)
  1531. {
  1532. HashMap<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
  1533. if (i != textureUnits_.End())
  1534. return i->second_;
  1535. else
  1536. return MAX_TEXTURE_UNITS;
  1537. }
  1538. Texture* Graphics::GetTexture(unsigned index) const
  1539. {
  1540. return index < MAX_TEXTURE_UNITS ? textures_[index] : 0;
  1541. }
  1542. RenderSurface* Graphics::GetRenderTarget(unsigned index) const
  1543. {
  1544. return index < MAX_RENDERTARGETS ? renderTargets_[index] : 0;
  1545. }
  1546. unsigned Graphics::GetStreamFrequency(unsigned index) const
  1547. {
  1548. return index < MAX_VERTEX_STREAMS ? streamFrequencies_[index] : 0;
  1549. }
  1550. IntVector2 Graphics::GetRenderTargetDimensions() const
  1551. {
  1552. int width, height;
  1553. if (renderTargets_[0])
  1554. {
  1555. width = renderTargets_[0]->GetWidth();
  1556. height = renderTargets_[0]->GetHeight();
  1557. }
  1558. else
  1559. {
  1560. width = width_;
  1561. height = height_;
  1562. }
  1563. return IntVector2(width, height);
  1564. }
  1565. void Graphics::WindowResized(int width, int height)
  1566. {
  1567. if (!impl_->device_ || (width == width_ && height == height_))
  1568. return;
  1569. width_ = width;
  1570. height_ = height;
  1571. impl_->presentParams_.BackBufferWidth = width;
  1572. impl_->presentParams_.BackBufferHeight = height;
  1573. ResetDevice();
  1574. // Reset rendertargets and viewport for the new screen size
  1575. ResetRenderTargets();
  1576. LOGDEBUG(ToString("Window was resized to %dx%d", width_, height_));
  1577. using namespace ScreenMode;
  1578. VariantMap eventData;
  1579. eventData[P_WIDTH] = width_;
  1580. eventData[P_HEIGHT] = height_;
  1581. eventData[P_FULLSCREEN] = fullscreen_;
  1582. eventData[P_RESIZABLE] = resizable_;
  1583. SendEvent(E_SCREENMODE, eventData);
  1584. }
  1585. void Graphics::AddGPUObject(GPUObject* object)
  1586. {
  1587. gpuObjects_.Push(object);
  1588. }
  1589. void Graphics::RemoveGPUObject(GPUObject* object)
  1590. {
  1591. gpuObjects_.Remove(object);
  1592. }
  1593. void* Graphics::ReserveScratchBuffer(unsigned size)
  1594. {
  1595. if (!size)
  1596. return 0;
  1597. if (size > maxScratchBufferRequest_)
  1598. maxScratchBufferRequest_ = size;
  1599. // First check for a free buffer that is large enough
  1600. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1601. {
  1602. if (!i->reserved_ && i->size_ >= size)
  1603. {
  1604. i->reserved_ = true;
  1605. return i->data_.Get();
  1606. }
  1607. }
  1608. // Then check if a free buffer can be resized
  1609. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1610. {
  1611. if (!i->reserved_)
  1612. {
  1613. i->data_ = new unsigned char[size];
  1614. i->size_ = size;
  1615. i->reserved_ = true;
  1616. LOGDEBUG("Resized scratch buffer to size " + String(size));
  1617. return i->data_.Get();
  1618. }
  1619. }
  1620. // Finally allocate a new buffer
  1621. ScratchBuffer newBuffer;
  1622. newBuffer.data_ = new unsigned char[size];
  1623. newBuffer.size_ = size;
  1624. newBuffer.reserved_ = true;
  1625. scratchBuffers_.Push(newBuffer);
  1626. return newBuffer.data_.Get();
  1627. LOGDEBUG("Allocated scratch buffer with size " + String(size));
  1628. }
  1629. void Graphics::FreeScratchBuffer(void* buffer)
  1630. {
  1631. if (!buffer)
  1632. return;
  1633. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1634. {
  1635. if (i->reserved_ && i->data_.Get() == buffer)
  1636. {
  1637. i->reserved_ = false;
  1638. return;
  1639. }
  1640. }
  1641. LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  1642. }
  1643. void Graphics::CleanupScratchBuffers()
  1644. {
  1645. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1646. {
  1647. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2)
  1648. {
  1649. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  1650. i->size_ = maxScratchBufferRequest_;
  1651. LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  1652. }
  1653. }
  1654. maxScratchBufferRequest_ = 0;
  1655. }
  1656. unsigned Graphics::GetAlphaFormat()
  1657. {
  1658. return D3DFMT_A8;
  1659. }
  1660. unsigned Graphics::GetLuminanceFormat()
  1661. {
  1662. return D3DFMT_L8;
  1663. }
  1664. unsigned Graphics::GetLuminanceAlphaFormat()
  1665. {
  1666. return D3DFMT_A8L8;
  1667. }
  1668. unsigned Graphics::GetRGBFormat()
  1669. {
  1670. return D3DFMT_X8R8G8B8;
  1671. }
  1672. unsigned Graphics::GetRGBAFormat()
  1673. {
  1674. return D3DFMT_A8R8G8B8;
  1675. }
  1676. unsigned Graphics::GetRGBA16Format()
  1677. {
  1678. return D3DFMT_A16B16G16R16;
  1679. }
  1680. unsigned Graphics::GetRGBAFloat16Format()
  1681. {
  1682. return D3DFMT_A16B16G16R16F;
  1683. }
  1684. unsigned Graphics::GetRGBAFloat32Format()
  1685. {
  1686. return D3DFMT_A32B32G32R32F;
  1687. }
  1688. unsigned Graphics::GetRG16Format()
  1689. {
  1690. return D3DFMT_G16R16;
  1691. }
  1692. unsigned Graphics::GetRGFloat16Format()
  1693. {
  1694. return D3DFMT_G16R16F;
  1695. }
  1696. unsigned Graphics::GetRGFloat32Format()
  1697. {
  1698. return D3DFMT_G32R32F;
  1699. }
  1700. unsigned Graphics::GetFloat16Format()
  1701. {
  1702. return D3DFMT_R16F;
  1703. }
  1704. unsigned Graphics::GetFloat32Format()
  1705. {
  1706. return D3DFMT_R32F;
  1707. }
  1708. unsigned Graphics::GetLinearDepthFormat()
  1709. {
  1710. return D3DFMT_R32F;
  1711. }
  1712. unsigned Graphics::GetDepthStencilFormat()
  1713. {
  1714. return depthStencilFormat;
  1715. }
  1716. unsigned Graphics::GetFormat(const String& formatName)
  1717. {
  1718. String nameLower = formatName.ToLower().Trimmed();
  1719. if (nameLower == "a")
  1720. return GetAlphaFormat();
  1721. if (nameLower == "l")
  1722. return GetLuminanceFormat();
  1723. if (nameLower == "la")
  1724. return GetLuminanceAlphaFormat();
  1725. if (nameLower == "rgb")
  1726. return GetRGBFormat();
  1727. if (nameLower == "rgba")
  1728. return GetRGBAFormat();
  1729. if (nameLower == "rgba16")
  1730. return GetRGBA16Format();
  1731. if (nameLower == "rgba16f")
  1732. return GetRGBAFloat16Format();
  1733. if (nameLower == "rgba32f")
  1734. return GetRGBAFloat32Format();
  1735. if (nameLower == "rg16")
  1736. return GetRG16Format();
  1737. if (nameLower == "rg16f")
  1738. return GetRGFloat16Format();
  1739. if (nameLower == "rg32f")
  1740. return GetRGFloat32Format();
  1741. if (nameLower == "r16f")
  1742. return GetFloat16Format();
  1743. if (nameLower == "r32f" || nameLower == "float")
  1744. return GetFloat32Format();
  1745. if (nameLower == "lineardepth" || nameLower == "depth")
  1746. return GetLinearDepthFormat();
  1747. if (nameLower == "d24s8")
  1748. return GetDepthStencilFormat();
  1749. return GetRGBFormat();
  1750. }
  1751. bool Graphics::OpenWindow(int width, int height, bool resizable)
  1752. {
  1753. if (!externalWindow_)
  1754. impl_->window_ = SDL_CreateWindow(windowTitle_.CString(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, resizable ? SDL_WINDOW_RESIZABLE : 0);
  1755. else
  1756. impl_->window_ = SDL_CreateWindowFrom(externalWindow_, 0);
  1757. if (!impl_->window_)
  1758. {
  1759. LOGERROR("Could not create window");
  1760. return false;
  1761. }
  1762. return true;
  1763. }
  1764. void Graphics::AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen)
  1765. {
  1766. if (!externalWindow_)
  1767. {
  1768. SDL_SetWindowSize(impl_->window_, newWidth, newHeight);
  1769. SDL_SetWindowFullscreen(impl_->window_, newFullscreen ? SDL_TRUE : SDL_FALSE);
  1770. }
  1771. else
  1772. {
  1773. // If external window, must ask its dimensions instead of trying to set them
  1774. SDL_GetWindowSize(impl_->window_, &newWidth, &newHeight);
  1775. newFullscreen = false;
  1776. }
  1777. }
  1778. bool Graphics::CreateInterface()
  1779. {
  1780. impl_->interface_ = Direct3DCreate9(D3D9b_SDK_VERSION);
  1781. if (!impl_->interface_)
  1782. {
  1783. LOGERROR("Could not create Direct3D9 interface");
  1784. return false;
  1785. }
  1786. if (FAILED(impl_->interface_->GetDeviceCaps(impl_->adapter_, impl_->deviceType_, &impl_->deviceCaps_)))
  1787. {
  1788. LOGERROR("Could not get Direct3D capabilities");
  1789. return false;
  1790. }
  1791. if (FAILED(impl_->interface_->GetAdapterIdentifier(impl_->adapter_, 0, &impl_->adapterIdentifier_)))
  1792. {
  1793. LOGERROR("Could not get Direct3D adapter identifier");
  1794. return false;
  1795. }
  1796. if (impl_->deviceCaps_.PixelShaderVersion < D3DPS_VERSION(2, 0))
  1797. {
  1798. LOGERROR("Shader model 2.0 display adapter is required");
  1799. return false;
  1800. }
  1801. return true;
  1802. }
  1803. bool Graphics::CreateDevice(unsigned adapter, unsigned deviceType)
  1804. {
  1805. DWORD behaviorFlags = 0;
  1806. if (impl_->deviceCaps_.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  1807. {
  1808. behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  1809. if (impl_->deviceCaps_.DevCaps & D3DDEVCAPS_PUREDEVICE)
  1810. behaviorFlags |= D3DCREATE_PUREDEVICE;
  1811. }
  1812. else
  1813. behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  1814. if (FAILED(impl_->interface_->CreateDevice(
  1815. adapter,
  1816. (D3DDEVTYPE)deviceType,
  1817. WIN_GetWindowHandle(impl_->window_),
  1818. behaviorFlags,
  1819. &impl_->presentParams_,
  1820. &impl_->device_)))
  1821. {
  1822. LOGERROR("Could not create Direct3D9 device");
  1823. return false;
  1824. }
  1825. impl_->adapter_ = adapter;
  1826. impl_->deviceType_ = (D3DDEVTYPE)deviceType;
  1827. OnDeviceReset();
  1828. LOGINFO("Created Direct3D9 device");
  1829. return true;
  1830. }
  1831. void Graphics::CheckFeatureSupport()
  1832. {
  1833. // Reset features first
  1834. lightPrepassSupport_ = false;
  1835. deferredSupport_ = false;
  1836. hardwareShadowSupport_ = false;
  1837. streamOffsetSupport_ = false;
  1838. hasSM3_ = false;
  1839. depthStencilFormat = D3DFMT_D24S8;
  1840. // Check hardware shadow map support: prefer NVIDIA style hardware depth compared shadow maps if available
  1841. shadowMapFormat_ = D3DFMT_D16;
  1842. if (impl_->CheckFormatSupport((D3DFORMAT)shadowMapFormat_, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1843. {
  1844. hardwareShadowSupport_ = true;
  1845. // Check for hires depth support
  1846. hiresShadowMapFormat_ = D3DFMT_D24X8;
  1847. if (!impl_->CheckFormatSupport((D3DFORMAT)hiresShadowMapFormat_, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1848. hiresShadowMapFormat_ = 0;
  1849. }
  1850. else
  1851. {
  1852. // ATI DF16 format needs manual depth compare in the shader
  1853. shadowMapFormat_ = MAKEFOURCC('D', 'F', '1', '6');
  1854. if (impl_->CheckFormatSupport((D3DFORMAT)shadowMapFormat_, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1855. {
  1856. // Check for hires depth support
  1857. hiresShadowMapFormat_ = MAKEFOURCC('D', 'F', '2', '4');
  1858. if (!impl_->CheckFormatSupport((D3DFORMAT)hiresShadowMapFormat_, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE))
  1859. hiresShadowMapFormat_ = 0;
  1860. }
  1861. else
  1862. {
  1863. // No shadow map support
  1864. shadowMapFormat_ = 0;
  1865. hiresShadowMapFormat_ = 0;
  1866. }
  1867. }
  1868. // Check for Intel 4 Series with an old driver, enable manual shadow map compare in that case
  1869. if (shadowMapFormat_ == D3DFMT_D16)
  1870. {
  1871. if (impl_->adapterIdentifier_.VendorId == 0x8086 && impl_->adapterIdentifier_.DeviceId == 0x2a42 &&
  1872. impl_->adapterIdentifier_.DriverVersion.QuadPart <= 0x0007000f000a05d0ULL)
  1873. hardwareShadowSupport_ = false;
  1874. }
  1875. // Check for dummy color rendertarget format used with hardware shadow maps
  1876. dummyColorFormat_ = D3DFMT_A8R8G8B8;
  1877. D3DFORMAT nullFormat = (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L');
  1878. if (impl_->CheckFormatSupport(nullFormat, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1879. dummyColorFormat_ = nullFormat;
  1880. else if (impl_->CheckFormatSupport(D3DFMT_R16F, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1881. dummyColorFormat_ = D3DFMT_R16F;
  1882. else if (impl_->CheckFormatSupport(D3DFMT_R5G6B5, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1883. dummyColorFormat_ = D3DFMT_R5G6B5;
  1884. else if (impl_->CheckFormatSupport(D3DFMT_A4R4G4B4, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE))
  1885. dummyColorFormat_ = D3DFMT_A4R4G4B4;
  1886. // Check for Shader Model 3
  1887. if (!forceSM2_)
  1888. {
  1889. if (impl_->deviceCaps_.VertexShaderVersion >= D3DVS_VERSION(3, 0) && impl_->deviceCaps_.PixelShaderVersion >=
  1890. D3DPS_VERSION(3, 0))
  1891. hasSM3_ = true;
  1892. }
  1893. // Check for light prepass and deferred rendering support
  1894. if (impl_->deviceCaps_.NumSimultaneousRTs >= 2 && impl_->CheckFormatSupport(D3DFMT_R32F, D3DUSAGE_RENDERTARGET,
  1895. D3DRTYPE_TEXTURE))
  1896. {
  1897. lightPrepassSupport_ = true;
  1898. if (impl_->deviceCaps_.NumSimultaneousRTs >= 4)
  1899. deferredSupport_ = true;
  1900. }
  1901. // Check for stream offset (needed for instancing)
  1902. if (impl_->deviceCaps_.DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
  1903. streamOffsetSupport_ = true;
  1904. // Check for sRGB read & write
  1905. /// \todo Should be checked for each texture format separately
  1906. sRGBSupport_ = impl_->CheckFormatSupport(D3DFMT_X8R8G8B8, D3DUSAGE_QUERY_SRGBREAD, D3DRTYPE_TEXTURE);
  1907. sRGBWriteSupport_ = impl_->CheckFormatSupport(D3DFMT_X8R8G8B8, D3DUSAGE_QUERY_SRGBWRITE, D3DRTYPE_TEXTURE);
  1908. SendEvent(E_GRAPHICSFEATURES);
  1909. }
  1910. void Graphics::ResetDevice()
  1911. {
  1912. OnDeviceLost();
  1913. if (SUCCEEDED(impl_->device_->Reset(&impl_->presentParams_)))
  1914. {
  1915. deviceLost_ = false;
  1916. OnDeviceReset();
  1917. }
  1918. }
  1919. void Graphics::OnDeviceLost()
  1920. {
  1921. LOGINFO("Device lost");
  1922. if (impl_->defaultColorSurface_)
  1923. {
  1924. impl_->defaultColorSurface_->Release();
  1925. impl_->defaultColorSurface_ = 0;
  1926. }
  1927. if (impl_->defaultDepthStencilSurface_)
  1928. {
  1929. impl_->defaultDepthStencilSurface_->Release();
  1930. impl_->defaultDepthStencilSurface_ = 0;
  1931. }
  1932. for (unsigned i = 0; i < gpuObjects_.Size(); ++i)
  1933. gpuObjects_[i]->OnDeviceLost();
  1934. }
  1935. void Graphics::OnDeviceReset()
  1936. {
  1937. for (unsigned i = 0; i < gpuObjects_.Size(); ++i)
  1938. gpuObjects_[i]->OnDeviceReset();
  1939. // Get default surfaces
  1940. impl_->device_->GetRenderTarget(0, &impl_->defaultColorSurface_);
  1941. impl_->device_->GetDepthStencilSurface(&impl_->defaultDepthStencilSurface_);
  1942. ResetCachedState();
  1943. }
  1944. void Graphics::ResetCachedState()
  1945. {
  1946. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1947. {
  1948. vertexBuffers_[i] = 0;
  1949. streamOffsets_[i] = 0;
  1950. }
  1951. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1952. {
  1953. textures_[i] = 0;
  1954. impl_->minMagFilters_[i] = D3DTEXF_POINT;
  1955. impl_->mipFilters_[i] = D3DTEXF_NONE;
  1956. impl_->uAddressModes_[i] = D3DTADDRESS_WRAP;
  1957. impl_->vAddressModes_[i] = D3DTADDRESS_WRAP;
  1958. impl_->wAddressModes_[i] = D3DTADDRESS_WRAP;
  1959. impl_->borderColors_[i] = Color(0.0f, 0.0f, 0.0f, 0.0f);
  1960. impl_->sRGBModes_[i] = false;
  1961. }
  1962. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1963. {
  1964. renderTargets_[i] = 0;
  1965. impl_->colorSurfaces_[i] = 0;
  1966. }
  1967. depthStencil_ = 0;
  1968. impl_->depthStencilSurface_ = 0;
  1969. viewTexture_ = 0;
  1970. viewport_ = IntRect(0, 0, width_, height_);
  1971. impl_->sRGBWrite_ = false;
  1972. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1973. streamFrequencies_[i] = 1;
  1974. indexBuffer_ = 0;
  1975. vertexDeclaration_ = 0;
  1976. vertexShader_ = 0;
  1977. pixelShader_ = 0;
  1978. blendMode_ = BLEND_REPLACE;
  1979. textureAnisotropy_ = 1;
  1980. colorWrite_ = true;
  1981. cullMode_ = CULL_CCW;
  1982. constantDepthBias_ = 0.0f;
  1983. slopeScaledDepthBias_ = 0.0f;
  1984. depthTestMode_ = CMP_LESSEQUAL;
  1985. depthWrite_ = true;
  1986. fillMode_ = FILL_SOLID;
  1987. scissorTest_ = false;
  1988. scissorRect_ = IntRect::ZERO;
  1989. stencilTest_ = false;
  1990. stencilTestMode_ = CMP_ALWAYS;
  1991. stencilPass_ = OP_KEEP;
  1992. stencilFail_ = OP_KEEP;
  1993. stencilZFail_ = OP_KEEP;
  1994. stencilRef_ = 0;
  1995. stencilCompareMask_ = M_MAX_UNSIGNED;
  1996. stencilWriteMask_ = M_MAX_UNSIGNED;
  1997. impl_->blendEnable_ = FALSE;
  1998. impl_->srcBlend_ = D3DBLEND_ONE;
  1999. impl_->destBlend_ = D3DBLEND_ZERO;
  2000. }
  2001. void Graphics::SetTextureUnitMappings()
  2002. {
  2003. textureUnits_["DiffMap"] = TU_DIFFUSE;
  2004. textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
  2005. textureUnits_["NormalMap"] = TU_NORMAL;
  2006. textureUnits_["SpecMap"] = TU_SPECULAR;
  2007. textureUnits_["EmissiveMap"] = TU_EMISSIVE;
  2008. textureUnits_["EnvMap"] = TU_ENVIRONMENT;
  2009. textureUnits_["EnvCubeMap"] = TU_ENVIRONMENT;
  2010. textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
  2011. textureUnits_["LightSpotMap"] = TU_LIGHTSHAPE;
  2012. textureUnits_["LightCubeMap"] = TU_LIGHTSHAPE;
  2013. textureUnits_["ShadowMap"] = TU_SHADOWMAP;
  2014. textureUnits_["FaceSelectCubeMap"] = TU_FACESELECT;
  2015. textureUnits_["IndirectionCubeMap"] = TU_INDIRECTION;
  2016. }
  2017. void RegisterGraphicsLibrary(Context* context)
  2018. {
  2019. Animation::RegisterObject(context);
  2020. Material::RegisterObject(context);
  2021. Model::RegisterObject(context);
  2022. Shader::RegisterObject(context);
  2023. Technique::RegisterObject(context);
  2024. Texture2D::RegisterObject(context);
  2025. TextureCube::RegisterObject(context);
  2026. Camera::RegisterObject(context);
  2027. Drawable::RegisterObject(context);
  2028. Light::RegisterObject(context);
  2029. StaticModel::RegisterObject(context);
  2030. Skybox::RegisterObject(context);
  2031. AnimatedModel::RegisterObject(context);
  2032. AnimationController::RegisterObject(context);
  2033. BillboardSet::RegisterObject(context);
  2034. ParticleEmitter::RegisterObject(context);
  2035. CustomGeometry::RegisterObject(context);
  2036. DecalSet::RegisterObject(context);
  2037. Terrain::RegisterObject(context);
  2038. TerrainPatch::RegisterObject(context);
  2039. DebugRenderer::RegisterObject(context);
  2040. Octree::RegisterObject(context);
  2041. Zone::RegisterObject(context);
  2042. }
  2043. }