D3D11Graphics.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../../Precompiled.h"
  4. #include "../../Core/Context.h"
  5. #include "../../Core/ProcessUtils.h"
  6. #include "../../Core/Profiler.h"
  7. #include "../../Graphics/Geometry.h"
  8. #include "../../Graphics/Graphics.h"
  9. #include "../../Graphics/GraphicsEvents.h"
  10. #include "../../Graphics/Renderer.h"
  11. #include "../../GraphicsAPI/ConstantBuffer.h"
  12. #include "../../GraphicsAPI/Direct3D11/D3D11GraphicsImpl.h"
  13. #include "../../GraphicsAPI/Direct3D11/D3D11ShaderProgram.h"
  14. #include "../../GraphicsAPI/IndexBuffer.h"
  15. #include "../../GraphicsAPI/Shader.h"
  16. #include "../../GraphicsAPI/ShaderPrecache.h"
  17. #include "../../GraphicsAPI/Texture2D.h"
  18. #include "../../GraphicsAPI/TextureCube.h"
  19. #include "../../GraphicsAPI/VertexBuffer.h"
  20. #include "../../IO/File.h"
  21. #include "../../IO/Log.h"
  22. #include "../../Resource/ResourceCache.h"
  23. #include <SDL/SDL.h>
  24. #include <SDL/SDL_syswm.h>
  25. #include "../../DebugNew.h"
  26. #ifdef _MSC_VER
  27. #pragma warning(disable:4355)
  28. #endif
  29. namespace Urho3D
  30. {
  31. static const D3D11_COMPARISON_FUNC d3dCmpFunc[] =
  32. {
  33. D3D11_COMPARISON_ALWAYS,
  34. D3D11_COMPARISON_EQUAL,
  35. D3D11_COMPARISON_NOT_EQUAL,
  36. D3D11_COMPARISON_LESS,
  37. D3D11_COMPARISON_LESS_EQUAL,
  38. D3D11_COMPARISON_GREATER,
  39. D3D11_COMPARISON_GREATER_EQUAL
  40. };
  41. static const DWORD d3dBlendEnable[] =
  42. {
  43. FALSE,
  44. TRUE,
  45. TRUE,
  46. TRUE,
  47. TRUE,
  48. TRUE,
  49. TRUE,
  50. TRUE,
  51. TRUE
  52. };
  53. static const D3D11_BLEND d3dSrcBlend[] =
  54. {
  55. D3D11_BLEND_ONE,
  56. D3D11_BLEND_ONE,
  57. D3D11_BLEND_DEST_COLOR,
  58. D3D11_BLEND_SRC_ALPHA,
  59. D3D11_BLEND_SRC_ALPHA,
  60. D3D11_BLEND_ONE,
  61. D3D11_BLEND_INV_DEST_ALPHA,
  62. D3D11_BLEND_ONE,
  63. D3D11_BLEND_SRC_ALPHA,
  64. };
  65. static const D3D11_BLEND d3dDestBlend[] =
  66. {
  67. D3D11_BLEND_ZERO,
  68. D3D11_BLEND_ONE,
  69. D3D11_BLEND_ZERO,
  70. D3D11_BLEND_INV_SRC_ALPHA,
  71. D3D11_BLEND_ONE,
  72. D3D11_BLEND_INV_SRC_ALPHA,
  73. D3D11_BLEND_DEST_ALPHA,
  74. D3D11_BLEND_ONE,
  75. D3D11_BLEND_ONE
  76. };
  77. static const D3D11_BLEND_OP d3dBlendOp[] =
  78. {
  79. D3D11_BLEND_OP_ADD,
  80. D3D11_BLEND_OP_ADD,
  81. D3D11_BLEND_OP_ADD,
  82. D3D11_BLEND_OP_ADD,
  83. D3D11_BLEND_OP_ADD,
  84. D3D11_BLEND_OP_ADD,
  85. D3D11_BLEND_OP_ADD,
  86. D3D11_BLEND_OP_REV_SUBTRACT,
  87. D3D11_BLEND_OP_REV_SUBTRACT
  88. };
  89. static const D3D11_STENCIL_OP d3dStencilOp[] =
  90. {
  91. D3D11_STENCIL_OP_KEEP,
  92. D3D11_STENCIL_OP_ZERO,
  93. D3D11_STENCIL_OP_REPLACE,
  94. D3D11_STENCIL_OP_INCR,
  95. D3D11_STENCIL_OP_DECR
  96. };
  97. static const D3D11_CULL_MODE d3dCullMode[] =
  98. {
  99. D3D11_CULL_NONE,
  100. D3D11_CULL_BACK,
  101. D3D11_CULL_FRONT
  102. };
  103. static const D3D11_FILL_MODE d3dFillMode[] =
  104. {
  105. D3D11_FILL_SOLID,
  106. D3D11_FILL_WIREFRAME,
  107. D3D11_FILL_WIREFRAME // Point fill mode not supported
  108. };
  109. static void GetD3DPrimitiveType(unsigned elementCount, PrimitiveType type, unsigned& primitiveCount,
  110. D3D_PRIMITIVE_TOPOLOGY& d3dPrimitiveType)
  111. {
  112. switch (type)
  113. {
  114. case TRIANGLE_LIST:
  115. primitiveCount = elementCount / 3;
  116. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
  117. break;
  118. case LINE_LIST:
  119. primitiveCount = elementCount / 2;
  120. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_LINELIST;
  121. break;
  122. case POINT_LIST:
  123. primitiveCount = elementCount;
  124. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_POINTLIST;
  125. break;
  126. case TRIANGLE_STRIP:
  127. primitiveCount = elementCount - 2;
  128. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
  129. break;
  130. case LINE_STRIP:
  131. primitiveCount = elementCount - 1;
  132. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP;
  133. break;
  134. case TRIANGLE_FAN:
  135. // Triangle fan is not supported on D3D11
  136. primitiveCount = 0;
  137. d3dPrimitiveType = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
  138. break;
  139. }
  140. }
  141. static HWND GetWindowHandle(SDL_Window* window)
  142. {
  143. SDL_SysWMinfo sysInfo;
  144. SDL_VERSION(&sysInfo.version);
  145. SDL_GetWindowWMInfo(window, &sysInfo);
  146. return sysInfo.info.win.window;
  147. }
  148. void Graphics::Constructor_D3D11()
  149. {
  150. impl_ = new GraphicsImpl_D3D11();
  151. position_ = IntVector2(SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED);
  152. shaderPath_ = "Shaders/HLSL/";
  153. shaderExtension_ = ".hlsl";
  154. orientations_ = "LandscapeLeft LandscapeRight";
  155. apiName_ = "D3D11";
  156. Graphics::gl3Support = false;
  157. SetTextureUnitMappings_D3D11();
  158. ResetCachedState_D3D11();
  159. context_->RequireSDL(SDL_INIT_VIDEO);
  160. // Register Graphics library object factories
  161. RegisterGraphicsLibrary(context_);
  162. }
  163. void Graphics::Destructor_D3D11()
  164. {
  165. {
  166. MutexLock lock(gpuObjectMutex_);
  167. // Release all GPU objects that still exist
  168. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  169. (*i)->Release();
  170. gpuObjects_.Clear();
  171. }
  172. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  173. impl->vertexDeclarations_.Clear();
  174. impl->allConstantBuffers_.Clear();
  175. for (HashMap<hash32, ID3D11BlendState*>::Iterator i = impl->blendStates_.Begin(); i != impl->blendStates_.End(); ++i)
  176. {
  177. URHO3D_SAFE_RELEASE(i->second_);
  178. }
  179. impl->blendStates_.Clear();
  180. for (HashMap<hash32, ID3D11DepthStencilState*>::Iterator i = impl->depthStates_.Begin(); i != impl->depthStates_.End(); ++i)
  181. {
  182. URHO3D_SAFE_RELEASE(i->second_);
  183. }
  184. impl->depthStates_.Clear();
  185. for (HashMap<hash32, ID3D11RasterizerState*>::Iterator i = impl->rasterizerStates_.Begin();
  186. i != impl->rasterizerStates_.End(); ++i)
  187. {
  188. URHO3D_SAFE_RELEASE(i->second_);
  189. }
  190. impl->rasterizerStates_.Clear();
  191. URHO3D_SAFE_RELEASE(impl->defaultRenderTargetView_);
  192. URHO3D_SAFE_RELEASE(impl->defaultDepthStencilView_);
  193. URHO3D_SAFE_RELEASE(impl->defaultDepthTexture_);
  194. URHO3D_SAFE_RELEASE(impl->resolveTexture_);
  195. URHO3D_SAFE_RELEASE(impl->swapChain_);
  196. URHO3D_SAFE_RELEASE(impl->deviceContext_);
  197. URHO3D_SAFE_RELEASE(impl->device_);
  198. if (window_)
  199. {
  200. SDL_ShowCursor(SDL_TRUE);
  201. SDL_DestroyWindow(window_);
  202. window_ = nullptr;
  203. }
  204. delete static_cast<GraphicsImpl_D3D11*>(impl_);
  205. impl_ = nullptr;
  206. context_->ReleaseSDL();
  207. }
  208. bool Graphics::SetScreenMode_D3D11(int width, int height, const ScreenModeParams& params, bool maximize)
  209. {
  210. URHO3D_PROFILE(SetScreenMode_D3D11);
  211. // Ensure that parameters are properly filled
  212. ScreenModeParams newParams = params;
  213. AdjustScreenMode(width, height, newParams, maximize);
  214. // Find out the full screen mode display format (match desktop color depth)
  215. SDL_DisplayMode mode;
  216. SDL_GetDesktopDisplayMode(newParams.monitor_, &mode);
  217. const DXGI_FORMAT fullscreenFormat = SDL_BITSPERPIXEL(mode.format) == 16 ? DXGI_FORMAT_B5G6R5_UNORM : DXGI_FORMAT_R8G8B8A8_UNORM;
  218. // If nothing changes, do not reset the device
  219. if (width == width_ && height == height_ && newParams == screenParams_)
  220. return true;
  221. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  222. if (!window_)
  223. {
  224. if (!OpenWindow_D3D11(width, height, newParams.resizable_, newParams.borderless_))
  225. return false;
  226. }
  227. AdjustWindow_D3D11(width, height, newParams.fullscreen_, newParams.borderless_, newParams.monitor_);
  228. if (maximize)
  229. {
  230. Maximize();
  231. SDL_GetWindowSize(window_, &width, &height);
  232. }
  233. const int oldMultiSample = screenParams_.multiSample_;
  234. screenParams_ = newParams;
  235. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  236. if (!impl->device_ || screenParams_.multiSample_ != oldMultiSample)
  237. CreateDevice_D3D11(width, height);
  238. UpdateSwapChain_D3D11(width, height);
  239. // Clear the initial window contents to black
  240. Clear_D3D11(CLEAR_COLOR);
  241. impl->swapChain_->Present(0, 0);
  242. OnScreenModeChanged();
  243. return true;
  244. }
  245. void Graphics::SetSRGB_D3D11(bool enable)
  246. {
  247. bool newEnable = enable && sRGBWriteSupport_;
  248. if (newEnable != sRGB_)
  249. {
  250. sRGB_ = newEnable;
  251. if (GetImpl_D3D11()->swapChain_)
  252. {
  253. // Recreate swap chain for the new backbuffer format
  254. CreateDevice_D3D11(width_, height_);
  255. UpdateSwapChain_D3D11(width_, height_);
  256. }
  257. }
  258. }
  259. void Graphics::SetDither_D3D11(bool enable)
  260. {
  261. // No effect on Direct3D11
  262. }
  263. void Graphics::SetFlushGPU_D3D11(bool enable)
  264. {
  265. flushGPU_ = enable;
  266. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  267. if (impl->device_)
  268. {
  269. IDXGIDevice1* dxgiDevice;
  270. impl->device_->QueryInterface(IID_IDXGIDevice1, (void**)&dxgiDevice);
  271. if (dxgiDevice)
  272. {
  273. dxgiDevice->SetMaximumFrameLatency(enable ? 1 : 3);
  274. dxgiDevice->Release();
  275. }
  276. }
  277. }
  278. void Graphics::SetForceGL2_D3D11(bool enable)
  279. {
  280. // No effect on Direct3D11
  281. }
  282. void Graphics::Close_D3D11()
  283. {
  284. if (window_)
  285. {
  286. SDL_ShowCursor(SDL_TRUE);
  287. SDL_DestroyWindow(window_);
  288. window_ = nullptr;
  289. }
  290. }
  291. bool Graphics::TakeScreenShot_D3D11(Image& destImage)
  292. {
  293. URHO3D_PROFILE(TakeScreenShot_D3D11);
  294. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  295. if (!impl->device_)
  296. return false;
  297. D3D11_TEXTURE2D_DESC textureDesc;
  298. memset(&textureDesc, 0, sizeof textureDesc);
  299. textureDesc.Width = (UINT)width_;
  300. textureDesc.Height = (UINT)height_;
  301. textureDesc.MipLevels = 1;
  302. textureDesc.ArraySize = 1;
  303. textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  304. textureDesc.SampleDesc.Count = 1;
  305. textureDesc.SampleDesc.Quality = 0;
  306. textureDesc.Usage = D3D11_USAGE_STAGING;
  307. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  308. ID3D11Texture2D* stagingTexture = nullptr;
  309. HRESULT hr = impl->device_->CreateTexture2D(&textureDesc, nullptr, &stagingTexture);
  310. if (FAILED(hr))
  311. {
  312. URHO3D_SAFE_RELEASE(stagingTexture);
  313. URHO3D_LOGD3DERROR("Could not create staging texture for screenshot", hr);
  314. return false;
  315. }
  316. ID3D11Resource* source = nullptr;
  317. impl->defaultRenderTargetView_->GetResource(&source);
  318. if (screenParams_.multiSample_ > 1)
  319. {
  320. // If backbuffer is multisampled, need another DEFAULT usage texture to resolve the data to first
  321. CreateResolveTexture_D3D11();
  322. if (!impl->resolveTexture_)
  323. {
  324. stagingTexture->Release();
  325. source->Release();
  326. return false;
  327. }
  328. impl->deviceContext_->ResolveSubresource(impl->resolveTexture_, 0, source, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
  329. impl->deviceContext_->CopyResource(stagingTexture, impl->resolveTexture_);
  330. }
  331. else
  332. impl->deviceContext_->CopyResource(stagingTexture, source);
  333. source->Release();
  334. D3D11_MAPPED_SUBRESOURCE mappedData;
  335. mappedData.pData = nullptr;
  336. hr = impl->deviceContext_->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mappedData);
  337. if (FAILED(hr) || !mappedData.pData)
  338. {
  339. URHO3D_LOGD3DERROR("Could not map staging texture for screenshot", hr);
  340. stagingTexture->Release();
  341. return false;
  342. }
  343. destImage.SetSize(width_, height_, 3);
  344. unsigned char* destData = destImage.GetData();
  345. for (int y = 0; y < height_; ++y)
  346. {
  347. unsigned char* src = (unsigned char*)mappedData.pData + y * mappedData.RowPitch;
  348. for (int x = 0; x < width_; ++x)
  349. {
  350. *destData++ = *src++;
  351. *destData++ = *src++;
  352. *destData++ = *src++;
  353. ++src;
  354. }
  355. }
  356. impl->deviceContext_->Unmap(stagingTexture, 0);
  357. stagingTexture->Release();
  358. return true;
  359. }
  360. bool Graphics::BeginFrame_D3D11()
  361. {
  362. if (!IsInitialized_D3D11())
  363. return false;
  364. // If using an external window, check it for size changes, and reset screen mode if necessary
  365. if (externalWindow_)
  366. {
  367. int width, height;
  368. SDL_GetWindowSize(window_, &width, &height);
  369. if (width != width_ || height != height_)
  370. SetMode(width, height);
  371. }
  372. else
  373. {
  374. // To prevent a loop of endless device loss and flicker, do not attempt to render when in fullscreen
  375. // and the window is minimized
  376. if (screenParams_.fullscreen_ && (SDL_GetWindowFlags(window_) & SDL_WINDOW_MINIMIZED))
  377. return false;
  378. }
  379. // Set default rendertarget and depth buffer
  380. ResetRenderTargets_D3D11();
  381. // Cleanup textures from previous frame
  382. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  383. SetTexture_D3D11(i, nullptr);
  384. numPrimitives_ = 0;
  385. numBatches_ = 0;
  386. SendEvent(E_BEGINRENDERING);
  387. return true;
  388. }
  389. void Graphics::EndFrame_D3D11()
  390. {
  391. if (!IsInitialized_D3D11())
  392. return;
  393. {
  394. URHO3D_PROFILE(Present);
  395. SendEvent(E_ENDRENDERING);
  396. GetImpl_D3D11()->swapChain_->Present(screenParams_.vsync_ ? 1 : 0, 0);
  397. }
  398. // Clean up too large scratch buffers
  399. CleanupScratchBuffers();
  400. }
  401. void Graphics::Clear_D3D11(ClearTargetFlags flags, const Color& color, float depth, u32 stencil)
  402. {
  403. IntVector2 rtSize = GetRenderTargetDimensions_D3D11();
  404. bool oldColorWrite = colorWrite_;
  405. bool oldDepthWrite = depthWrite_;
  406. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  407. // D3D11 clear always clears the whole target regardless of viewport or scissor test settings
  408. // Emulate partial clear by rendering a quad
  409. if (!viewport_.left_ && !viewport_.top_ && viewport_.right_ == rtSize.x_ && viewport_.bottom_ == rtSize.y_)
  410. {
  411. // Make sure we use the read-write version of the depth stencil
  412. SetDepthWrite_D3D11(true);
  413. PrepareDraw_D3D11();
  414. if ((flags & CLEAR_COLOR) && impl->renderTargetViews_[0])
  415. impl->deviceContext_->ClearRenderTargetView(impl->renderTargetViews_[0], color.Data());
  416. if ((flags & (CLEAR_DEPTH | CLEAR_STENCIL)) && impl->depthStencilView_)
  417. {
  418. unsigned depthClearFlags = 0;
  419. if (flags & CLEAR_DEPTH)
  420. depthClearFlags |= D3D11_CLEAR_DEPTH;
  421. if (flags & CLEAR_STENCIL)
  422. depthClearFlags |= D3D11_CLEAR_STENCIL;
  423. impl->deviceContext_->ClearDepthStencilView(impl->depthStencilView_, depthClearFlags, depth, (UINT8)stencil);
  424. }
  425. }
  426. else
  427. {
  428. Renderer* renderer = GetSubsystem<Renderer>();
  429. if (!renderer)
  430. return;
  431. Geometry* geometry = renderer->GetQuadGeometry();
  432. Matrix3x4 model = Matrix3x4::IDENTITY;
  433. Matrix4 projection = Matrix4::IDENTITY;
  434. model.m23_ = Clamp(depth, 0.0f, 1.0f);
  435. SetBlendMode_D3D11(BLEND_REPLACE);
  436. SetColorWrite_D3D11(flags & CLEAR_COLOR);
  437. SetCullMode_D3D11(CULL_NONE);
  438. SetDepthTest_D3D11(CMP_ALWAYS);
  439. SetDepthWrite_D3D11(flags & CLEAR_DEPTH);
  440. SetFillMode_D3D11(FILL_SOLID);
  441. SetScissorTest_D3D11(false);
  442. SetStencilTest_D3D11(flags & CLEAR_STENCIL, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, stencil);
  443. SetShaders_D3D11(GetShader_D3D11(VS, "ClearFramebuffer"), GetShader_D3D11(PS, "ClearFramebuffer"));
  444. SetShaderParameter_D3D11(VSP_MODEL, model);
  445. SetShaderParameter_D3D11(VSP_VIEWPROJ, projection);
  446. SetShaderParameter_D3D11(PSP_MATDIFFCOLOR, color);
  447. geometry->Draw(this);
  448. SetStencilTest_D3D11(false);
  449. ClearParameterSources_D3D11();
  450. }
  451. // Restore color & depth write state now
  452. SetColorWrite_D3D11(oldColorWrite);
  453. SetDepthWrite_D3D11(oldDepthWrite);
  454. }
  455. bool Graphics::ResolveToTexture_D3D11(Texture2D* destination, const IntRect& viewport)
  456. {
  457. if (!destination || !destination->GetRenderSurface())
  458. return false;
  459. URHO3D_PROFILE(ResolveToTexture_D3D11);
  460. IntRect vpCopy = viewport;
  461. if (vpCopy.right_ <= vpCopy.left_)
  462. vpCopy.right_ = vpCopy.left_ + 1;
  463. if (vpCopy.bottom_ <= vpCopy.top_)
  464. vpCopy.bottom_ = vpCopy.top_ + 1;
  465. D3D11_BOX srcBox;
  466. srcBox.left = Clamp(vpCopy.left_, 0, width_);
  467. srcBox.top = Clamp(vpCopy.top_, 0, height_);
  468. srcBox.right = Clamp(vpCopy.right_, 0, width_);
  469. srcBox.bottom = Clamp(vpCopy.bottom_, 0, height_);
  470. srcBox.front = 0;
  471. srcBox.back = 1;
  472. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  473. ID3D11Resource* source = nullptr;
  474. const bool resolve = screenParams_.multiSample_ > 1;
  475. impl->defaultRenderTargetView_->GetResource(&source);
  476. if (!resolve)
  477. {
  478. if (!srcBox.left && !srcBox.top && srcBox.right == width_ && srcBox.bottom == height_)
  479. impl->deviceContext_->CopyResource((ID3D11Resource*)destination->GetGPUObject(), source);
  480. else
  481. impl->deviceContext_->CopySubresourceRegion((ID3D11Resource*)destination->GetGPUObject(), 0, 0, 0, 0, source, 0, &srcBox);
  482. }
  483. else
  484. {
  485. if (!srcBox.left && !srcBox.top && srcBox.right == width_ && srcBox.bottom == height_)
  486. {
  487. impl->deviceContext_->ResolveSubresource((ID3D11Resource*)destination->GetGPUObject(), 0, source, 0, (DXGI_FORMAT)
  488. destination->GetFormat());
  489. }
  490. else
  491. {
  492. CreateResolveTexture_D3D11();
  493. if (impl->resolveTexture_)
  494. {
  495. impl->deviceContext_->ResolveSubresource(impl->resolveTexture_, 0, source, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
  496. impl->deviceContext_->CopySubresourceRegion((ID3D11Resource*)destination->GetGPUObject(), 0, 0, 0, 0, impl->resolveTexture_, 0, &srcBox);
  497. }
  498. }
  499. }
  500. source->Release();
  501. return true;
  502. }
  503. bool Graphics::ResolveToTexture_D3D11(Texture2D* texture)
  504. {
  505. if (!texture)
  506. return false;
  507. RenderSurface* surface = texture->GetRenderSurface();
  508. if (!surface)
  509. return false;
  510. texture->SetResolveDirty(false);
  511. surface->SetResolveDirty(false);
  512. ID3D11Resource* source = (ID3D11Resource*)texture->GetGPUObject();
  513. ID3D11Resource* dest = (ID3D11Resource*)texture->GetResolveTexture();
  514. if (!source || !dest)
  515. return false;
  516. GetImpl_D3D11()->deviceContext_->ResolveSubresource(dest, 0, source, 0, (DXGI_FORMAT)texture->GetFormat());
  517. return true;
  518. }
  519. bool Graphics::ResolveToTexture_D3D11(TextureCube* texture)
  520. {
  521. if (!texture)
  522. return false;
  523. texture->SetResolveDirty(false);
  524. ID3D11Resource* source = (ID3D11Resource*)texture->GetGPUObject();
  525. ID3D11Resource* dest = (ID3D11Resource*)texture->GetResolveTexture();
  526. if (!source || !dest)
  527. return false;
  528. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  529. {
  530. // Resolve only the surface(s) that were actually rendered to
  531. RenderSurface* surface = texture->GetRenderSurface((CubeMapFace)i);
  532. if (!surface->IsResolveDirty())
  533. continue;
  534. surface->SetResolveDirty(false);
  535. unsigned subResource = D3D11CalcSubresource(0, i, texture->GetLevels());
  536. GetImpl_D3D11()->deviceContext_->ResolveSubresource(dest, subResource, source, subResource, (DXGI_FORMAT)texture->GetFormat());
  537. }
  538. return true;
  539. }
  540. void Graphics::Draw_D3D11(PrimitiveType type, unsigned vertexStart, unsigned vertexCount)
  541. {
  542. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  543. if (!vertexCount || !impl->shaderProgram_)
  544. return;
  545. PrepareDraw_D3D11();
  546. unsigned primitiveCount;
  547. D3D_PRIMITIVE_TOPOLOGY d3dPrimitiveType;
  548. if (fillMode_ == FILL_POINT)
  549. type = POINT_LIST;
  550. GetD3DPrimitiveType(vertexCount, type, primitiveCount, d3dPrimitiveType);
  551. if (d3dPrimitiveType != primitiveType_)
  552. {
  553. impl->deviceContext_->IASetPrimitiveTopology(d3dPrimitiveType);
  554. primitiveType_ = d3dPrimitiveType;
  555. }
  556. impl->deviceContext_->Draw(vertexCount, vertexStart);
  557. numPrimitives_ += primitiveCount;
  558. ++numBatches_;
  559. }
  560. void Graphics::Draw_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  561. {
  562. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  563. if (!vertexCount || !impl->shaderProgram_)
  564. return;
  565. PrepareDraw_D3D11();
  566. unsigned primitiveCount;
  567. D3D_PRIMITIVE_TOPOLOGY d3dPrimitiveType;
  568. if (fillMode_ == FILL_POINT)
  569. type = POINT_LIST;
  570. GetD3DPrimitiveType(indexCount, type, primitiveCount, d3dPrimitiveType);
  571. if (d3dPrimitiveType != primitiveType_)
  572. {
  573. impl->deviceContext_->IASetPrimitiveTopology(d3dPrimitiveType);
  574. primitiveType_ = d3dPrimitiveType;
  575. }
  576. impl->deviceContext_->DrawIndexed(indexCount, indexStart, 0);
  577. numPrimitives_ += primitiveCount;
  578. ++numBatches_;
  579. }
  580. void Graphics::Draw_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount)
  581. {
  582. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  583. if (!vertexCount || !impl->shaderProgram_)
  584. return;
  585. PrepareDraw_D3D11();
  586. unsigned primitiveCount;
  587. D3D_PRIMITIVE_TOPOLOGY d3dPrimitiveType;
  588. if (fillMode_ == FILL_POINT)
  589. type = POINT_LIST;
  590. GetD3DPrimitiveType(indexCount, type, primitiveCount, d3dPrimitiveType);
  591. if (d3dPrimitiveType != primitiveType_)
  592. {
  593. impl->deviceContext_->IASetPrimitiveTopology(d3dPrimitiveType);
  594. primitiveType_ = d3dPrimitiveType;
  595. }
  596. impl->deviceContext_->DrawIndexed(indexCount, indexStart, baseVertexIndex);
  597. numPrimitives_ += primitiveCount;
  598. ++numBatches_;
  599. }
  600. void Graphics::DrawInstanced_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  601. unsigned instanceCount)
  602. {
  603. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  604. if (!indexCount || !instanceCount || !impl->shaderProgram_)
  605. return;
  606. PrepareDraw_D3D11();
  607. unsigned primitiveCount;
  608. D3D_PRIMITIVE_TOPOLOGY d3dPrimitiveType;
  609. if (fillMode_ == FILL_POINT)
  610. type = POINT_LIST;
  611. GetD3DPrimitiveType(indexCount, type, primitiveCount, d3dPrimitiveType);
  612. if (d3dPrimitiveType != primitiveType_)
  613. {
  614. impl->deviceContext_->IASetPrimitiveTopology(d3dPrimitiveType);
  615. primitiveType_ = d3dPrimitiveType;
  616. }
  617. impl->deviceContext_->DrawIndexedInstanced(indexCount, instanceCount, indexStart, 0, 0);
  618. numPrimitives_ += instanceCount * primitiveCount;
  619. ++numBatches_;
  620. }
  621. void Graphics::DrawInstanced_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount,
  622. unsigned instanceCount)
  623. {
  624. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  625. if (!indexCount || !instanceCount || !impl->shaderProgram_)
  626. return;
  627. PrepareDraw_D3D11();
  628. unsigned primitiveCount;
  629. D3D_PRIMITIVE_TOPOLOGY d3dPrimitiveType;
  630. if (fillMode_ == FILL_POINT)
  631. type = POINT_LIST;
  632. GetD3DPrimitiveType(indexCount, type, primitiveCount, d3dPrimitiveType);
  633. if (d3dPrimitiveType != primitiveType_)
  634. {
  635. impl->deviceContext_->IASetPrimitiveTopology(d3dPrimitiveType);
  636. primitiveType_ = d3dPrimitiveType;
  637. }
  638. impl->deviceContext_->DrawIndexedInstanced(indexCount, instanceCount, indexStart, baseVertexIndex, 0);
  639. numPrimitives_ += instanceCount * primitiveCount;
  640. ++numBatches_;
  641. }
  642. void Graphics::SetVertexBuffer_D3D11(VertexBuffer* buffer)
  643. {
  644. // Note: this is not multi-instance safe
  645. static Vector<VertexBuffer*> vertexBuffers(1);
  646. vertexBuffers[0] = buffer;
  647. SetVertexBuffers_D3D11(vertexBuffers);
  648. }
  649. bool Graphics::SetVertexBuffers_D3D11(const Vector<VertexBuffer*>& buffers, unsigned instanceOffset)
  650. {
  651. if (buffers.Size() > MAX_VERTEX_STREAMS)
  652. {
  653. URHO3D_LOGERROR("Too many vertex buffers");
  654. return false;
  655. }
  656. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  657. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  658. {
  659. VertexBuffer* buffer = nullptr;
  660. bool changed = false;
  661. buffer = i < buffers.Size() ? buffers[i] : nullptr;
  662. if (buffer)
  663. {
  664. const Vector<VertexElement>& elements = buffer->GetElements();
  665. // Check if buffer has per-instance data
  666. bool hasInstanceData = elements.Size() && elements[0].perInstance_;
  667. unsigned offset = hasInstanceData ? instanceOffset * buffer->GetVertexSize() : 0;
  668. if (buffer != vertexBuffers_[i] || offset != impl->vertexOffsets_[i])
  669. {
  670. vertexBuffers_[i] = buffer;
  671. impl->vertexBuffers_[i] = (ID3D11Buffer*)buffer->GetGPUObject();
  672. impl->vertexSizes_[i] = buffer->GetVertexSize();
  673. impl->vertexOffsets_[i] = offset;
  674. changed = true;
  675. }
  676. }
  677. else if (vertexBuffers_[i])
  678. {
  679. vertexBuffers_[i] = nullptr;
  680. impl->vertexBuffers_[i] = nullptr;
  681. impl->vertexSizes_[i] = 0;
  682. impl->vertexOffsets_[i] = 0;
  683. changed = true;
  684. }
  685. if (changed)
  686. {
  687. impl->vertexDeclarationDirty_ = true;
  688. if (impl->firstDirtyVB_ == M_MAX_UNSIGNED)
  689. impl->firstDirtyVB_ = impl->lastDirtyVB_ = i;
  690. else
  691. {
  692. if (i < impl->firstDirtyVB_)
  693. impl->firstDirtyVB_ = i;
  694. if (i > impl->lastDirtyVB_)
  695. impl->lastDirtyVB_ = i;
  696. }
  697. }
  698. }
  699. return true;
  700. }
  701. bool Graphics::SetVertexBuffers_D3D11(const Vector<SharedPtr<VertexBuffer>>& buffers, unsigned instanceOffset)
  702. {
  703. return SetVertexBuffers_D3D11(reinterpret_cast<const Vector<VertexBuffer*>&>(buffers), instanceOffset);
  704. }
  705. void Graphics::SetIndexBuffer_D3D11(IndexBuffer* buffer)
  706. {
  707. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  708. if (buffer != indexBuffer_)
  709. {
  710. if (buffer)
  711. impl->deviceContext_->IASetIndexBuffer((ID3D11Buffer*)buffer->GetGPUObject(),
  712. buffer->GetIndexSize() == sizeof(unsigned short) ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  713. else
  714. impl->deviceContext_->IASetIndexBuffer(nullptr, DXGI_FORMAT_UNKNOWN, 0);
  715. indexBuffer_ = buffer;
  716. }
  717. }
  718. void Graphics::SetShaders_D3D11(ShaderVariation* vs, ShaderVariation* ps)
  719. {
  720. // Switch to the clip plane variations if necessary
  721. if (useClipPlane_)
  722. {
  723. if (vs)
  724. vs = vs->GetOwner()->GetVariation(VS, vs->GetDefinesClipPlane());
  725. if (ps)
  726. ps = ps->GetOwner()->GetVariation(PS, ps->GetDefinesClipPlane());
  727. }
  728. if (vs == vertexShader_ && ps == pixelShader_)
  729. return;
  730. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  731. if (vs != vertexShader_)
  732. {
  733. // Create the shader now if not yet created. If already attempted, do not retry
  734. if (vs && !vs->GetGPUObject())
  735. {
  736. if (vs->GetCompilerOutput().Empty())
  737. {
  738. URHO3D_PROFILE(CompileVertexShader);
  739. bool success = vs->Create();
  740. if (!success)
  741. {
  742. URHO3D_LOGERROR("Failed to compile vertex shader " + vs->GetFullName() + ":\n" + vs->GetCompilerOutput());
  743. vs = nullptr;
  744. }
  745. }
  746. else
  747. vs = nullptr;
  748. }
  749. impl->deviceContext_->VSSetShader((ID3D11VertexShader*)(vs ? vs->GetGPUObject() : nullptr), nullptr, 0);
  750. vertexShader_ = vs;
  751. impl->vertexDeclarationDirty_ = true;
  752. }
  753. if (ps != pixelShader_)
  754. {
  755. if (ps && !ps->GetGPUObject())
  756. {
  757. if (ps->GetCompilerOutput().Empty())
  758. {
  759. URHO3D_PROFILE(CompilePixelShader);
  760. bool success = ps->Create();
  761. if (!success)
  762. {
  763. URHO3D_LOGERROR("Failed to compile pixel shader " + ps->GetFullName() + ":\n" + ps->GetCompilerOutput());
  764. ps = nullptr;
  765. }
  766. }
  767. else
  768. ps = nullptr;
  769. }
  770. impl->deviceContext_->PSSetShader((ID3D11PixelShader*)(ps ? ps->GetGPUObject() : nullptr), nullptr, 0);
  771. pixelShader_ = ps;
  772. }
  773. // Update current shader parameters & constant buffers
  774. if (vertexShader_ && pixelShader_)
  775. {
  776. Pair<ShaderVariation*, ShaderVariation*> key = MakePair(vertexShader_, pixelShader_);
  777. ShaderProgramMap_D3D11::Iterator i = impl->shaderPrograms_.Find(key);
  778. if (i != impl->shaderPrograms_.End())
  779. impl->shaderProgram_ = i->second_.Get();
  780. else
  781. {
  782. ShaderProgram_D3D11* newProgram = impl->shaderPrograms_[key] = new ShaderProgram_D3D11(this, vertexShader_, pixelShader_);
  783. impl->shaderProgram_ = newProgram;
  784. }
  785. bool vsBuffersChanged = false;
  786. bool psBuffersChanged = false;
  787. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  788. {
  789. ID3D11Buffer* vsBuffer = impl->shaderProgram_->vsConstantBuffers_[i] ? (ID3D11Buffer*)impl->shaderProgram_->vsConstantBuffers_[i]->
  790. GetGPUObject() : nullptr;
  791. if (vsBuffer != impl->constantBuffers_[VS][i])
  792. {
  793. impl->constantBuffers_[VS][i] = vsBuffer;
  794. shaderParameterSources_[i] = (const void*)M_MAX_UNSIGNED;
  795. vsBuffersChanged = true;
  796. }
  797. ID3D11Buffer* psBuffer = impl->shaderProgram_->psConstantBuffers_[i] ? (ID3D11Buffer*)impl->shaderProgram_->psConstantBuffers_[i]->
  798. GetGPUObject() : nullptr;
  799. if (psBuffer != impl->constantBuffers_[PS][i])
  800. {
  801. impl->constantBuffers_[PS][i] = psBuffer;
  802. shaderParameterSources_[i] = (const void*)M_MAX_UNSIGNED;
  803. psBuffersChanged = true;
  804. }
  805. }
  806. if (vsBuffersChanged)
  807. impl->deviceContext_->VSSetConstantBuffers(0, MAX_SHADER_PARAMETER_GROUPS, &impl->constantBuffers_[VS][0]);
  808. if (psBuffersChanged)
  809. impl->deviceContext_->PSSetConstantBuffers(0, MAX_SHADER_PARAMETER_GROUPS, &impl->constantBuffers_[PS][0]);
  810. }
  811. else
  812. impl->shaderProgram_ = nullptr;
  813. // Store shader combination if shader dumping in progress
  814. if (shaderPrecache_)
  815. shaderPrecache_->StoreShaders(vertexShader_, pixelShader_);
  816. // Update clip plane parameter if necessary
  817. if (useClipPlane_)
  818. SetShaderParameter_D3D11(VSP_CLIPPLANE, clipPlane_);
  819. }
  820. void Graphics::SetShaderParameter_D3D11(StringHash param, const float* data, unsigned count)
  821. {
  822. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  823. HashMap<StringHash, ShaderParameter>::Iterator i;
  824. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  825. return;
  826. ConstantBuffer* buffer = i->second_.bufferPtr_;
  827. if (!buffer->IsDirty())
  828. impl->dirtyConstantBuffers_.Push(buffer);
  829. buffer->SetParameter(i->second_.offset_, (unsigned)(count * sizeof(float)), data);
  830. }
  831. void Graphics::SetShaderParameter_D3D11(StringHash param, float value)
  832. {
  833. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  834. HashMap<StringHash, ShaderParameter>::Iterator i;
  835. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  836. return;
  837. ConstantBuffer* buffer = i->second_.bufferPtr_;
  838. if (!buffer->IsDirty())
  839. impl->dirtyConstantBuffers_.Push(buffer);
  840. buffer->SetParameter(i->second_.offset_, sizeof(float), &value);
  841. }
  842. void Graphics::SetShaderParameter_D3D11(StringHash param, int value)
  843. {
  844. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  845. HashMap<StringHash, ShaderParameter>::Iterator i;
  846. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  847. return;
  848. ConstantBuffer* buffer = i->second_.bufferPtr_;
  849. if (!buffer->IsDirty())
  850. impl->dirtyConstantBuffers_.Push(buffer);
  851. buffer->SetParameter(i->second_.offset_, sizeof(int), &value);
  852. }
  853. void Graphics::SetShaderParameter_D3D11(StringHash param, bool value)
  854. {
  855. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  856. HashMap<StringHash, ShaderParameter>::Iterator i;
  857. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  858. return;
  859. ConstantBuffer* buffer = i->second_.bufferPtr_;
  860. if (!buffer->IsDirty())
  861. impl->dirtyConstantBuffers_.Push(buffer);
  862. buffer->SetParameter(i->second_.offset_, sizeof(bool), &value);
  863. }
  864. void Graphics::SetShaderParameter_D3D11(StringHash param, const Color& color)
  865. {
  866. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  867. HashMap<StringHash, ShaderParameter>::Iterator i;
  868. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  869. return;
  870. ConstantBuffer* buffer = i->second_.bufferPtr_;
  871. if (!buffer->IsDirty())
  872. impl->dirtyConstantBuffers_.Push(buffer);
  873. buffer->SetParameter(i->second_.offset_, sizeof(Color), &color);
  874. }
  875. void Graphics::SetShaderParameter_D3D11(StringHash param, const Vector2& vector)
  876. {
  877. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  878. HashMap<StringHash, ShaderParameter>::Iterator i;
  879. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  880. return;
  881. ConstantBuffer* buffer = i->second_.bufferPtr_;
  882. if (!buffer->IsDirty())
  883. impl->dirtyConstantBuffers_.Push(buffer);
  884. buffer->SetParameter(i->second_.offset_, sizeof(Vector2), &vector);
  885. }
  886. void Graphics::SetShaderParameter_D3D11(StringHash param, const Matrix3& matrix)
  887. {
  888. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  889. HashMap<StringHash, ShaderParameter>::Iterator i;
  890. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  891. return;
  892. ConstantBuffer* buffer = i->second_.bufferPtr_;
  893. if (!buffer->IsDirty())
  894. impl->dirtyConstantBuffers_.Push(buffer);
  895. buffer->SetVector3ArrayParameter(i->second_.offset_, 3, &matrix);
  896. }
  897. void Graphics::SetShaderParameter_D3D11(StringHash param, const Vector3& vector)
  898. {
  899. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  900. HashMap<StringHash, ShaderParameter>::Iterator i;
  901. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  902. return;
  903. ConstantBuffer* buffer = i->second_.bufferPtr_;
  904. if (!buffer->IsDirty())
  905. impl->dirtyConstantBuffers_.Push(buffer);
  906. buffer->SetParameter(i->second_.offset_, sizeof(Vector3), &vector);
  907. }
  908. void Graphics::SetShaderParameter_D3D11(StringHash param, const Matrix4& matrix)
  909. {
  910. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  911. HashMap<StringHash, ShaderParameter>::Iterator i;
  912. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  913. return;
  914. ConstantBuffer* buffer = i->second_.bufferPtr_;
  915. if (!buffer->IsDirty())
  916. impl->dirtyConstantBuffers_.Push(buffer);
  917. buffer->SetParameter(i->second_.offset_, sizeof(Matrix4), &matrix);
  918. }
  919. void Graphics::SetShaderParameter_D3D11(StringHash param, const Vector4& vector)
  920. {
  921. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  922. HashMap<StringHash, ShaderParameter>::Iterator i;
  923. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  924. return;
  925. ConstantBuffer* buffer = i->second_.bufferPtr_;
  926. if (!buffer->IsDirty())
  927. impl->dirtyConstantBuffers_.Push(buffer);
  928. buffer->SetParameter(i->second_.offset_, sizeof(Vector4), &vector);
  929. }
  930. void Graphics::SetShaderParameter_D3D11(StringHash param, const Matrix3x4& matrix)
  931. {
  932. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  933. HashMap<StringHash, ShaderParameter>::Iterator i;
  934. if (!impl->shaderProgram_ || (i = impl->shaderProgram_->parameters_.Find(param)) == impl->shaderProgram_->parameters_.End())
  935. return;
  936. ConstantBuffer* buffer = i->second_.bufferPtr_;
  937. if (!buffer->IsDirty())
  938. impl->dirtyConstantBuffers_.Push(buffer);
  939. buffer->SetParameter(i->second_.offset_, sizeof(Matrix3x4), &matrix);
  940. }
  941. bool Graphics::NeedParameterUpdate_D3D11(ShaderParameterGroup group, const void* source)
  942. {
  943. if ((unsigned)(size_t)shaderParameterSources_[group] == M_MAX_UNSIGNED || shaderParameterSources_[group] != source)
  944. {
  945. shaderParameterSources_[group] = source;
  946. return true;
  947. }
  948. else
  949. return false;
  950. }
  951. bool Graphics::HasShaderParameter_D3D11(StringHash param)
  952. {
  953. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  954. return impl->shaderProgram_ && impl->shaderProgram_->parameters_.Find(param) != impl->shaderProgram_->parameters_.End();
  955. }
  956. bool Graphics::HasTextureUnit_D3D11(TextureUnit unit)
  957. {
  958. return (vertexShader_ && vertexShader_->HasTextureUnit(unit)) || (pixelShader_ && pixelShader_->HasTextureUnit(unit));
  959. }
  960. void Graphics::ClearParameterSource_D3D11(ShaderParameterGroup group)
  961. {
  962. shaderParameterSources_[group] = (const void*)M_MAX_UNSIGNED;
  963. }
  964. void Graphics::ClearParameterSources_D3D11()
  965. {
  966. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  967. shaderParameterSources_[i] = (const void*)M_MAX_UNSIGNED;
  968. }
  969. void Graphics::ClearTransformSources_D3D11()
  970. {
  971. shaderParameterSources_[SP_CAMERA] = (const void*)M_MAX_UNSIGNED;
  972. shaderParameterSources_[SP_OBJECT] = (const void*)M_MAX_UNSIGNED;
  973. }
  974. void Graphics::SetTexture_D3D11(unsigned index, Texture* texture)
  975. {
  976. if (index >= MAX_TEXTURE_UNITS)
  977. return;
  978. // Check if texture is currently bound as a rendertarget. In that case, use its backup texture, or blank if not defined
  979. if (texture)
  980. {
  981. if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
  982. texture = texture->GetBackupTexture();
  983. else
  984. {
  985. // Resolve multisampled texture now as necessary
  986. if (texture->GetMultiSample() > 1 && texture->GetAutoResolve() && texture->IsResolveDirty())
  987. {
  988. if (texture->GetType() == Texture2D::GetTypeStatic())
  989. ResolveToTexture_D3D11(static_cast<Texture2D*>(texture));
  990. if (texture->GetType() == TextureCube::GetTypeStatic())
  991. ResolveToTexture_D3D11(static_cast<TextureCube*>(texture));
  992. }
  993. }
  994. if (texture && texture->GetLevelsDirty())
  995. texture->RegenerateLevels();
  996. }
  997. if (texture && texture->GetParametersDirty())
  998. {
  999. texture->UpdateParameters();
  1000. textures_[index] = nullptr; // Force reassign
  1001. }
  1002. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1003. if (texture != textures_[index])
  1004. {
  1005. if (impl->firstDirtyTexture_ == M_MAX_UNSIGNED)
  1006. impl->firstDirtyTexture_ = impl->lastDirtyTexture_ = index;
  1007. else
  1008. {
  1009. if (index < impl->firstDirtyTexture_)
  1010. impl->firstDirtyTexture_ = index;
  1011. if (index > impl->lastDirtyTexture_)
  1012. impl->lastDirtyTexture_ = index;
  1013. }
  1014. textures_[index] = texture;
  1015. impl->shaderResourceViews_[index] = texture ? (ID3D11ShaderResourceView*)texture->GetShaderResourceView() : nullptr;
  1016. impl->samplers_[index] = texture ? (ID3D11SamplerState*)texture->GetSampler() : nullptr;
  1017. impl->texturesDirty_ = true;
  1018. }
  1019. }
  1020. void Graphics::SetDefaultTextureFilterMode_D3D11(TextureFilterMode mode)
  1021. {
  1022. if (mode != defaultTextureFilterMode_)
  1023. {
  1024. defaultTextureFilterMode_ = mode;
  1025. SetTextureParametersDirty_D3D11();
  1026. }
  1027. }
  1028. void Graphics::SetDefaultTextureAnisotropy_D3D11(unsigned level)
  1029. {
  1030. level = Max(level, 1U);
  1031. if (level != defaultTextureAnisotropy_)
  1032. {
  1033. defaultTextureAnisotropy_ = level;
  1034. SetTextureParametersDirty_D3D11();
  1035. }
  1036. }
  1037. void Graphics::SetTextureParametersDirty_D3D11()
  1038. {
  1039. MutexLock lock(gpuObjectMutex_);
  1040. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1041. {
  1042. Texture* texture = dynamic_cast<Texture*>(*i);
  1043. if (texture)
  1044. texture->SetParametersDirty();
  1045. }
  1046. }
  1047. void Graphics::ResetRenderTargets_D3D11()
  1048. {
  1049. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1050. SetRenderTarget_D3D11(i, (RenderSurface*)nullptr);
  1051. SetDepthStencil_D3D11((RenderSurface*)nullptr);
  1052. SetViewport_D3D11(IntRect(0, 0, width_, height_));
  1053. }
  1054. void Graphics::ResetRenderTarget_D3D11(unsigned index)
  1055. {
  1056. SetRenderTarget_D3D11(index, (RenderSurface*)nullptr);
  1057. }
  1058. void Graphics::ResetDepthStencil_D3D11()
  1059. {
  1060. SetDepthStencil_D3D11((RenderSurface*)nullptr);
  1061. }
  1062. void Graphics::SetRenderTarget_D3D11(unsigned index, RenderSurface* renderTarget)
  1063. {
  1064. if (index >= MAX_RENDERTARGETS)
  1065. return;
  1066. if (renderTarget != renderTargets_[index])
  1067. {
  1068. renderTargets_[index] = renderTarget;
  1069. GetImpl_D3D11()->renderTargetsDirty_ = true;
  1070. // If the rendertarget is also bound as a texture, replace with backup texture or null
  1071. if (renderTarget)
  1072. {
  1073. Texture* parentTexture = renderTarget->GetParentTexture();
  1074. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1075. {
  1076. if (textures_[i] == parentTexture)
  1077. SetTexture_D3D11(i, textures_[i]->GetBackupTexture());
  1078. }
  1079. // If multisampled, mark the texture & surface needing resolve
  1080. if (parentTexture->GetMultiSample() > 1 && parentTexture->GetAutoResolve())
  1081. {
  1082. parentTexture->SetResolveDirty(true);
  1083. renderTarget->SetResolveDirty(true);
  1084. }
  1085. // If mipmapped, mark the levels needing regeneration
  1086. if (parentTexture->GetLevels() > 1)
  1087. parentTexture->SetLevelsDirty();
  1088. }
  1089. }
  1090. }
  1091. void Graphics::SetRenderTarget_D3D11(unsigned index, Texture2D* texture)
  1092. {
  1093. RenderSurface* renderTarget = nullptr;
  1094. if (texture)
  1095. renderTarget = texture->GetRenderSurface();
  1096. SetRenderTarget_D3D11(index, renderTarget);
  1097. }
  1098. void Graphics::SetDepthStencil_D3D11(RenderSurface* depthStencil)
  1099. {
  1100. if (depthStencil != depthStencil_)
  1101. {
  1102. depthStencil_ = depthStencil;
  1103. GetImpl_D3D11()->renderTargetsDirty_ = true;
  1104. }
  1105. }
  1106. void Graphics::SetDepthStencil_D3D11(Texture2D* texture)
  1107. {
  1108. RenderSurface* depthStencil = nullptr;
  1109. if (texture)
  1110. depthStencil = texture->GetRenderSurface();
  1111. SetDepthStencil_D3D11(depthStencil);
  1112. // Constant depth bias depends on the bitdepth
  1113. GetImpl_D3D11()->rasterizerStateDirty_ = true;
  1114. }
  1115. void Graphics::SetViewport_D3D11(const IntRect& rect)
  1116. {
  1117. IntVector2 size = GetRenderTargetDimensions_D3D11();
  1118. IntRect rectCopy = rect;
  1119. if (rectCopy.right_ <= rectCopy.left_)
  1120. rectCopy.right_ = rectCopy.left_ + 1;
  1121. if (rectCopy.bottom_ <= rectCopy.top_)
  1122. rectCopy.bottom_ = rectCopy.top_ + 1;
  1123. rectCopy.left_ = Clamp(rectCopy.left_, 0, size.x_);
  1124. rectCopy.top_ = Clamp(rectCopy.top_, 0, size.y_);
  1125. rectCopy.right_ = Clamp(rectCopy.right_, 0, size.x_);
  1126. rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, size.y_);
  1127. static D3D11_VIEWPORT d3dViewport;
  1128. d3dViewport.TopLeftX = (float)rectCopy.left_;
  1129. d3dViewport.TopLeftY = (float)rectCopy.top_;
  1130. d3dViewport.Width = (float)(rectCopy.right_ - rectCopy.left_);
  1131. d3dViewport.Height = (float)(rectCopy.bottom_ - rectCopy.top_);
  1132. d3dViewport.MinDepth = 0.0f;
  1133. d3dViewport.MaxDepth = 1.0f;
  1134. GetImpl_D3D11()->deviceContext_->RSSetViewports(1, &d3dViewport);
  1135. viewport_ = rectCopy;
  1136. // Disable scissor test, needs to be re-enabled by the user
  1137. SetScissorTest_D3D11(false);
  1138. }
  1139. void Graphics::SetBlendMode_D3D11(BlendMode mode, bool alphaToCoverage)
  1140. {
  1141. if (mode != blendMode_ || alphaToCoverage != alphaToCoverage_)
  1142. {
  1143. blendMode_ = mode;
  1144. alphaToCoverage_ = alphaToCoverage;
  1145. GetImpl_D3D11()->blendStateDirty_ = true;
  1146. }
  1147. }
  1148. void Graphics::SetColorWrite_D3D11(bool enable)
  1149. {
  1150. if (enable != colorWrite_)
  1151. {
  1152. colorWrite_ = enable;
  1153. GetImpl_D3D11()->blendStateDirty_ = true;
  1154. }
  1155. }
  1156. void Graphics::SetCullMode_D3D11(CullMode mode)
  1157. {
  1158. if (mode != cullMode_)
  1159. {
  1160. cullMode_ = mode;
  1161. GetImpl_D3D11()->rasterizerStateDirty_ = true;
  1162. }
  1163. }
  1164. void Graphics::SetDepthBias_D3D11(float constantBias, float slopeScaledBias)
  1165. {
  1166. if (constantBias != constantDepthBias_ || slopeScaledBias != slopeScaledDepthBias_)
  1167. {
  1168. constantDepthBias_ = constantBias;
  1169. slopeScaledDepthBias_ = slopeScaledBias;
  1170. GetImpl_D3D11()->rasterizerStateDirty_ = true;
  1171. }
  1172. }
  1173. void Graphics::SetDepthTest_D3D11(CompareMode mode)
  1174. {
  1175. if (mode != depthTestMode_)
  1176. {
  1177. depthTestMode_ = mode;
  1178. GetImpl_D3D11()->depthStateDirty_ = true;
  1179. }
  1180. }
  1181. void Graphics::SetDepthWrite_D3D11(bool enable)
  1182. {
  1183. if (enable != depthWrite_)
  1184. {
  1185. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1186. depthWrite_ = enable;
  1187. impl->depthStateDirty_ = true;
  1188. // Also affects whether a read-only version of depth-stencil should be bound, to allow sampling
  1189. impl->renderTargetsDirty_ = true;
  1190. }
  1191. }
  1192. void Graphics::SetFillMode_D3D11(FillMode mode)
  1193. {
  1194. if (mode != fillMode_)
  1195. {
  1196. fillMode_ = mode;
  1197. GetImpl_D3D11()->rasterizerStateDirty_ = true;
  1198. }
  1199. }
  1200. void Graphics::SetLineAntiAlias_D3D11(bool enable)
  1201. {
  1202. if (enable != lineAntiAlias_)
  1203. {
  1204. lineAntiAlias_ = enable;
  1205. GetImpl_D3D11()->rasterizerStateDirty_ = true;
  1206. }
  1207. }
  1208. void Graphics::SetScissorTest_D3D11(bool enable, const Rect& rect, bool borderInclusive)
  1209. {
  1210. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1211. // Disable scissor in that case to reduce state changes
  1212. if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.x_ >= 1.0f && rect.max_.y_ >= 1.0f)
  1213. enable = false;
  1214. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1215. if (enable)
  1216. {
  1217. IntVector2 rtSize(GetRenderTargetDimensions_D3D11());
  1218. IntVector2 viewSize(viewport_.Size());
  1219. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1220. IntRect intRect;
  1221. int expand = borderInclusive ? 1 : 0;
  1222. intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
  1223. intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
  1224. intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
  1225. intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
  1226. if (intRect.right_ == intRect.left_)
  1227. intRect.right_++;
  1228. if (intRect.bottom_ == intRect.top_)
  1229. intRect.bottom_++;
  1230. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1231. enable = false;
  1232. if (enable && intRect != scissorRect_)
  1233. {
  1234. scissorRect_ = intRect;
  1235. impl->scissorRectDirty_ = true;
  1236. }
  1237. }
  1238. if (enable != scissorTest_)
  1239. {
  1240. scissorTest_ = enable;
  1241. impl->rasterizerStateDirty_ = true;
  1242. }
  1243. }
  1244. void Graphics::SetScissorTest_D3D11(bool enable, const IntRect& rect)
  1245. {
  1246. IntVector2 rtSize(GetRenderTargetDimensions_D3D11());
  1247. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1248. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1249. if (enable)
  1250. {
  1251. IntRect intRect;
  1252. intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
  1253. intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
  1254. intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
  1255. intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
  1256. if (intRect.right_ == intRect.left_)
  1257. intRect.right_++;
  1258. if (intRect.bottom_ == intRect.top_)
  1259. intRect.bottom_++;
  1260. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1261. enable = false;
  1262. if (enable && intRect != scissorRect_)
  1263. {
  1264. scissorRect_ = intRect;
  1265. impl->scissorRectDirty_ = true;
  1266. }
  1267. }
  1268. if (enable != scissorTest_)
  1269. {
  1270. scissorTest_ = enable;
  1271. impl->rasterizerStateDirty_ = true;
  1272. }
  1273. }
  1274. void Graphics::SetStencilTest_D3D11(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, u32 stencilRef,
  1275. u32 compareMask, u32 writeMask)
  1276. {
  1277. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1278. if (enable != stencilTest_)
  1279. {
  1280. stencilTest_ = enable;
  1281. impl->depthStateDirty_ = true;
  1282. }
  1283. if (enable)
  1284. {
  1285. if (mode != stencilTestMode_)
  1286. {
  1287. stencilTestMode_ = mode;
  1288. impl->depthStateDirty_ = true;
  1289. }
  1290. if (pass != stencilPass_)
  1291. {
  1292. stencilPass_ = pass;
  1293. impl->depthStateDirty_ = true;
  1294. }
  1295. if (fail != stencilFail_)
  1296. {
  1297. stencilFail_ = fail;
  1298. impl->depthStateDirty_ = true;
  1299. }
  1300. if (zFail != stencilZFail_)
  1301. {
  1302. stencilZFail_ = zFail;
  1303. impl->depthStateDirty_ = true;
  1304. }
  1305. if (compareMask != stencilCompareMask_)
  1306. {
  1307. stencilCompareMask_ = compareMask;
  1308. impl->depthStateDirty_ = true;
  1309. }
  1310. if (writeMask != stencilWriteMask_)
  1311. {
  1312. stencilWriteMask_ = writeMask;
  1313. impl->depthStateDirty_ = true;
  1314. }
  1315. if (stencilRef != stencilRef_)
  1316. {
  1317. stencilRef_ = stencilRef;
  1318. impl->stencilRefDirty_ = true;
  1319. impl->depthStateDirty_ = true;
  1320. }
  1321. }
  1322. }
  1323. void Graphics::SetClipPlane_D3D11(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection)
  1324. {
  1325. useClipPlane_ = enable;
  1326. if (enable)
  1327. {
  1328. Matrix4 viewProj = projection * view;
  1329. clipPlane_ = clipPlane.Transformed(viewProj).ToVector4();
  1330. SetShaderParameter_D3D11(VSP_CLIPPLANE, clipPlane_);
  1331. }
  1332. }
  1333. bool Graphics::IsInitialized_D3D11() const
  1334. {
  1335. return window_ != nullptr && GetImpl_D3D11()->GetDevice() != nullptr;
  1336. }
  1337. Vector<int> Graphics::GetMultiSampleLevels_D3D11() const
  1338. {
  1339. Vector<int> ret;
  1340. ret.Push(1);
  1341. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1342. if (impl->device_)
  1343. {
  1344. for (unsigned i = 2; i <= 16; ++i)
  1345. {
  1346. if (impl->CheckMultiSampleSupport(sRGB_ ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM, i))
  1347. ret.Push(i);
  1348. }
  1349. }
  1350. return ret;
  1351. }
  1352. unsigned Graphics::GetFormat_D3D11(CompressedFormat format) const
  1353. {
  1354. switch (format)
  1355. {
  1356. case CF_RGBA:
  1357. return DXGI_FORMAT_R8G8B8A8_UNORM;
  1358. case CF_DXT1:
  1359. return DXGI_FORMAT_BC1_UNORM;
  1360. case CF_DXT3:
  1361. return DXGI_FORMAT_BC2_UNORM;
  1362. case CF_DXT5:
  1363. return DXGI_FORMAT_BC3_UNORM;
  1364. default:
  1365. return 0;
  1366. }
  1367. }
  1368. ShaderVariation* Graphics::GetShader_D3D11(ShaderType type, const String& name, const String& defines) const
  1369. {
  1370. return GetShader_D3D11(type, name.CString(), defines.CString());
  1371. }
  1372. ShaderVariation* Graphics::GetShader_D3D11(ShaderType type, const char* name, const char* defines) const
  1373. {
  1374. if (lastShaderName_ != name || !lastShader_)
  1375. {
  1376. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1377. String fullShaderName = shaderPath_ + name + shaderExtension_;
  1378. // Try to reduce repeated error log prints because of missing shaders
  1379. if (lastShaderName_ == name && !cache->Exists(fullShaderName))
  1380. return nullptr;
  1381. lastShader_ = cache->GetResource<Shader>(fullShaderName);
  1382. lastShaderName_ = name;
  1383. }
  1384. return lastShader_ ? lastShader_->GetVariation(type, defines) : nullptr;
  1385. }
  1386. VertexBuffer* Graphics::GetVertexBuffer_D3D11(unsigned index) const
  1387. {
  1388. return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : nullptr;
  1389. }
  1390. ShaderProgram_D3D11* Graphics::GetShaderProgram_D3D11() const
  1391. {
  1392. return GetImpl_D3D11()->shaderProgram_;
  1393. }
  1394. TextureUnit Graphics::GetTextureUnit_D3D11(const String& name)
  1395. {
  1396. HashMap<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
  1397. if (i != textureUnits_.End())
  1398. return i->second_;
  1399. else
  1400. return MAX_TEXTURE_UNITS;
  1401. }
  1402. const String& Graphics::GetTextureUnitName_D3D11(TextureUnit unit)
  1403. {
  1404. for (HashMap<String, TextureUnit>::Iterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
  1405. {
  1406. if (i->second_ == unit)
  1407. return i->first_;
  1408. }
  1409. return String::EMPTY;
  1410. }
  1411. Texture* Graphics::GetTexture_D3D11(unsigned index) const
  1412. {
  1413. return index < MAX_TEXTURE_UNITS ? textures_[index] : nullptr;
  1414. }
  1415. RenderSurface* Graphics::GetRenderTarget_D3D11(unsigned index) const
  1416. {
  1417. return index < MAX_RENDERTARGETS ? renderTargets_[index] : nullptr;
  1418. }
  1419. IntVector2 Graphics::GetRenderTargetDimensions_D3D11() const
  1420. {
  1421. int width, height;
  1422. if (renderTargets_[0])
  1423. {
  1424. width = renderTargets_[0]->GetWidth();
  1425. height = renderTargets_[0]->GetHeight();
  1426. }
  1427. else if (depthStencil_) // Depth-only rendering
  1428. {
  1429. width = depthStencil_->GetWidth();
  1430. height = depthStencil_->GetHeight();
  1431. }
  1432. else
  1433. {
  1434. width = width_;
  1435. height = height_;
  1436. }
  1437. return IntVector2(width, height);
  1438. }
  1439. bool Graphics::GetDither_D3D11() const
  1440. {
  1441. return false;
  1442. }
  1443. bool Graphics::IsDeviceLost_D3D11() const
  1444. {
  1445. // Direct3D11 graphics context is never considered lost
  1446. /// \todo The device could be lost in case of graphics adapters getting disabled during runtime. This is not currently handled
  1447. return false;
  1448. }
  1449. void Graphics::OnWindowResized_D3D11()
  1450. {
  1451. if (!GetImpl_D3D11()->device_ || !window_)
  1452. return;
  1453. int newWidth, newHeight;
  1454. SDL_GetWindowSize(window_, &newWidth, &newHeight);
  1455. if (newWidth == width_ && newHeight == height_)
  1456. return;
  1457. UpdateSwapChain_D3D11(newWidth, newHeight);
  1458. // Reset rendertargets and viewport for the new screen size
  1459. ResetRenderTargets_D3D11();
  1460. URHO3D_LOGDEBUGF("Window was resized to %dx%d", width_, height_);
  1461. using namespace ScreenMode;
  1462. VariantMap& eventData = GetEventDataMap();
  1463. eventData[P_WIDTH] = width_;
  1464. eventData[P_HEIGHT] = height_;
  1465. eventData[P_FULLSCREEN] = screenParams_.fullscreen_;
  1466. eventData[P_RESIZABLE] = screenParams_.resizable_;
  1467. eventData[P_BORDERLESS] = screenParams_.borderless_;
  1468. eventData[P_HIGHDPI] = screenParams_.highDPI_;
  1469. SendEvent(E_SCREENMODE, eventData);
  1470. }
  1471. void Graphics::OnWindowMoved_D3D11()
  1472. {
  1473. if (!GetImpl_D3D11()->device_ || !window_ || screenParams_.fullscreen_)
  1474. return;
  1475. int newX, newY;
  1476. SDL_GetWindowPosition(window_, &newX, &newY);
  1477. if (newX == position_.x_ && newY == position_.y_)
  1478. return;
  1479. position_.x_ = newX;
  1480. position_.y_ = newY;
  1481. URHO3D_LOGTRACEF("Window was moved to %d,%d", position_.x_, position_.y_);
  1482. using namespace WindowPos;
  1483. VariantMap& eventData = GetEventDataMap();
  1484. eventData[P_X] = position_.x_;
  1485. eventData[P_Y] = position_.y_;
  1486. SendEvent(E_WINDOWPOS, eventData);
  1487. }
  1488. void Graphics::CleanupShaderPrograms_D3D11(ShaderVariation* variation)
  1489. {
  1490. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1491. for (ShaderProgramMap_D3D11::Iterator i = impl->shaderPrograms_.Begin(); i != impl->shaderPrograms_.End();)
  1492. {
  1493. if (i->first_.first_ == variation || i->first_.second_ == variation)
  1494. i = impl->shaderPrograms_.Erase(i);
  1495. else
  1496. ++i;
  1497. }
  1498. if (vertexShader_ == variation || pixelShader_ == variation)
  1499. impl->shaderProgram_ = nullptr;
  1500. }
  1501. ConstantBuffer* Graphics::GetOrCreateConstantBuffer_D3D11(ShaderType type, unsigned index, unsigned size)
  1502. {
  1503. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1504. // Ensure that different shader types and index slots get unique buffers, even if the size is same
  1505. hash32 key = type | (index << 1) | (size << 4);
  1506. ConstantBufferMap::Iterator i = impl->allConstantBuffers_.Find(key);
  1507. if (i != impl->allConstantBuffers_.End())
  1508. return i->second_.Get();
  1509. else
  1510. {
  1511. SharedPtr<ConstantBuffer> newConstantBuffer(new ConstantBuffer(context_));
  1512. newConstantBuffer->SetSize(size);
  1513. impl->allConstantBuffers_[key] = newConstantBuffer;
  1514. return newConstantBuffer.Get();
  1515. }
  1516. }
  1517. unsigned Graphics::GetAlphaFormat_D3D11()
  1518. {
  1519. return DXGI_FORMAT_A8_UNORM;
  1520. }
  1521. unsigned Graphics::GetLuminanceFormat_D3D11()
  1522. {
  1523. // Note: not same sampling behavior as on D3D9; need to sample the R channel only
  1524. return DXGI_FORMAT_R8_UNORM;
  1525. }
  1526. unsigned Graphics::GetLuminanceAlphaFormat_D3D11()
  1527. {
  1528. // Note: not same sampling behavior as on D3D9; need to sample the RG channels
  1529. return DXGI_FORMAT_R8G8_UNORM;
  1530. }
  1531. unsigned Graphics::GetRGBFormat_D3D11()
  1532. {
  1533. return DXGI_FORMAT_R8G8B8A8_UNORM;
  1534. }
  1535. unsigned Graphics::GetRGBAFormat_D3D11()
  1536. {
  1537. return DXGI_FORMAT_R8G8B8A8_UNORM;
  1538. }
  1539. unsigned Graphics::GetRGBA16Format_D3D11()
  1540. {
  1541. return DXGI_FORMAT_R16G16B16A16_UNORM;
  1542. }
  1543. unsigned Graphics::GetRGBAFloat16Format_D3D11()
  1544. {
  1545. return DXGI_FORMAT_R16G16B16A16_FLOAT;
  1546. }
  1547. unsigned Graphics::GetRGBAFloat32Format_D3D11()
  1548. {
  1549. return DXGI_FORMAT_R32G32B32A32_FLOAT;
  1550. }
  1551. unsigned Graphics::GetRG16Format_D3D11()
  1552. {
  1553. return DXGI_FORMAT_R16G16_UNORM;
  1554. }
  1555. unsigned Graphics::GetRGFloat16Format_D3D11()
  1556. {
  1557. return DXGI_FORMAT_R16G16_FLOAT;
  1558. }
  1559. unsigned Graphics::GetRGFloat32Format_D3D11()
  1560. {
  1561. return DXGI_FORMAT_R32G32_FLOAT;
  1562. }
  1563. unsigned Graphics::GetFloat16Format_D3D11()
  1564. {
  1565. return DXGI_FORMAT_R16_FLOAT;
  1566. }
  1567. unsigned Graphics::GetFloat32Format_D3D11()
  1568. {
  1569. return DXGI_FORMAT_R32_FLOAT;
  1570. }
  1571. unsigned Graphics::GetLinearDepthFormat_D3D11()
  1572. {
  1573. return DXGI_FORMAT_R32_FLOAT;
  1574. }
  1575. unsigned Graphics::GetDepthStencilFormat_D3D11()
  1576. {
  1577. return DXGI_FORMAT_R24G8_TYPELESS;
  1578. }
  1579. unsigned Graphics::GetReadableDepthFormat_D3D11()
  1580. {
  1581. return DXGI_FORMAT_R24G8_TYPELESS;
  1582. }
  1583. unsigned Graphics::GetFormat_D3D11(const String& formatName)
  1584. {
  1585. String nameLower = formatName.ToLower().Trimmed();
  1586. if (nameLower == "a")
  1587. return GetAlphaFormat_D3D11();
  1588. if (nameLower == "l")
  1589. return GetLuminanceFormat_D3D11();
  1590. if (nameLower == "la")
  1591. return GetLuminanceAlphaFormat_D3D11();
  1592. if (nameLower == "rgb")
  1593. return GetRGBFormat_D3D11();
  1594. if (nameLower == "rgba")
  1595. return GetRGBAFormat_D3D11();
  1596. if (nameLower == "rgba16")
  1597. return GetRGBA16Format_D3D11();
  1598. if (nameLower == "rgba16f")
  1599. return GetRGBAFloat16Format_D3D11();
  1600. if (nameLower == "rgba32f")
  1601. return GetRGBAFloat32Format_D3D11();
  1602. if (nameLower == "rg16")
  1603. return GetRG16Format_D3D11();
  1604. if (nameLower == "rg16f")
  1605. return GetRGFloat16Format_D3D11();
  1606. if (nameLower == "rg32f")
  1607. return GetRGFloat32Format_D3D11();
  1608. if (nameLower == "r16f")
  1609. return GetFloat16Format_D3D11();
  1610. if (nameLower == "r32f" || nameLower == "float")
  1611. return GetFloat32Format_D3D11();
  1612. if (nameLower == "lineardepth" || nameLower == "depth")
  1613. return GetLinearDepthFormat_D3D11();
  1614. if (nameLower == "d24s8")
  1615. return GetDepthStencilFormat_D3D11();
  1616. if (nameLower == "readabledepth" || nameLower == "hwdepth")
  1617. return GetReadableDepthFormat_D3D11();
  1618. return GetRGBFormat_D3D11();
  1619. }
  1620. unsigned Graphics::GetMaxBones_D3D11()
  1621. {
  1622. return 128;
  1623. }
  1624. bool Graphics::GetGL3Support_D3D11()
  1625. {
  1626. return false;
  1627. }
  1628. bool Graphics::OpenWindow_D3D11(int width, int height, bool resizable, bool borderless)
  1629. {
  1630. if (!externalWindow_)
  1631. {
  1632. unsigned flags = 0;
  1633. if (resizable)
  1634. flags |= SDL_WINDOW_RESIZABLE;
  1635. if (borderless)
  1636. flags |= SDL_WINDOW_BORDERLESS;
  1637. window_ = SDL_CreateWindow(windowTitle_.CString(), position_.x_, position_.y_, width, height, flags);
  1638. }
  1639. else
  1640. window_ = SDL_CreateWindowFrom(externalWindow_, 0);
  1641. if (!window_)
  1642. {
  1643. URHO3D_LOGERRORF("Could not create window, root cause: '%s'", SDL_GetError());
  1644. return false;
  1645. }
  1646. SDL_GetWindowPosition(window_, &position_.x_, &position_.y_);
  1647. CreateWindowIcon();
  1648. return true;
  1649. }
  1650. void Graphics::AdjustWindow_D3D11(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless, int& monitor)
  1651. {
  1652. if (!externalWindow_)
  1653. {
  1654. // Keep current window position because it may change in intermediate callbacks
  1655. const IntVector2 oldPosition = position_;
  1656. bool reposition = false;
  1657. bool resizePostponed = false;
  1658. if (!newWidth || !newHeight)
  1659. {
  1660. SDL_MaximizeWindow(window_);
  1661. SDL_GetWindowSize(window_, &newWidth, &newHeight);
  1662. }
  1663. else
  1664. {
  1665. SDL_Rect display_rect;
  1666. SDL_GetDisplayBounds(monitor, &display_rect);
  1667. reposition = newFullscreen || (newBorderless && newWidth >= display_rect.w && newHeight >= display_rect.h);
  1668. if (reposition)
  1669. {
  1670. // Reposition the window on the specified monitor if it's supposed to cover the entire monitor
  1671. SDL_SetWindowPosition(window_, display_rect.x, display_rect.y);
  1672. }
  1673. // Postpone window resize if exiting fullscreen to avoid redundant resolution change
  1674. if (!newFullscreen && screenParams_.fullscreen_)
  1675. resizePostponed = true;
  1676. else
  1677. SDL_SetWindowSize(window_, newWidth, newHeight);
  1678. }
  1679. // Turn off window fullscreen mode so it gets repositioned to the correct monitor
  1680. SDL_SetWindowFullscreen(window_, SDL_FALSE);
  1681. // Hack fix: on SDL 2.0.4 a fullscreen->windowed transition results in a maximized window when the D3D device is reset, so hide before
  1682. if (!newFullscreen) SDL_HideWindow(window_);
  1683. SDL_SetWindowFullscreen(window_, newFullscreen ? SDL_WINDOW_FULLSCREEN : 0);
  1684. SDL_SetWindowBordered(window_, newBorderless ? SDL_FALSE : SDL_TRUE);
  1685. if (!newFullscreen) SDL_ShowWindow(window_);
  1686. // Resize now if was postponed
  1687. if (resizePostponed)
  1688. SDL_SetWindowSize(window_, newWidth, newHeight);
  1689. // Ensure that window keeps its position
  1690. if (!reposition)
  1691. SDL_SetWindowPosition(window_, oldPosition.x_, oldPosition.y_);
  1692. else
  1693. position_ = oldPosition;
  1694. }
  1695. else
  1696. {
  1697. // If external window, must ask its dimensions instead of trying to set them
  1698. SDL_GetWindowSize(window_, &newWidth, &newHeight);
  1699. newFullscreen = false;
  1700. }
  1701. }
  1702. bool Graphics::CreateDevice_D3D11(int width, int height)
  1703. {
  1704. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1705. // Device needs only to be created once
  1706. if (!impl->device_)
  1707. {
  1708. HRESULT hr = D3D11CreateDevice(
  1709. nullptr,
  1710. D3D_DRIVER_TYPE_HARDWARE,
  1711. nullptr,
  1712. 0,
  1713. nullptr,
  1714. 0,
  1715. D3D11_SDK_VERSION,
  1716. &impl->device_,
  1717. nullptr,
  1718. &impl->deviceContext_
  1719. );
  1720. if (FAILED(hr))
  1721. {
  1722. URHO3D_SAFE_RELEASE(impl->device_);
  1723. URHO3D_SAFE_RELEASE(impl->deviceContext_);
  1724. URHO3D_LOGD3DERROR("Failed to create D3D11 device", hr);
  1725. return false;
  1726. }
  1727. CheckFeatureSupport_D3D11();
  1728. // Set the flush mode now as the device has been created
  1729. SetFlushGPU_D3D11(flushGPU_);
  1730. }
  1731. // Check that multisample level is supported
  1732. Vector<int> multiSampleLevels = GetMultiSampleLevels_D3D11();
  1733. if (!multiSampleLevels.Contains(screenParams_.multiSample_))
  1734. screenParams_.multiSample_ = 1;
  1735. // Create swap chain. Release old if necessary
  1736. if (impl->swapChain_)
  1737. {
  1738. impl->swapChain_->Release();
  1739. impl->swapChain_ = nullptr;
  1740. }
  1741. IDXGIDevice* dxgiDevice = nullptr;
  1742. impl->device_->QueryInterface(IID_IDXGIDevice, (void**)&dxgiDevice);
  1743. IDXGIAdapter* dxgiAdapter = nullptr;
  1744. dxgiDevice->GetParent(IID_IDXGIAdapter, (void**)&dxgiAdapter);
  1745. IDXGIFactory* dxgiFactory = nullptr;
  1746. dxgiAdapter->GetParent(IID_IDXGIFactory, (void**)&dxgiFactory);
  1747. DXGI_RATIONAL refreshRateRational = {};
  1748. IDXGIOutput* dxgiOutput = nullptr;
  1749. UINT numModes = 0;
  1750. dxgiAdapter->EnumOutputs(screenParams_.monitor_, &dxgiOutput);
  1751. dxgiOutput->GetDisplayModeList(sRGB_ ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM, 0, &numModes, 0);
  1752. // find the best matching refresh rate with the specified resolution
  1753. if (numModes > 0)
  1754. {
  1755. DXGI_MODE_DESC* modes = new DXGI_MODE_DESC[numModes];
  1756. dxgiOutput->GetDisplayModeList(sRGB_ ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM, 0, &numModes, modes);
  1757. unsigned bestMatchingRateIndex = -1;
  1758. unsigned bestError = M_MAX_UNSIGNED;
  1759. for (unsigned i = 0; i < numModes; ++i)
  1760. {
  1761. if (width != modes[i].Width || height != modes[i].Height)
  1762. continue;
  1763. float rate = (float)modes[i].RefreshRate.Numerator / modes[i].RefreshRate.Denominator;
  1764. unsigned error = (unsigned)(Abs(rate - screenParams_.refreshRate_));
  1765. if (error < bestError)
  1766. {
  1767. bestMatchingRateIndex = i;
  1768. bestError = error;
  1769. }
  1770. }
  1771. if (bestMatchingRateIndex != -1)
  1772. {
  1773. refreshRateRational.Numerator = modes[bestMatchingRateIndex].RefreshRate.Numerator;
  1774. refreshRateRational.Denominator = modes[bestMatchingRateIndex].RefreshRate.Denominator;
  1775. }
  1776. delete[] modes;
  1777. }
  1778. dxgiOutput->Release();
  1779. DXGI_SWAP_CHAIN_DESC swapChainDesc;
  1780. memset(&swapChainDesc, 0, sizeof swapChainDesc);
  1781. swapChainDesc.BufferCount = 1;
  1782. swapChainDesc.BufferDesc.Width = (UINT)width;
  1783. swapChainDesc.BufferDesc.Height = (UINT)height;
  1784. swapChainDesc.BufferDesc.Format = sRGB_ ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;
  1785. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  1786. swapChainDesc.BufferDesc.RefreshRate.Numerator = refreshRateRational.Numerator;
  1787. swapChainDesc.BufferDesc.RefreshRate.Denominator = refreshRateRational.Denominator;
  1788. swapChainDesc.OutputWindow = GetWindowHandle(window_);
  1789. swapChainDesc.SampleDesc.Count = static_cast<UINT>(screenParams_.multiSample_);
  1790. swapChainDesc.SampleDesc.Quality = impl->GetMultiSampleQuality(swapChainDesc.BufferDesc.Format, screenParams_.multiSample_);
  1791. swapChainDesc.Windowed = TRUE;
  1792. swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  1793. HRESULT hr = dxgiFactory->CreateSwapChain(impl->device_, &swapChainDesc, &impl->swapChain_);
  1794. // After creating the swap chain, disable automatic Alt-Enter fullscreen/windowed switching
  1795. // (the application will switch manually if it wants to)
  1796. dxgiFactory->MakeWindowAssociation(GetWindowHandle(window_), DXGI_MWA_NO_ALT_ENTER);
  1797. #ifdef URHO3D_LOGGING
  1798. DXGI_ADAPTER_DESC desc;
  1799. dxgiAdapter->GetDesc(&desc);
  1800. String adapterDesc(desc.Description);
  1801. URHO3D_LOGINFO("Adapter used " + adapterDesc);
  1802. #endif
  1803. dxgiFactory->Release();
  1804. dxgiAdapter->Release();
  1805. dxgiDevice->Release();
  1806. if (FAILED(hr))
  1807. {
  1808. URHO3D_SAFE_RELEASE(impl->swapChain_);
  1809. URHO3D_LOGD3DERROR("Failed to create D3D11 swap chain", hr);
  1810. return false;
  1811. }
  1812. return true;
  1813. }
  1814. bool Graphics::UpdateSwapChain_D3D11(int width, int height)
  1815. {
  1816. bool success = true;
  1817. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1818. ID3D11RenderTargetView* nullView = nullptr;
  1819. impl->deviceContext_->OMSetRenderTargets(1, &nullView, nullptr);
  1820. if (impl->defaultRenderTargetView_)
  1821. {
  1822. impl->defaultRenderTargetView_->Release();
  1823. impl->defaultRenderTargetView_ = nullptr;
  1824. }
  1825. if (impl->defaultDepthStencilView_)
  1826. {
  1827. impl->defaultDepthStencilView_->Release();
  1828. impl->defaultDepthStencilView_ = nullptr;
  1829. }
  1830. if (impl->defaultDepthTexture_)
  1831. {
  1832. impl->defaultDepthTexture_->Release();
  1833. impl->defaultDepthTexture_ = nullptr;
  1834. }
  1835. if (impl->resolveTexture_)
  1836. {
  1837. impl->resolveTexture_->Release();
  1838. impl->resolveTexture_ = nullptr;
  1839. }
  1840. impl->depthStencilView_ = nullptr;
  1841. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1842. impl->renderTargetViews_[i] = nullptr;
  1843. impl->renderTargetsDirty_ = true;
  1844. impl->swapChain_->ResizeBuffers(1, (UINT)width, (UINT)height, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  1845. // Create default rendertarget view representing the backbuffer
  1846. ID3D11Texture2D* backbufferTexture;
  1847. HRESULT hr = impl->swapChain_->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backbufferTexture);
  1848. if (FAILED(hr))
  1849. {
  1850. URHO3D_SAFE_RELEASE(backbufferTexture);
  1851. URHO3D_LOGD3DERROR("Failed to get backbuffer texture", hr);
  1852. success = false;
  1853. }
  1854. else
  1855. {
  1856. hr = impl->device_->CreateRenderTargetView(backbufferTexture, nullptr, &impl->defaultRenderTargetView_);
  1857. backbufferTexture->Release();
  1858. if (FAILED(hr))
  1859. {
  1860. URHO3D_SAFE_RELEASE(impl->defaultRenderTargetView_);
  1861. URHO3D_LOGD3DERROR("Failed to create backbuffer rendertarget view", hr);
  1862. success = false;
  1863. }
  1864. }
  1865. // Create default depth-stencil texture and view
  1866. D3D11_TEXTURE2D_DESC depthDesc;
  1867. memset(&depthDesc, 0, sizeof depthDesc);
  1868. depthDesc.Width = (UINT)width;
  1869. depthDesc.Height = (UINT)height;
  1870. depthDesc.MipLevels = 1;
  1871. depthDesc.ArraySize = 1;
  1872. depthDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  1873. depthDesc.SampleDesc.Count = static_cast<UINT>(screenParams_.multiSample_);
  1874. depthDesc.SampleDesc.Quality = impl->GetMultiSampleQuality(depthDesc.Format, screenParams_.multiSample_);
  1875. depthDesc.Usage = D3D11_USAGE_DEFAULT;
  1876. depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  1877. depthDesc.CPUAccessFlags = 0;
  1878. depthDesc.MiscFlags = 0;
  1879. hr = impl->device_->CreateTexture2D(&depthDesc, nullptr, &impl->defaultDepthTexture_);
  1880. if (FAILED(hr))
  1881. {
  1882. URHO3D_SAFE_RELEASE(impl->defaultDepthTexture_);
  1883. URHO3D_LOGD3DERROR("Failed to create backbuffer depth-stencil texture", hr);
  1884. success = false;
  1885. }
  1886. else
  1887. {
  1888. hr = impl->device_->CreateDepthStencilView(impl->defaultDepthTexture_, nullptr, &impl->defaultDepthStencilView_);
  1889. if (FAILED(hr))
  1890. {
  1891. URHO3D_SAFE_RELEASE(impl->defaultDepthStencilView_);
  1892. URHO3D_LOGD3DERROR("Failed to create backbuffer depth-stencil view", hr);
  1893. success = false;
  1894. }
  1895. }
  1896. // Update internally held backbuffer size
  1897. width_ = width;
  1898. height_ = height;
  1899. ResetRenderTargets_D3D11();
  1900. return success;
  1901. }
  1902. void Graphics::CheckFeatureSupport_D3D11()
  1903. {
  1904. anisotropySupport_ = true;
  1905. dxtTextureSupport_ = true;
  1906. lightPrepassSupport_ = true;
  1907. deferredSupport_ = true;
  1908. hardwareShadowSupport_ = true;
  1909. instancingSupport_ = true;
  1910. shadowMapFormat_ = DXGI_FORMAT_R16_TYPELESS;
  1911. hiresShadowMapFormat_ = DXGI_FORMAT_R32_TYPELESS;
  1912. dummyColorFormat_ = DXGI_FORMAT_UNKNOWN;
  1913. sRGBSupport_ = true;
  1914. sRGBWriteSupport_ = true;
  1915. }
  1916. void Graphics::ResetCachedState_D3D11()
  1917. {
  1918. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1919. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1920. {
  1921. vertexBuffers_[i] = nullptr;
  1922. impl->vertexBuffers_[i] = nullptr;
  1923. impl->vertexSizes_[i] = 0;
  1924. impl->vertexOffsets_[i] = 0;
  1925. }
  1926. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1927. {
  1928. textures_[i] = nullptr;
  1929. impl->shaderResourceViews_[i] = nullptr;
  1930. impl->samplers_[i] = nullptr;
  1931. }
  1932. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1933. {
  1934. renderTargets_[i] = nullptr;
  1935. impl->renderTargetViews_[i] = nullptr;
  1936. }
  1937. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  1938. {
  1939. impl->constantBuffers_[VS][i] = nullptr;
  1940. impl->constantBuffers_[PS][i] = nullptr;
  1941. }
  1942. depthStencil_ = nullptr;
  1943. impl->depthStencilView_ = nullptr;
  1944. viewport_ = IntRect(0, 0, width_, height_);
  1945. indexBuffer_ = nullptr;
  1946. vertexDeclarationHash_ = 0;
  1947. primitiveType_ = 0;
  1948. vertexShader_ = nullptr;
  1949. pixelShader_ = nullptr;
  1950. blendMode_ = BLEND_REPLACE;
  1951. alphaToCoverage_ = false;
  1952. colorWrite_ = true;
  1953. cullMode_ = CULL_CCW;
  1954. constantDepthBias_ = 0.0f;
  1955. slopeScaledDepthBias_ = 0.0f;
  1956. depthTestMode_ = CMP_LESSEQUAL;
  1957. depthWrite_ = true;
  1958. fillMode_ = FILL_SOLID;
  1959. lineAntiAlias_ = false;
  1960. scissorTest_ = false;
  1961. scissorRect_ = IntRect::ZERO;
  1962. stencilTest_ = false;
  1963. stencilTestMode_ = CMP_ALWAYS;
  1964. stencilPass_ = OP_KEEP;
  1965. stencilFail_ = OP_KEEP;
  1966. stencilZFail_ = OP_KEEP;
  1967. stencilRef_ = 0;
  1968. stencilCompareMask_ = M_U32_MASK_ALL_BITS;
  1969. stencilWriteMask_ = M_U32_MASK_ALL_BITS;
  1970. useClipPlane_ = false;
  1971. impl->shaderProgram_ = nullptr;
  1972. impl->renderTargetsDirty_ = true;
  1973. impl->texturesDirty_ = true;
  1974. impl->vertexDeclarationDirty_ = true;
  1975. impl->blendStateDirty_ = true;
  1976. impl->depthStateDirty_ = true;
  1977. impl->rasterizerStateDirty_ = true;
  1978. impl->scissorRectDirty_ = true;
  1979. impl->stencilRefDirty_ = true;
  1980. impl->blendStateHash_ = M_MAX_U32;
  1981. impl->depthStateHash_ = M_MAX_U32;
  1982. impl->rasterizerStateHash_ = M_MAX_U32;
  1983. impl->firstDirtyTexture_ = impl->lastDirtyTexture_ = M_MAX_UNSIGNED;
  1984. impl->firstDirtyVB_ = impl->lastDirtyVB_ = M_MAX_UNSIGNED;
  1985. impl->dirtyConstantBuffers_.Clear();
  1986. }
  1987. void Graphics::PrepareDraw_D3D11()
  1988. {
  1989. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  1990. if (impl->renderTargetsDirty_)
  1991. {
  1992. impl->depthStencilView_ =
  1993. (depthStencil_ && depthStencil_->GetUsage() == TEXTURE_DEPTHSTENCIL) ?
  1994. (ID3D11DepthStencilView*)depthStencil_->GetRenderTargetView() : impl->defaultDepthStencilView_;
  1995. // If possible, bind a read-only depth stencil view to allow reading depth in shader
  1996. if (!depthWrite_ && depthStencil_ && depthStencil_->GetReadOnlyView())
  1997. impl->depthStencilView_ = (ID3D11DepthStencilView*)depthStencil_->GetReadOnlyView();
  1998. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1999. impl->renderTargetViews_[i] =
  2000. (renderTargets_[i] && renderTargets_[i]->GetUsage() == TEXTURE_RENDERTARGET) ?
  2001. (ID3D11RenderTargetView*)renderTargets_[i]->GetRenderTargetView() : nullptr;
  2002. // If rendertarget 0 is null and not doing depth-only rendering, render to the backbuffer
  2003. // Special case: if rendertarget 0 is null and depth stencil has same size as backbuffer, assume the intention is to do
  2004. // backbuffer rendering with a custom depth stencil
  2005. if (!renderTargets_[0] &&
  2006. (!depthStencil_ || (depthStencil_ && depthStencil_->GetWidth() == width_ && depthStencil_->GetHeight() == height_)))
  2007. impl->renderTargetViews_[0] = impl->defaultRenderTargetView_;
  2008. impl->deviceContext_->OMSetRenderTargets(MAX_RENDERTARGETS, &impl->renderTargetViews_[0], impl->depthStencilView_);
  2009. impl->renderTargetsDirty_ = false;
  2010. }
  2011. if (impl->texturesDirty_ && impl->firstDirtyTexture_ < M_MAX_UNSIGNED)
  2012. {
  2013. // Set also VS textures to enable vertex texture fetch to work the same way as on OpenGL
  2014. impl->deviceContext_->VSSetShaderResources(impl->firstDirtyTexture_, impl->lastDirtyTexture_ - impl->firstDirtyTexture_ + 1,
  2015. &impl->shaderResourceViews_[impl->firstDirtyTexture_]);
  2016. impl->deviceContext_->VSSetSamplers(impl->firstDirtyTexture_, impl->lastDirtyTexture_ - impl->firstDirtyTexture_ + 1,
  2017. &impl->samplers_[impl->firstDirtyTexture_]);
  2018. impl->deviceContext_->PSSetShaderResources(impl->firstDirtyTexture_, impl->lastDirtyTexture_ - impl->firstDirtyTexture_ + 1,
  2019. &impl->shaderResourceViews_[impl->firstDirtyTexture_]);
  2020. impl->deviceContext_->PSSetSamplers(impl->firstDirtyTexture_, impl->lastDirtyTexture_ - impl->firstDirtyTexture_ + 1,
  2021. &impl->samplers_[impl->firstDirtyTexture_]);
  2022. impl->firstDirtyTexture_ = impl->lastDirtyTexture_ = M_MAX_UNSIGNED;
  2023. impl->texturesDirty_ = false;
  2024. }
  2025. if (impl->vertexDeclarationDirty_ && vertexShader_ && vertexShader_->GetByteCode().Size())
  2026. {
  2027. if (impl->firstDirtyVB_ < M_MAX_UNSIGNED)
  2028. {
  2029. impl->deviceContext_->IASetVertexBuffers(impl->firstDirtyVB_, impl->lastDirtyVB_ - impl->firstDirtyVB_ + 1,
  2030. &impl->vertexBuffers_[impl->firstDirtyVB_], &impl->vertexSizes_[impl->firstDirtyVB_], &impl->vertexOffsets_[impl->firstDirtyVB_]);
  2031. impl->firstDirtyVB_ = impl->lastDirtyVB_ = M_MAX_UNSIGNED;
  2032. }
  2033. hash64 newVertexDeclarationHash = 0;
  2034. for (i32 i = 0; i < MAX_VERTEX_STREAMS; ++i)
  2035. {
  2036. if (vertexBuffers_[i])
  2037. newVertexDeclarationHash |= vertexBuffers_[i]->GetBufferHash(i);
  2038. }
  2039. // Do not create input layout if no vertex buffers / elements
  2040. if (newVertexDeclarationHash)
  2041. {
  2042. /// \todo Using a 64bit total hash for vertex shader and vertex buffer elements hash may not guarantee uniqueness
  2043. newVertexDeclarationHash += vertexShader_->GetElementHash();
  2044. if (newVertexDeclarationHash != vertexDeclarationHash_)
  2045. {
  2046. VertexDeclarationMap_D3D11::Iterator i =
  2047. impl->vertexDeclarations_.Find(newVertexDeclarationHash);
  2048. if (i == impl->vertexDeclarations_.End())
  2049. {
  2050. SharedPtr<VertexDeclaration_D3D11> newVertexDeclaration(new VertexDeclaration_D3D11(this, vertexShader_, vertexBuffers_));
  2051. i = impl->vertexDeclarations_.Insert(MakePair(newVertexDeclarationHash, newVertexDeclaration));
  2052. }
  2053. impl->deviceContext_->IASetInputLayout((ID3D11InputLayout*)i->second_->GetInputLayout());
  2054. vertexDeclarationHash_ = newVertexDeclarationHash;
  2055. }
  2056. }
  2057. impl->vertexDeclarationDirty_ = false;
  2058. }
  2059. if (impl->blendStateDirty_)
  2060. {
  2061. hash32 newBlendStateHash = (hash32)((colorWrite_ ? 1 : 0) | (alphaToCoverage_ ? 2 : 0) | (blendMode_ << 2));
  2062. if (newBlendStateHash != impl->blendStateHash_)
  2063. {
  2064. HashMap<hash32, ID3D11BlendState*>::Iterator i = impl->blendStates_.Find(newBlendStateHash);
  2065. if (i == impl->blendStates_.End())
  2066. {
  2067. URHO3D_PROFILE(CreateBlendState);
  2068. D3D11_BLEND_DESC stateDesc;
  2069. memset(&stateDesc, 0, sizeof stateDesc);
  2070. stateDesc.AlphaToCoverageEnable = alphaToCoverage_ ? TRUE : FALSE;
  2071. stateDesc.IndependentBlendEnable = false;
  2072. stateDesc.RenderTarget[0].BlendEnable = d3dBlendEnable[blendMode_];
  2073. stateDesc.RenderTarget[0].SrcBlend = d3dSrcBlend[blendMode_];
  2074. stateDesc.RenderTarget[0].DestBlend = d3dDestBlend[blendMode_];
  2075. stateDesc.RenderTarget[0].BlendOp = d3dBlendOp[blendMode_];
  2076. stateDesc.RenderTarget[0].SrcBlendAlpha = d3dSrcBlend[blendMode_];
  2077. stateDesc.RenderTarget[0].DestBlendAlpha = d3dDestBlend[blendMode_];
  2078. stateDesc.RenderTarget[0].BlendOpAlpha = d3dBlendOp[blendMode_];
  2079. stateDesc.RenderTarget[0].RenderTargetWriteMask = colorWrite_ ? D3D11_COLOR_WRITE_ENABLE_ALL : 0x0;
  2080. ID3D11BlendState* newBlendState = nullptr;
  2081. HRESULT hr = impl->device_->CreateBlendState(&stateDesc, &newBlendState);
  2082. if (FAILED(hr))
  2083. {
  2084. URHO3D_SAFE_RELEASE(newBlendState);
  2085. URHO3D_LOGD3DERROR("Failed to create blend state", hr);
  2086. }
  2087. i = impl->blendStates_.Insert(MakePair(newBlendStateHash, newBlendState));
  2088. }
  2089. impl->deviceContext_->OMSetBlendState(i->second_, nullptr, M_MAX_UNSIGNED);
  2090. impl->blendStateHash_ = newBlendStateHash;
  2091. }
  2092. impl->blendStateDirty_ = false;
  2093. }
  2094. if (impl->depthStateDirty_)
  2095. {
  2096. hash32 newDepthStateHash =
  2097. (depthWrite_ ? 1 : 0) | (stencilTest_ ? 2 : 0) | (depthTestMode_ << 2) | ((stencilCompareMask_ & 0xff) << 5) |
  2098. ((stencilWriteMask_ & 0xff) << 13) | (stencilTestMode_ << 21) |
  2099. ((stencilFail_ + stencilZFail_ * 5 + stencilPass_ * 25) << 24);
  2100. if (newDepthStateHash != impl->depthStateHash_ || impl->stencilRefDirty_)
  2101. {
  2102. HashMap<hash32, ID3D11DepthStencilState*>::Iterator i = impl->depthStates_.Find(newDepthStateHash);
  2103. if (i == impl->depthStates_.End())
  2104. {
  2105. URHO3D_PROFILE(CreateDepthState);
  2106. D3D11_DEPTH_STENCIL_DESC stateDesc;
  2107. memset(&stateDesc, 0, sizeof stateDesc);
  2108. stateDesc.DepthEnable = TRUE;
  2109. stateDesc.DepthWriteMask = depthWrite_ ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
  2110. stateDesc.DepthFunc = d3dCmpFunc[depthTestMode_];
  2111. stateDesc.StencilEnable = stencilTest_ ? TRUE : FALSE;
  2112. stateDesc.StencilReadMask = (UINT8)stencilCompareMask_;
  2113. stateDesc.StencilWriteMask = (UINT8)stencilWriteMask_;
  2114. stateDesc.FrontFace.StencilFailOp = d3dStencilOp[stencilFail_];
  2115. stateDesc.FrontFace.StencilDepthFailOp = d3dStencilOp[stencilZFail_];
  2116. stateDesc.FrontFace.StencilPassOp = d3dStencilOp[stencilPass_];
  2117. stateDesc.FrontFace.StencilFunc = d3dCmpFunc[stencilTestMode_];
  2118. stateDesc.BackFace.StencilFailOp = d3dStencilOp[stencilFail_];
  2119. stateDesc.BackFace.StencilDepthFailOp = d3dStencilOp[stencilZFail_];
  2120. stateDesc.BackFace.StencilPassOp = d3dStencilOp[stencilPass_];
  2121. stateDesc.BackFace.StencilFunc = d3dCmpFunc[stencilTestMode_];
  2122. ID3D11DepthStencilState* newDepthState = nullptr;
  2123. HRESULT hr = impl->device_->CreateDepthStencilState(&stateDesc, &newDepthState);
  2124. if (FAILED(hr))
  2125. {
  2126. URHO3D_SAFE_RELEASE(newDepthState);
  2127. URHO3D_LOGD3DERROR("Failed to create depth state", hr);
  2128. }
  2129. i = impl->depthStates_.Insert(MakePair(newDepthStateHash, newDepthState));
  2130. }
  2131. impl->deviceContext_->OMSetDepthStencilState(i->second_, stencilRef_);
  2132. impl->depthStateHash_ = newDepthStateHash;
  2133. }
  2134. impl->depthStateDirty_ = false;
  2135. impl->stencilRefDirty_ = false;
  2136. }
  2137. if (impl->rasterizerStateDirty_)
  2138. {
  2139. unsigned depthBits = 24;
  2140. if (depthStencil_ && depthStencil_->GetParentTexture()->GetFormat() == DXGI_FORMAT_R16_TYPELESS)
  2141. depthBits = 16;
  2142. int scaledDepthBias = (int)(constantDepthBias_ * (1 << depthBits));
  2143. hash32 newRasterizerStateHash =
  2144. (scissorTest_ ? 1 : 0) | (lineAntiAlias_ ? 2 : 0) | (fillMode_ << 2) | (cullMode_ << 4) |
  2145. ((scaledDepthBias & 0x1fff) << 6) | (((int)(slopeScaledDepthBias_ * 100.0f) & 0x1fff) << 19);
  2146. if (newRasterizerStateHash != impl->rasterizerStateHash_)
  2147. {
  2148. HashMap<hash32, ID3D11RasterizerState*>::Iterator i = impl->rasterizerStates_.Find(newRasterizerStateHash);
  2149. if (i == impl->rasterizerStates_.End())
  2150. {
  2151. URHO3D_PROFILE(CreateRasterizerState);
  2152. D3D11_RASTERIZER_DESC stateDesc;
  2153. memset(&stateDesc, 0, sizeof stateDesc);
  2154. stateDesc.FillMode = d3dFillMode[fillMode_];
  2155. stateDesc.CullMode = d3dCullMode[cullMode_];
  2156. stateDesc.FrontCounterClockwise = FALSE;
  2157. stateDesc.DepthBias = scaledDepthBias;
  2158. stateDesc.DepthBiasClamp = M_INFINITY;
  2159. stateDesc.SlopeScaledDepthBias = slopeScaledDepthBias_;
  2160. stateDesc.DepthClipEnable = TRUE;
  2161. stateDesc.ScissorEnable = scissorTest_ ? TRUE : FALSE;
  2162. stateDesc.MultisampleEnable = lineAntiAlias_ ? FALSE : TRUE;
  2163. stateDesc.AntialiasedLineEnable = lineAntiAlias_ ? TRUE : FALSE;
  2164. ID3D11RasterizerState* newRasterizerState = nullptr;
  2165. HRESULT hr = impl->device_->CreateRasterizerState(&stateDesc, &newRasterizerState);
  2166. if (FAILED(hr))
  2167. {
  2168. URHO3D_SAFE_RELEASE(newRasterizerState);
  2169. URHO3D_LOGD3DERROR("Failed to create rasterizer state", hr);
  2170. }
  2171. i = impl->rasterizerStates_.Insert(MakePair(newRasterizerStateHash, newRasterizerState));
  2172. }
  2173. impl->deviceContext_->RSSetState(i->second_);
  2174. impl->rasterizerStateHash_ = newRasterizerStateHash;
  2175. }
  2176. impl->rasterizerStateDirty_ = false;
  2177. }
  2178. if (impl->scissorRectDirty_)
  2179. {
  2180. D3D11_RECT d3dRect;
  2181. d3dRect.left = scissorRect_.left_;
  2182. d3dRect.top = scissorRect_.top_;
  2183. d3dRect.right = scissorRect_.right_;
  2184. d3dRect.bottom = scissorRect_.bottom_;
  2185. impl->deviceContext_->RSSetScissorRects(1, &d3dRect);
  2186. impl->scissorRectDirty_ = false;
  2187. }
  2188. for (unsigned i = 0; i < impl->dirtyConstantBuffers_.Size(); ++i)
  2189. impl->dirtyConstantBuffers_[i]->Apply();
  2190. impl->dirtyConstantBuffers_.Clear();
  2191. }
  2192. void Graphics::CreateResolveTexture_D3D11()
  2193. {
  2194. GraphicsImpl_D3D11* impl = GetImpl_D3D11();
  2195. if (impl->resolveTexture_)
  2196. return;
  2197. D3D11_TEXTURE2D_DESC textureDesc;
  2198. memset(&textureDesc, 0, sizeof textureDesc);
  2199. textureDesc.Width = (UINT)width_;
  2200. textureDesc.Height = (UINT)height_;
  2201. textureDesc.MipLevels = 1;
  2202. textureDesc.ArraySize = 1;
  2203. textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  2204. textureDesc.SampleDesc.Count = 1;
  2205. textureDesc.SampleDesc.Quality = 0;
  2206. textureDesc.Usage = D3D11_USAGE_DEFAULT;
  2207. textureDesc.CPUAccessFlags = 0;
  2208. HRESULT hr = impl->device_->CreateTexture2D(&textureDesc, nullptr, &impl->resolveTexture_);
  2209. if (FAILED(hr))
  2210. {
  2211. URHO3D_SAFE_RELEASE(impl->resolveTexture_);
  2212. URHO3D_LOGD3DERROR("Could not create resolve texture", hr);
  2213. }
  2214. }
  2215. void Graphics::SetTextureUnitMappings_D3D11()
  2216. {
  2217. textureUnits_["DiffMap"] = TU_DIFFUSE;
  2218. textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
  2219. textureUnits_["NormalMap"] = TU_NORMAL;
  2220. textureUnits_["SpecMap"] = TU_SPECULAR;
  2221. textureUnits_["EmissiveMap"] = TU_EMISSIVE;
  2222. textureUnits_["EnvMap"] = TU_ENVIRONMENT;
  2223. textureUnits_["EnvCubeMap"] = TU_ENVIRONMENT;
  2224. textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
  2225. textureUnits_["LightSpotMap"] = TU_LIGHTSHAPE;
  2226. textureUnits_["LightCubeMap"] = TU_LIGHTSHAPE;
  2227. textureUnits_["ShadowMap"] = TU_SHADOWMAP;
  2228. textureUnits_["FaceSelectCubeMap"] = TU_FACESELECT;
  2229. textureUnits_["IndirectionCubeMap"] = TU_INDIRECTION;
  2230. textureUnits_["VolumeMap"] = TU_VOLUMEMAP;
  2231. textureUnits_["ZoneCubeMap"] = TU_ZONE;
  2232. textureUnits_["ZoneVolumeMap"] = TU_ZONE;
  2233. }
  2234. }