D3D9Graphics.cpp 72 KB

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