OGLGraphics.cpp 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "AnimatedModel.h"
  24. #include "Animation.h"
  25. #include "AnimationController.h"
  26. #include "BillboardSet.h"
  27. #include "Camera.h"
  28. #include "Context.h"
  29. #include "CustomGeometry.h"
  30. #include "DebugRenderer.h"
  31. #include "DecalSet.h"
  32. #include "File.h"
  33. #include "Graphics.h"
  34. #include "GraphicsEvents.h"
  35. #include "GraphicsImpl.h"
  36. #include "IndexBuffer.h"
  37. #include "Log.h"
  38. #include "Material.h"
  39. #include "Mutex.h"
  40. #include "Octree.h"
  41. #include "ParticleEffect.h"
  42. #include "ParticleEmitter.h"
  43. #include "ProcessUtils.h"
  44. #include "Profiler.h"
  45. #include "RenderSurface.h"
  46. #include "ResourceCache.h"
  47. #include "Shader.h"
  48. #include "ShaderPrecache.h"
  49. #include "ShaderProgram.h"
  50. #include "ShaderVariation.h"
  51. #include "Skybox.h"
  52. #include "StaticModelGroup.h"
  53. #include "Technique.h"
  54. #include "Terrain.h"
  55. #include "TerrainPatch.h"
  56. #include "Texture2D.h"
  57. #include "Texture3D.h"
  58. #include "TextureCube.h"
  59. #include "VertexBuffer.h"
  60. #include "Zone.h"
  61. #include <stdio.h>
  62. #include "DebugNew.h"
  63. #ifdef GL_ES_VERSION_2_0
  64. #define GL_DEPTH_COMPONENT24 GL_DEPTH_COMPONENT24_OES
  65. #define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER
  66. #define GL_RENDERBUFFER_EXT GL_RENDERBUFFER
  67. #define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0
  68. #define GL_DEPTH_ATTACHMENT_EXT GL_DEPTH_ATTACHMENT
  69. #define GL_STENCIL_ATTACHMENT_EXT GL_STENCIL_ATTACHMENT
  70. #define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE
  71. #define glClearDepth glClearDepthf
  72. #define glBindFramebufferEXT glBindFramebuffer
  73. #define glFramebufferTexture2DEXT glFramebufferTexture2D
  74. #define glFramebufferRenderbufferEXT glFramebufferRenderbuffer
  75. #define glGenFramebuffersEXT glGenFramebuffers
  76. #define glDeleteFramebuffersEXT glDeleteFramebuffers
  77. #define glCheckFramebufferStatusEXT glCheckFramebufferStatus
  78. #endif
  79. #ifdef WIN32
  80. // On Intel / NVIDIA setups prefer the NVIDIA GPU
  81. #include <windows.h>
  82. extern "C" {
  83. __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
  84. }
  85. #endif
  86. namespace Urho3D
  87. {
  88. static const unsigned glCmpFunc[] =
  89. {
  90. GL_ALWAYS,
  91. GL_EQUAL,
  92. GL_NOTEQUAL,
  93. GL_LESS,
  94. GL_LEQUAL,
  95. GL_GREATER,
  96. GL_GEQUAL
  97. };
  98. static const unsigned glSrcBlend[] =
  99. {
  100. GL_ONE,
  101. GL_ONE,
  102. GL_DST_COLOR,
  103. GL_SRC_ALPHA,
  104. GL_SRC_ALPHA,
  105. GL_ONE,
  106. GL_ONE_MINUS_DST_ALPHA,
  107. GL_ONE,
  108. GL_SRC_ALPHA
  109. };
  110. static const unsigned glDestBlend[] =
  111. {
  112. GL_ZERO,
  113. GL_ONE,
  114. GL_ZERO,
  115. GL_ONE_MINUS_SRC_ALPHA,
  116. GL_ONE,
  117. GL_ONE_MINUS_SRC_ALPHA,
  118. GL_DST_ALPHA,
  119. GL_ONE,
  120. GL_ONE
  121. };
  122. static const unsigned glBlendOp[] =
  123. {
  124. GL_FUNC_ADD,
  125. GL_FUNC_ADD,
  126. GL_FUNC_ADD,
  127. GL_FUNC_ADD,
  128. GL_FUNC_ADD,
  129. GL_FUNC_ADD,
  130. GL_FUNC_ADD,
  131. GL_FUNC_REVERSE_SUBTRACT,
  132. GL_FUNC_REVERSE_SUBTRACT
  133. };
  134. #ifndef GL_ES_VERSION_2_0
  135. static const unsigned glFillMode[] =
  136. {
  137. GL_FILL,
  138. GL_LINE,
  139. GL_POINT
  140. };
  141. #endif
  142. static const unsigned glStencilOps[] =
  143. {
  144. GL_KEEP,
  145. GL_ZERO,
  146. GL_REPLACE,
  147. GL_INCR_WRAP,
  148. GL_DECR_WRAP
  149. };
  150. // Remap vertex attributes on OpenGL so that all usually needed attributes including skinning fit to the first 8.
  151. // This avoids a skinning bug on GLES2 devices which only support 8.
  152. static const unsigned glVertexAttrIndex[] =
  153. {
  154. 0, 1, 2, 3, 4, 8, 9, 5, 6, 7, 10, 11, 12
  155. };
  156. static const unsigned MAX_FRAMEBUFFER_AGE = 2000;
  157. #ifdef GL_ES_VERSION_2_0
  158. static unsigned glesDepthStencilFormat = GL_DEPTH_COMPONENT16;
  159. #endif
  160. bool CheckExtension(String& extensions, const String& name)
  161. {
  162. if (extensions.Empty())
  163. extensions = (const char*)glGetString(GL_EXTENSIONS);
  164. return extensions.Contains(name);
  165. }
  166. static void GetGLPrimitiveType(unsigned elementCount, PrimitiveType type, unsigned& primitiveCount, GLenum& glPrimitiveType)
  167. {
  168. switch (type)
  169. {
  170. case TRIANGLE_LIST:
  171. primitiveCount = elementCount / 3;
  172. glPrimitiveType = GL_TRIANGLES;
  173. break;
  174. case LINE_LIST:
  175. primitiveCount = elementCount / 2;
  176. glPrimitiveType = GL_LINES;
  177. break;
  178. case POINT_LIST:
  179. primitiveCount = elementCount;
  180. glPrimitiveType = GL_POINTS;
  181. break;
  182. case TRIANGLE_STRIP:
  183. primitiveCount = elementCount - 2;
  184. glPrimitiveType = GL_TRIANGLE_STRIP;
  185. break;
  186. case LINE_STRIP:
  187. primitiveCount = elementCount - 1;
  188. glPrimitiveType = GL_LINE_STRIP;
  189. break;
  190. case TRIANGLE_FAN:
  191. primitiveCount = elementCount - 2;
  192. glPrimitiveType = GL_TRIANGLE_FAN;
  193. break;
  194. }
  195. }
  196. Graphics::Graphics(Context* context_) :
  197. Object(context_),
  198. impl_(new GraphicsImpl()),
  199. windowIcon_(0),
  200. externalWindow_(0),
  201. width_(0),
  202. height_(0),
  203. position_(SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED),
  204. multiSample_(1),
  205. fullscreen_(false),
  206. borderless_(false),
  207. resizable_(false),
  208. vsync_(false),
  209. tripleBuffer_(false),
  210. sRGB_(false),
  211. instancingSupport_(false),
  212. lightPrepassSupport_(false),
  213. deferredSupport_(false),
  214. anisotropySupport_(false),
  215. dxtTextureSupport_(false),
  216. etcTextureSupport_(false),
  217. pvrtcTextureSupport_(false),
  218. sRGBSupport_(false),
  219. sRGBWriteSupport_(false),
  220. numPrimitives_(0),
  221. numBatches_(0),
  222. maxScratchBufferRequest_(0),
  223. dummyColorFormat_(0),
  224. shadowMapFormat_(GL_DEPTH_COMPONENT16),
  225. hiresShadowMapFormat_(GL_DEPTH_COMPONENT24),
  226. releasingGPUObjects_(false),
  227. defaultTextureFilterMode_(FILTER_TRILINEAR),
  228. shaderPath_("Shaders/GLSL/"),
  229. shaderExtension_(".glsl"),
  230. orientations_("LandscapeLeft LandscapeRight")
  231. {
  232. SetTextureUnitMappings();
  233. ResetCachedState();
  234. // Initialize SDL now. Graphics should be the first SDL-using subsystem to be created
  235. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_NOPARACHUTE);
  236. // Register Graphics library object factories
  237. RegisterGraphicsLibrary(context_);
  238. }
  239. Graphics::~Graphics()
  240. {
  241. Close();
  242. delete impl_;
  243. impl_ = 0;
  244. // Shut down SDL now. Graphics should be the last SDL-using subsystem to be destroyed
  245. SDL_Quit();
  246. }
  247. void Graphics::SetExternalWindow(void* window)
  248. {
  249. if (!impl_->window_)
  250. externalWindow_ = window;
  251. else
  252. LOGERROR("Window already opened, can not set external window");
  253. }
  254. void Graphics::SetWindowTitle(const String& windowTitle)
  255. {
  256. windowTitle_ = windowTitle;
  257. if (impl_->window_)
  258. SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
  259. }
  260. void Graphics::SetWindowIcon(Image* windowIcon)
  261. {
  262. windowIcon_ = windowIcon;
  263. if (impl_->window_)
  264. CreateWindowIcon();
  265. }
  266. void Graphics::SetWindowPosition(const IntVector2& position)
  267. {
  268. if (impl_->window_)
  269. SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
  270. else
  271. position_ = position; // Sets as initial position for future window creation
  272. }
  273. void Graphics::SetWindowPosition(int x, int y)
  274. {
  275. SetWindowPosition(IntVector2(x, y));
  276. }
  277. bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool tripleBuffer, int multiSample)
  278. {
  279. PROFILE(SetScreenMode);
  280. bool maximize = false;
  281. // Fullscreen or Borderless can not be resizable
  282. if (fullscreen || borderless)
  283. resizable = false;
  284. // Borderless cannot be fullscreen, they are mutually exclusive
  285. if (borderless)
  286. fullscreen = false;
  287. multiSample = Clamp(multiSample, 1, 16);
  288. if (IsInitialized() && width == width_ && height == height_ && fullscreen == fullscreen_ && borderless == borderless_ && resizable == resizable_ &&
  289. vsync == vsync_ && tripleBuffer == tripleBuffer_ && multiSample == multiSample_)
  290. return true;
  291. // If only vsync changes, do not destroy/recreate the context
  292. if (IsInitialized() && width == width_ && height == height_ && fullscreen == fullscreen_ && borderless == borderless_ && resizable == resizable_ &&
  293. tripleBuffer == tripleBuffer_ && multiSample == multiSample_ && vsync != vsync_)
  294. {
  295. SDL_GL_SetSwapInterval(vsync ? 1 : 0);
  296. vsync_ = vsync;
  297. return true;
  298. }
  299. // If zero dimensions in windowed mode, set windowed mode to maximize and set a predefined default restored window size. If zero in fullscreen, use desktop mode
  300. if (!width || !height)
  301. {
  302. if (fullscreen || borderless)
  303. {
  304. SDL_DisplayMode mode;
  305. SDL_GetDesktopDisplayMode(0, &mode);
  306. width = mode.w;
  307. height = mode.h;
  308. }
  309. else
  310. {
  311. maximize = resizable;
  312. width = 1024;
  313. height = 768;
  314. }
  315. }
  316. // Check fullscreen mode validity (desktop only). Use a closest match if not found
  317. #if !defined(ANDROID) && !defined(IOS) && !defined(RPI)
  318. if (fullscreen)
  319. {
  320. PODVector<IntVector2> resolutions = GetResolutions();
  321. if (resolutions.Empty())
  322. fullscreen = false;
  323. else
  324. {
  325. unsigned best = 0;
  326. unsigned bestError = M_MAX_UNSIGNED;
  327. for (unsigned i = 0; i < resolutions.Size(); ++i)
  328. {
  329. unsigned error = Abs(resolutions[i].x_ - width) + Abs(resolutions[i].y_ - height);
  330. if (error < bestError)
  331. {
  332. best = i;
  333. bestError = error;
  334. }
  335. }
  336. width = resolutions[best].x_;
  337. height = resolutions[best].y_;
  338. }
  339. }
  340. #endif
  341. String extensions;
  342. // With an external window, only the size can change after initial setup, so do not recreate context
  343. if (!externalWindow_ || !impl_->context_)
  344. {
  345. // Close the existing window and OpenGL context, mark GPU objects as lost
  346. Release(false, true);
  347. #ifdef IOS
  348. // On iOS window needs to be resizable to handle orientation changes properly
  349. resizable = true;
  350. #endif
  351. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  352. #ifndef GL_ES_VERSION_2_0
  353. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  354. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  355. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  356. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  357. if (externalWindow_)
  358. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  359. else
  360. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  361. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  362. #endif
  363. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  364. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  365. if (multiSample > 1)
  366. {
  367. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  368. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multiSample);
  369. }
  370. else
  371. {
  372. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
  373. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
  374. }
  375. int x = fullscreen ? 0 : position_.x_;
  376. int y = fullscreen ? 0 : position_.y_;
  377. unsigned flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
  378. if (fullscreen)
  379. flags |= SDL_WINDOW_FULLSCREEN;
  380. if (resizable)
  381. flags |= SDL_WINDOW_RESIZABLE;
  382. if (borderless)
  383. flags |= SDL_WINDOW_BORDERLESS;
  384. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  385. for (;;)
  386. {
  387. if (!externalWindow_)
  388. impl_->window_ = SDL_CreateWindow(windowTitle_.CString(), x, y, width, height, flags);
  389. else
  390. {
  391. if (!impl_->window_)
  392. impl_->window_ = SDL_CreateWindowFrom(externalWindow_, SDL_WINDOW_OPENGL);
  393. fullscreen = false;
  394. }
  395. if (impl_->window_)
  396. break;
  397. else
  398. {
  399. if (multiSample > 1)
  400. {
  401. // If failed with multisampling, retry first without
  402. multiSample = 1;
  403. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
  404. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
  405. }
  406. else
  407. {
  408. LOGERROR("Could not open window");
  409. return false;
  410. }
  411. }
  412. }
  413. CreateWindowIcon();
  414. if (maximize)
  415. {
  416. Maximize();
  417. SDL_GetWindowSize(impl_->window_, &width, &height);
  418. }
  419. // Create/restore context and GPU objects and set initial renderstate
  420. Restore();
  421. if (!impl_->context_)
  422. {
  423. LOGERROR("Could not create OpenGL context");
  424. return false;
  425. }
  426. // If OpenGL extensions not yet initialized, initialize now
  427. #ifndef GL_ES_VERSION_2_0
  428. GLenum err = glewInit();
  429. if (GLEW_OK != err)
  430. {
  431. LOGERROR("Cannot initialize OpenGL");
  432. Release(true, true);
  433. return false;
  434. }
  435. if (!GLEW_VERSION_2_0)
  436. {
  437. LOGERROR("OpenGL 2.0 is required");
  438. Release(true, true);
  439. return false;
  440. }
  441. if (!GLEW_EXT_framebuffer_object || !GLEW_EXT_packed_depth_stencil)
  442. {
  443. LOGERROR("EXT_framebuffer_object and EXT_packed_depth_stencil OpenGL extensions are required");
  444. Release(true, true);
  445. return false;
  446. }
  447. instancingSupport_ = GLEW_ARB_instanced_arrays != 0;
  448. dxtTextureSupport_ = GLEW_EXT_texture_compression_s3tc != 0;
  449. anisotropySupport_ = GLEW_EXT_texture_filter_anisotropic != 0;
  450. sRGBSupport_ = GLEW_EXT_texture_sRGB != 0;
  451. sRGBWriteSupport_ = GLEW_EXT_framebuffer_sRGB != 0;
  452. // Set up instancing divisors if supported
  453. if (instancingSupport_)
  454. {
  455. glVertexAttribDivisorARB(ELEMENT_INSTANCEMATRIX1, 1);
  456. glVertexAttribDivisorARB(ELEMENT_INSTANCEMATRIX2, 1);
  457. glVertexAttribDivisorARB(ELEMENT_INSTANCEMATRIX3, 1);
  458. }
  459. #else
  460. dxtTextureSupport_ = CheckExtension(extensions, "EXT_texture_compression_dxt1");
  461. etcTextureSupport_ = CheckExtension(extensions, "OES_compressed_ETC1_RGB8_texture");
  462. pvrtcTextureSupport_ = CheckExtension(extensions, "IMG_texture_compression_pvrtc");
  463. #endif
  464. }
  465. // Set vsync
  466. SDL_GL_SetSwapInterval(vsync ? 1 : 0);
  467. // Store the system FBO on IOS now
  468. #ifdef IOS
  469. glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&impl_->systemFbo_);
  470. #endif
  471. fullscreen_ = fullscreen;
  472. resizable_ = resizable;
  473. borderless_ = borderless;
  474. vsync_ = vsync;
  475. tripleBuffer_ = tripleBuffer;
  476. multiSample_ = multiSample;
  477. SDL_GetWindowSize(impl_->window_, &width_, &height_);
  478. SDL_GetWindowPosition(impl_->window_, &position_.x_, &position_.y_);
  479. // Reset rendertargets and viewport for the new screen mode
  480. ResetRenderTargets();
  481. // Clear the initial window contents to black
  482. Clear(CLEAR_COLOR);
  483. SDL_GL_SwapWindow(impl_->window_);
  484. CheckFeatureSupport(extensions);
  485. #ifdef URHO3D_LOGGING
  486. String msg;
  487. msg.AppendWithFormat("Set screen mode %dx%d %s", width_, height_, (fullscreen_ ? "fullscreen" : "windowed"));
  488. if (borderless_)
  489. msg.Append(" borderless");
  490. if (resizable_)
  491. msg.Append(" resizable");
  492. if (multiSample > 1)
  493. msg.AppendWithFormat(" multisample %d", multiSample);
  494. LOGINFO(msg);
  495. #endif
  496. using namespace ScreenMode;
  497. VariantMap& eventData = GetEventDataMap();
  498. eventData[P_WIDTH] = width_;
  499. eventData[P_HEIGHT] = height_;
  500. eventData[P_FULLSCREEN] = fullscreen_;
  501. eventData[P_RESIZABLE] = resizable_;
  502. eventData[P_BORDERLESS] = borderless_;
  503. SendEvent(E_SCREENMODE, eventData);
  504. return true;
  505. }
  506. bool Graphics::SetMode(int width, int height)
  507. {
  508. return SetMode(width, height, fullscreen_, borderless_, resizable_, vsync_, tripleBuffer_, multiSample_);
  509. }
  510. void Graphics::SetSRGB(bool enable)
  511. {
  512. enable &= sRGBWriteSupport_;
  513. if (enable != sRGB_)
  514. {
  515. sRGB_ = enable;
  516. impl_->fboDirty_ = true;
  517. }
  518. }
  519. void Graphics::SetFlushGPU(bool enable)
  520. {
  521. }
  522. void Graphics::SetOrientations(const String& orientations)
  523. {
  524. orientations_ = orientations.Trimmed();
  525. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  526. }
  527. bool Graphics::ToggleFullscreen()
  528. {
  529. return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, vsync_, tripleBuffer_, multiSample_);
  530. }
  531. void Graphics::Close()
  532. {
  533. if (!IsInitialized())
  534. return;
  535. // Actually close the window
  536. Release(true, true);
  537. }
  538. bool Graphics::TakeScreenShot(Image& destImage)
  539. {
  540. PROFILE(TakeScreenShot);
  541. ResetRenderTargets();
  542. destImage.SetSize(width_, height_, 3);
  543. glReadPixels(0, 0, width_, height_, GL_RGB, GL_UNSIGNED_BYTE, destImage.GetData());
  544. // On OpenGL we need to flip the image vertically after reading
  545. destImage.FlipVertical();
  546. return true;
  547. }
  548. bool Graphics::BeginFrame()
  549. {
  550. if (!IsInitialized() || IsDeviceLost())
  551. return false;
  552. // If using an external window, check it for size changes, and reset screen mode if necessary
  553. if (externalWindow_)
  554. {
  555. int width, height;
  556. SDL_GetWindowSize(impl_->window_, &width, &height);
  557. if (width != width_ || height != height_)
  558. SetMode(width, height);
  559. }
  560. // Set default rendertarget and depth buffer
  561. ResetRenderTargets();
  562. // Cleanup textures from previous frame
  563. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  564. SetTexture(i, 0);
  565. // Enable color and depth write
  566. SetColorWrite(true);
  567. SetDepthWrite(true);
  568. numPrimitives_ = 0;
  569. numBatches_ = 0;
  570. SendEvent(E_BEGINRENDERING);
  571. return true;
  572. }
  573. void Graphics::EndFrame()
  574. {
  575. if (!IsInitialized())
  576. return;
  577. PROFILE(Present);
  578. SendEvent(E_ENDRENDERING);
  579. SDL_GL_SwapWindow(impl_->window_);
  580. // Clean up FBO's that have not been used for a long time, and too large scratch buffers
  581. CleanupFramebuffers();
  582. CleanupScratchBuffers();
  583. }
  584. void Graphics::Clear(unsigned flags, const Color& color, float depth, unsigned stencil)
  585. {
  586. if (impl_->fboDirty_)
  587. CommitFramebuffer();
  588. #ifdef GL_ES_VERSION_2_0
  589. flags &= ~CLEAR_STENCIL;
  590. #endif
  591. bool oldColorWrite = colorWrite_;
  592. bool oldDepthWrite = depthWrite_;
  593. if (flags & CLEAR_COLOR && !oldColorWrite)
  594. SetColorWrite(true);
  595. if (flags & CLEAR_DEPTH && !oldDepthWrite)
  596. SetDepthWrite(true);
  597. if (flags & CLEAR_STENCIL && stencilWriteMask_ != M_MAX_UNSIGNED)
  598. glStencilMask(M_MAX_UNSIGNED);
  599. unsigned glFlags = 0;
  600. if (flags & CLEAR_COLOR)
  601. {
  602. glFlags |= GL_COLOR_BUFFER_BIT;
  603. glClearColor(color.r_, color.g_, color.b_, color.a_);
  604. }
  605. if (flags & CLEAR_DEPTH)
  606. {
  607. glFlags |= GL_DEPTH_BUFFER_BIT;
  608. glClearDepth(depth);
  609. }
  610. if (flags & CLEAR_STENCIL)
  611. {
  612. glFlags |= GL_STENCIL_BUFFER_BIT;
  613. glClearStencil(stencil);
  614. }
  615. // If viewport is less than full screen, set a scissor to limit the clear
  616. /// \todo Any user-set scissor test will be lost
  617. IntVector2 viewSize = GetRenderTargetDimensions();
  618. if (viewport_.left_ != 0 || viewport_.top_ != 0 || viewport_.right_ != viewSize.x_ || viewport_.bottom_ != viewSize.y_)
  619. SetScissorTest(true, IntRect(0, 0, viewport_.Width(), viewport_.Height()));
  620. else
  621. SetScissorTest(false);
  622. glClear(glFlags);
  623. SetScissorTest(false);
  624. SetColorWrite(oldColorWrite);
  625. SetDepthWrite(oldDepthWrite);
  626. if (flags & CLEAR_STENCIL && stencilWriteMask_ != M_MAX_UNSIGNED)
  627. glStencilMask(stencilWriteMask_);
  628. }
  629. bool Graphics::ResolveToTexture(Texture2D* destination, const IntRect& viewport)
  630. {
  631. if (!destination || !destination->GetRenderSurface())
  632. return false;
  633. PROFILE(ResolveToTexture);
  634. IntRect vpCopy = viewport;
  635. if (vpCopy.right_ <= vpCopy.left_)
  636. vpCopy.right_ = vpCopy.left_ + 1;
  637. if (vpCopy.bottom_ <= vpCopy.top_)
  638. vpCopy.bottom_ = vpCopy.top_ + 1;
  639. vpCopy.left_ = Clamp(vpCopy.left_, 0, width_);
  640. vpCopy.top_ = Clamp(vpCopy.top_, 0, height_);
  641. vpCopy.right_ = Clamp(vpCopy.right_, 0, width_);
  642. vpCopy.bottom_ = Clamp(vpCopy.bottom_, 0, height_);
  643. // Make sure the FBO is not in use
  644. ResetRenderTargets();
  645. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  646. SetTextureForUpdate(destination);
  647. glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vpCopy.left_, height_ - vpCopy.bottom_, vpCopy.Width(), vpCopy.Height());
  648. SetTexture(0, 0);
  649. return true;
  650. }
  651. void Graphics::Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount)
  652. {
  653. if (!vertexCount)
  654. return;
  655. if (impl_->fboDirty_)
  656. CommitFramebuffer();
  657. unsigned primitiveCount;
  658. GLenum glPrimitiveType;
  659. GetGLPrimitiveType(vertexCount, type, primitiveCount, glPrimitiveType);
  660. glDrawArrays(glPrimitiveType, vertexStart, vertexCount);
  661. numPrimitives_ += primitiveCount;
  662. ++numBatches_;
  663. }
  664. void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  665. {
  666. if (!indexCount || !indexBuffer_ || !indexBuffer_->GetGPUObject())
  667. return;
  668. if (impl_->fboDirty_)
  669. CommitFramebuffer();
  670. unsigned indexSize = indexBuffer_->GetIndexSize();
  671. unsigned primitiveCount;
  672. GLenum glPrimitiveType;
  673. GetGLPrimitiveType(indexCount, type, primitiveCount, glPrimitiveType);
  674. if (indexSize == sizeof(unsigned short))
  675. glDrawElements(glPrimitiveType, indexCount, GL_UNSIGNED_SHORT, reinterpret_cast<const GLvoid*>(indexStart * indexSize));
  676. else
  677. glDrawElements(glPrimitiveType, indexCount, GL_UNSIGNED_INT, reinterpret_cast<const GLvoid*>(indexStart * indexSize));
  678. numPrimitives_ += primitiveCount;
  679. ++numBatches_;
  680. }
  681. void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount)
  682. {
  683. #ifndef GL_ES_VERSION_2_0
  684. if (!indexCount || !indexBuffer_ || !indexBuffer_->GetGPUObject() || !instancingSupport_)
  685. return;
  686. if (impl_->fboDirty_)
  687. CommitFramebuffer();
  688. unsigned indexSize = indexBuffer_->GetIndexSize();
  689. unsigned primitiveCount;
  690. GLenum glPrimitiveType;
  691. GetGLPrimitiveType(indexCount, type, primitiveCount, glPrimitiveType);
  692. if (indexSize == sizeof(unsigned short))
  693. {
  694. glDrawElementsInstancedARB(glPrimitiveType, indexCount, GL_UNSIGNED_SHORT, reinterpret_cast<const GLvoid*>(indexStart * indexSize),
  695. instanceCount);
  696. }
  697. else
  698. {
  699. glDrawElementsInstancedARB(glPrimitiveType, indexCount, GL_UNSIGNED_INT, reinterpret_cast<const GLvoid*>(indexStart * indexSize),
  700. instanceCount);
  701. }
  702. numPrimitives_ += instanceCount * primitiveCount;
  703. ++numBatches_;
  704. #endif
  705. }
  706. void Graphics::SetVertexBuffer(VertexBuffer* buffer)
  707. {
  708. // Note: this is not multi-instance safe
  709. static PODVector<VertexBuffer*> vertexBuffers(1);
  710. static PODVector<unsigned> elementMasks(1);
  711. vertexBuffers[0] = buffer;
  712. elementMasks[0] = MASK_DEFAULT;
  713. SetVertexBuffers(vertexBuffers, elementMasks);
  714. }
  715. bool Graphics::SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks,
  716. unsigned instanceOffset)
  717. {
  718. if (buffers.Size() > MAX_VERTEX_STREAMS)
  719. {
  720. LOGERROR("Too many vertex buffers");
  721. return false;
  722. }
  723. if (buffers.Size() != elementMasks.Size())
  724. {
  725. LOGERROR("Amount of element masks and vertex buffers does not match");
  726. return false;
  727. }
  728. bool changed = false;
  729. unsigned newAttributes = 0;
  730. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  731. {
  732. VertexBuffer* buffer = 0;
  733. unsigned elementMask = 0;
  734. if (i < buffers.Size() && buffers[i])
  735. {
  736. buffer = buffers[i];
  737. if (elementMasks[i] == MASK_DEFAULT)
  738. elementMask = buffer->GetElementMask();
  739. else
  740. elementMask = buffer->GetElementMask() & elementMasks[i];
  741. }
  742. // If buffer and element mask have stayed the same, skip to the next buffer
  743. if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i] && instanceOffset == lastInstanceOffset_ && !changed)
  744. {
  745. newAttributes |= elementMask;
  746. continue;
  747. }
  748. vertexBuffers_[i] = buffer;
  749. elementMasks_[i] = elementMask;
  750. changed = true;
  751. // Beware buffers with missing OpenGL objects, as binding a zero buffer object means accessing CPU memory for vertex data,
  752. // in which case the pointer will be invalid and cause a crash
  753. if (!buffer || !buffer->GetGPUObject())
  754. continue;
  755. glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
  756. unsigned vertexSize = buffer->GetVertexSize();
  757. for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
  758. {
  759. unsigned attrIndex = glVertexAttrIndex[j];
  760. unsigned elementBit = 1 << j;
  761. if (elementMask & elementBit)
  762. {
  763. newAttributes |= elementBit;
  764. // Enable attribute if not enabled yet
  765. if ((impl_->enabledAttributes_ & elementBit) == 0)
  766. {
  767. glEnableVertexAttribArray(attrIndex);
  768. impl_->enabledAttributes_ |= elementBit;
  769. }
  770. // Set the attribute pointer. Add instance offset for the instance matrix pointers
  771. unsigned offset = j >= ELEMENT_INSTANCEMATRIX1 ? instanceOffset * vertexSize : 0;
  772. glVertexAttribPointer(attrIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
  773. VertexBuffer::elementNormalize[j], vertexSize, reinterpret_cast<const GLvoid*>(buffer->GetElementOffset((VertexElement)j)
  774. + offset));
  775. }
  776. }
  777. }
  778. if (!changed)
  779. return true;
  780. lastInstanceOffset_ = instanceOffset;
  781. // Now check which vertex attributes should be disabled
  782. unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
  783. unsigned disableIndex = 0;
  784. while (disableAttributes)
  785. {
  786. if (disableAttributes & 1)
  787. {
  788. glDisableVertexAttribArray(glVertexAttrIndex[disableIndex]);
  789. impl_->enabledAttributes_ &= ~(1 << disableIndex);
  790. }
  791. disableAttributes >>= 1;
  792. ++disableIndex;
  793. }
  794. return true;
  795. }
  796. bool Graphics::SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>&
  797. elementMasks, unsigned instanceOffset)
  798. {
  799. if (buffers.Size() > MAX_VERTEX_STREAMS)
  800. {
  801. LOGERROR("Too many vertex buffers");
  802. return false;
  803. }
  804. if (buffers.Size() != elementMasks.Size())
  805. {
  806. LOGERROR("Amount of element masks and vertex buffers does not match");
  807. return false;
  808. }
  809. bool changed = false;
  810. unsigned newAttributes = 0;
  811. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  812. {
  813. VertexBuffer* buffer = 0;
  814. unsigned elementMask = 0;
  815. if (i < buffers.Size() && buffers[i])
  816. {
  817. buffer = buffers[i];
  818. if (elementMasks[i] == MASK_DEFAULT)
  819. elementMask = buffer->GetElementMask();
  820. else
  821. elementMask = buffer->GetElementMask() & elementMasks[i];
  822. }
  823. // If buffer and element mask have stayed the same, skip to the next buffer
  824. if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i] && instanceOffset == lastInstanceOffset_ && !changed)
  825. {
  826. newAttributes |= elementMask;
  827. continue;
  828. }
  829. vertexBuffers_[i] = buffer;
  830. elementMasks_[i] = elementMask;
  831. changed = true;
  832. // Beware buffers with missing OpenGL objects, as binding a zero buffer object means accessing CPU memory for vertex data,
  833. // in which case the pointer will be invalid and cause a crash
  834. if (!buffer || !buffer->GetGPUObject())
  835. continue;
  836. glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
  837. unsigned vertexSize = buffer->GetVertexSize();
  838. for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
  839. {
  840. unsigned attrIndex = glVertexAttrIndex[j];
  841. unsigned elementBit = 1 << j;
  842. if (elementMask & elementBit)
  843. {
  844. newAttributes |= elementBit;
  845. // Enable attribute if not enabled yet
  846. if ((impl_->enabledAttributes_ & elementBit) == 0)
  847. {
  848. glEnableVertexAttribArray(attrIndex);
  849. impl_->enabledAttributes_ |= elementBit;
  850. }
  851. // Set the attribute pointer. Add instance offset for the instance matrix pointers
  852. unsigned offset = j >= ELEMENT_INSTANCEMATRIX1 ? instanceOffset * vertexSize : 0;
  853. glVertexAttribPointer(attrIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
  854. VertexBuffer::elementNormalize[j], vertexSize, reinterpret_cast<const GLvoid*>(buffer->GetElementOffset((VertexElement)j)
  855. + offset));
  856. }
  857. }
  858. }
  859. if (!changed)
  860. return true;
  861. lastInstanceOffset_ = instanceOffset;
  862. // Now check which vertex attributes should be disabled
  863. unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
  864. unsigned disableIndex = 0;
  865. while (disableAttributes)
  866. {
  867. if (disableAttributes & 1)
  868. {
  869. glDisableVertexAttribArray(glVertexAttrIndex[disableIndex]);
  870. impl_->enabledAttributes_ &= ~(1 << disableIndex);
  871. }
  872. disableAttributes >>= 1;
  873. ++disableIndex;
  874. }
  875. return true;
  876. }
  877. void Graphics::SetIndexBuffer(IndexBuffer* buffer)
  878. {
  879. if (indexBuffer_ == buffer)
  880. return;
  881. if (buffer)
  882. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetGPUObject());
  883. else
  884. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  885. indexBuffer_ = buffer;
  886. }
  887. void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
  888. {
  889. if (vs == vertexShader_ && ps == pixelShader_)
  890. return;
  891. ClearParameterSources();
  892. // Compile the shaders now if not yet compiled. If already attempted, do not retry
  893. if (vs && !vs->GetGPUObject())
  894. {
  895. if (vs->GetCompilerOutput().Empty())
  896. {
  897. PROFILE(CompileVertexShader);
  898. bool success = vs->Create();
  899. if (success)
  900. LOGDEBUG("Compiled vertex shader " + vs->GetFullName());
  901. else
  902. {
  903. LOGERROR("Failed to compile vertex shader " + vs->GetFullName() + ":\n" + vs->GetCompilerOutput());
  904. vs = 0;
  905. }
  906. }
  907. else
  908. vs = 0;
  909. }
  910. if (ps && !ps->GetGPUObject())
  911. {
  912. if (ps->GetCompilerOutput().Empty())
  913. {
  914. PROFILE(CompilePixelShader);
  915. bool success = ps->Create();
  916. if (success)
  917. LOGDEBUG("Compiled pixel shader " + ps->GetFullName());
  918. else
  919. {
  920. LOGERROR("Failed to compile pixel shader " + ps->GetFullName() + ":\n" + ps->GetCompilerOutput());
  921. ps = 0;
  922. }
  923. }
  924. else
  925. ps = 0;
  926. }
  927. if (!vs || !ps)
  928. {
  929. glUseProgram(0);
  930. vertexShader_ = 0;
  931. pixelShader_ = 0;
  932. shaderProgram_ = 0;
  933. }
  934. else
  935. {
  936. vertexShader_ = vs;
  937. pixelShader_ = ps;
  938. Pair<ShaderVariation*, ShaderVariation*> combination(vs, ps);
  939. ShaderProgramMap::Iterator i = shaderPrograms_.Find(combination);
  940. if (i != shaderPrograms_.End())
  941. {
  942. // Use the existing linked program
  943. if (i->second_->GetGPUObject())
  944. {
  945. glUseProgram(i->second_->GetGPUObject());
  946. shaderProgram_ = i->second_;
  947. }
  948. else
  949. {
  950. glUseProgram(0);
  951. shaderProgram_ = 0;
  952. }
  953. }
  954. else
  955. {
  956. // Link a new combination
  957. PROFILE(LinkShaders);
  958. SharedPtr<ShaderProgram> newProgram(new ShaderProgram(this, vs, ps));
  959. if (newProgram->Link())
  960. {
  961. LOGDEBUG("Linked vertex shader " + vs->GetFullName() + " and pixel shader " + ps->GetFullName());
  962. // Note: Link() calls glUseProgram() to set the texture sampler uniforms,
  963. // so it is not necessary to call it again
  964. shaderProgram_ = newProgram;
  965. }
  966. else
  967. {
  968. LOGERROR("Failed to link vertex shader " + vs->GetFullName() + " and pixel shader " + ps->GetFullName() + ":\n" +
  969. newProgram->GetLinkerOutput());
  970. glUseProgram(0);
  971. shaderProgram_ = 0;
  972. }
  973. shaderPrograms_[combination] = newProgram;
  974. }
  975. }
  976. // Store shader combination if shader dumping in progress
  977. if (shaderPrecache_)
  978. shaderPrecache_->StoreShaders(vertexShader_, pixelShader_);
  979. }
  980. void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
  981. {
  982. if (shaderProgram_)
  983. {
  984. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  985. if (info)
  986. {
  987. switch (info->type_)
  988. {
  989. case GL_FLOAT:
  990. glUniform1fv(info->location_, count, data);
  991. break;
  992. case GL_FLOAT_VEC2:
  993. glUniform2fv(info->location_, count / 2, data);
  994. break;
  995. case GL_FLOAT_VEC3:
  996. glUniform3fv(info->location_, count / 3, data);
  997. break;
  998. case GL_FLOAT_VEC4:
  999. glUniform4fv(info->location_, count / 4, data);
  1000. break;
  1001. case GL_FLOAT_MAT3:
  1002. count = Min((int)count, (int)NUM_TEMP_MATRICES * 9);
  1003. Matrix3::BulkTranspose(&tempMatrices3_[0].m00_, data, count / 9);
  1004. glUniformMatrix3fv(info->location_, count / 9, GL_FALSE, tempMatrices3_[0].Data());
  1005. break;
  1006. case GL_FLOAT_MAT4:
  1007. count = Min((int)count, (int)NUM_TEMP_MATRICES * 16);
  1008. Matrix4::BulkTranspose(&tempMatrices4_[0].m00_, data, count / 16);
  1009. glUniformMatrix4fv(info->location_, count / 16, GL_FALSE, tempMatrices4_[0].Data());
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. void Graphics::SetShaderParameter(StringHash param, float value)
  1016. {
  1017. if (shaderProgram_)
  1018. {
  1019. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1020. if (info)
  1021. glUniform1fv(info->location_, 1, &value);
  1022. }
  1023. }
  1024. void Graphics::SetShaderParameter(StringHash param, const Color& color)
  1025. {
  1026. SetShaderParameter(param, color.Data(), 4);
  1027. }
  1028. void Graphics::SetShaderParameter(StringHash param, const Vector2& vector)
  1029. {
  1030. if (shaderProgram_)
  1031. {
  1032. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1033. if (info)
  1034. {
  1035. // Check the uniform type to avoid mismatch
  1036. switch (info->type_)
  1037. {
  1038. case GL_FLOAT:
  1039. glUniform1fv(info->location_, 1, vector.Data());
  1040. break;
  1041. case GL_FLOAT_VEC2:
  1042. glUniform2fv(info->location_, 1, vector.Data());
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
  1049. {
  1050. if (shaderProgram_)
  1051. {
  1052. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1053. if (info)
  1054. glUniformMatrix3fv(info->location_, 1, GL_FALSE, matrix.Transpose().Data());
  1055. }
  1056. }
  1057. void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
  1058. {
  1059. if (shaderProgram_)
  1060. {
  1061. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1062. if (info)
  1063. {
  1064. // Check the uniform type to avoid mismatch
  1065. switch (info->type_)
  1066. {
  1067. case GL_FLOAT:
  1068. glUniform1fv(info->location_, 1, vector.Data());
  1069. break;
  1070. case GL_FLOAT_VEC2:
  1071. glUniform2fv(info->location_, 1, vector.Data());
  1072. break;
  1073. case GL_FLOAT_VEC3:
  1074. glUniform3fv(info->location_, 1, vector.Data());
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
  1081. {
  1082. if (shaderProgram_)
  1083. {
  1084. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1085. if (info)
  1086. glUniformMatrix4fv(info->location_, 1, GL_FALSE, matrix.Transpose().Data());
  1087. }
  1088. }
  1089. void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
  1090. {
  1091. if (shaderProgram_)
  1092. {
  1093. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1094. if (info)
  1095. {
  1096. // Check the uniform type to avoid mismatch
  1097. switch (info->type_)
  1098. {
  1099. case GL_FLOAT:
  1100. glUniform1fv(info->location_, 1, vector.Data());
  1101. break;
  1102. case GL_FLOAT_VEC2:
  1103. glUniform2fv(info->location_, 1, vector.Data());
  1104. break;
  1105. case GL_FLOAT_VEC3:
  1106. glUniform3fv(info->location_, 1, vector.Data());
  1107. break;
  1108. case GL_FLOAT_VEC4:
  1109. glUniform4fv(info->location_, 1, vector.Data());
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. }
  1115. void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
  1116. {
  1117. if (shaderProgram_)
  1118. {
  1119. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  1120. if (info)
  1121. {
  1122. float data[16];
  1123. data[0] = matrix.m00_;
  1124. data[1] = matrix.m10_;
  1125. data[2] = matrix.m20_;
  1126. data[3] = 0.0f;
  1127. data[4] = matrix.m01_;
  1128. data[5] = matrix.m11_;
  1129. data[6] = matrix.m21_;
  1130. data[7] = 0.0f;
  1131. data[8] = matrix.m02_;
  1132. data[9] = matrix.m12_;
  1133. data[10] = matrix.m22_;
  1134. data[11] = 0.0f;
  1135. data[12] = matrix.m03_;
  1136. data[13] = matrix.m13_;
  1137. data[14] = matrix.m23_;
  1138. data[15] = 1.0f;
  1139. glUniformMatrix4fv(info->location_, 1, GL_FALSE, data);
  1140. }
  1141. }
  1142. }
  1143. void Graphics::SetShaderParameter(StringHash param, const Variant& value)
  1144. {
  1145. switch (value.GetType())
  1146. {
  1147. case VAR_BOOL:
  1148. SetShaderParameter(param, value.GetBool());
  1149. break;
  1150. case VAR_FLOAT:
  1151. SetShaderParameter(param, value.GetFloat());
  1152. break;
  1153. case VAR_VECTOR2:
  1154. SetShaderParameter(param, value.GetVector2());
  1155. break;
  1156. case VAR_VECTOR3:
  1157. SetShaderParameter(param, value.GetVector3());
  1158. break;
  1159. case VAR_VECTOR4:
  1160. SetShaderParameter(param, value.GetVector4());
  1161. break;
  1162. case VAR_COLOR:
  1163. SetShaderParameter(param, value.GetColor());
  1164. break;
  1165. case VAR_MATRIX3:
  1166. SetShaderParameter(param, value.GetMatrix3());
  1167. break;
  1168. case VAR_MATRIX3X4:
  1169. SetShaderParameter(param, value.GetMatrix3x4());
  1170. break;
  1171. case VAR_MATRIX4:
  1172. SetShaderParameter(param, value.GetMatrix4());
  1173. break;
  1174. default:
  1175. // Unsupported parameter type, do nothing
  1176. break;
  1177. }
  1178. }
  1179. bool Graphics::NeedParameterUpdate(ShaderParameterGroup group, const void* source)
  1180. {
  1181. if ((unsigned)(size_t)shaderParameterSources_[group] == M_MAX_UNSIGNED || shaderParameterSources_[group] != source)
  1182. {
  1183. shaderParameterSources_[group] = source;
  1184. return true;
  1185. }
  1186. else
  1187. return false;
  1188. }
  1189. bool Graphics::HasShaderParameter(ShaderType type, StringHash param)
  1190. {
  1191. return shaderProgram_ && shaderProgram_->HasParameter(param);
  1192. }
  1193. bool Graphics::HasTextureUnit(TextureUnit unit)
  1194. {
  1195. return shaderProgram_ && shaderProgram_->HasTextureUnit(unit);
  1196. }
  1197. void Graphics::ClearParameterSource(ShaderParameterGroup group)
  1198. {
  1199. shaderParameterSources_[group] = (const void*)M_MAX_UNSIGNED;
  1200. }
  1201. void Graphics::ClearParameterSources()
  1202. {
  1203. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  1204. shaderParameterSources_[i] = (const void*)M_MAX_UNSIGNED;
  1205. }
  1206. void Graphics::ClearTransformSources()
  1207. {
  1208. shaderParameterSources_[SP_CAMERA] = (const void*)M_MAX_UNSIGNED;
  1209. shaderParameterSources_[SP_OBJECTTRANSFORM] = (const void*)M_MAX_UNSIGNED;
  1210. }
  1211. void Graphics::CleanupShaderPrograms()
  1212. {
  1213. // Ignore individual call from ShaderVariation instance when Graphics subsystem is in process of
  1214. // releasing all GPU objects or recreating GPU objects due device lost, because the Graphics subsystem
  1215. // will eventually erase all the shader programs afterward as part of the release process.
  1216. if (releasingGPUObjects_)
  1217. return;
  1218. for (ShaderProgramMap::Iterator i = shaderPrograms_.Begin(); i != shaderPrograms_.End();)
  1219. {
  1220. ShaderVariation* vs = i->second_->GetVertexShader();
  1221. ShaderVariation* ps = i->second_->GetPixelShader();
  1222. if (!vs || !ps || !vs->GetGPUObject() || !ps->GetGPUObject())
  1223. i = shaderPrograms_.Erase(i);
  1224. else
  1225. ++i;
  1226. }
  1227. }
  1228. void Graphics::SetTexture(unsigned index, Texture* texture)
  1229. {
  1230. if (index >= MAX_TEXTURE_UNITS)
  1231. return;
  1232. // Check if texture is currently bound as a rendertarget. In that case, use its backup texture, or blank if not defined
  1233. if (texture)
  1234. {
  1235. if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
  1236. texture = texture->GetBackupTexture();
  1237. }
  1238. if (textures_[index] != texture)
  1239. {
  1240. if (impl_->activeTexture_ != index)
  1241. {
  1242. glActiveTexture(GL_TEXTURE0 + index);
  1243. impl_->activeTexture_ = index;
  1244. }
  1245. if (texture)
  1246. {
  1247. unsigned glType = texture->GetTarget();
  1248. if (glType != textureTypes_[index])
  1249. {
  1250. if (textureTypes_[index])
  1251. glDisable(textureTypes_[index]);
  1252. glEnable(glType);
  1253. textureTypes_[index] = glType;
  1254. }
  1255. glBindTexture(glType, texture->GetGPUObject());
  1256. if (texture->GetParametersDirty())
  1257. texture->UpdateParameters();
  1258. }
  1259. else
  1260. {
  1261. if (textureTypes_[index])
  1262. glBindTexture(textureTypes_[index], 0);
  1263. }
  1264. textures_[index] = texture;
  1265. }
  1266. else
  1267. {
  1268. if (texture && texture->GetParametersDirty())
  1269. {
  1270. if (impl_->activeTexture_ != index)
  1271. {
  1272. glActiveTexture(GL_TEXTURE0 + index);
  1273. impl_->activeTexture_ = index;
  1274. }
  1275. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  1276. texture->UpdateParameters();
  1277. }
  1278. }
  1279. }
  1280. void Graphics::SetTextureForUpdate(Texture* texture)
  1281. {
  1282. if (impl_->activeTexture_ != 0)
  1283. {
  1284. glActiveTexture(GL_TEXTURE0);
  1285. impl_->activeTexture_ = 0;
  1286. }
  1287. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  1288. textures_[0] = texture;
  1289. }
  1290. void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
  1291. {
  1292. if (mode != defaultTextureFilterMode_)
  1293. {
  1294. defaultTextureFilterMode_ = mode;
  1295. SetTextureParametersDirty();
  1296. }
  1297. }
  1298. void Graphics::SetTextureAnisotropy(unsigned level)
  1299. {
  1300. if (level != textureAnisotropy_)
  1301. {
  1302. textureAnisotropy_ = level;
  1303. SetTextureParametersDirty();
  1304. }
  1305. }
  1306. void Graphics::SetTextureParametersDirty()
  1307. {
  1308. MutexLock lock(gpuObjectMutex_);
  1309. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1310. {
  1311. Texture* texture = dynamic_cast<Texture*>(*i);
  1312. if (texture)
  1313. texture->SetParametersDirty();
  1314. }
  1315. }
  1316. void Graphics::ResetRenderTargets()
  1317. {
  1318. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1319. SetRenderTarget(i, (RenderSurface*)0);
  1320. SetDepthStencil((RenderSurface*)0);
  1321. SetViewport(IntRect(0, 0, width_, height_));
  1322. }
  1323. void Graphics::ResetRenderTarget(unsigned index)
  1324. {
  1325. SetRenderTarget(index, (RenderSurface*)0);
  1326. }
  1327. void Graphics::ResetDepthStencil()
  1328. {
  1329. SetDepthStencil((RenderSurface*)0);
  1330. }
  1331. void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
  1332. {
  1333. if (index >= MAX_RENDERTARGETS)
  1334. return;
  1335. if (renderTarget != renderTargets_[index])
  1336. {
  1337. renderTargets_[index] = renderTarget;
  1338. // If the rendertarget is also bound as a texture, replace with backup texture or null
  1339. if (renderTarget)
  1340. {
  1341. Texture* parentTexture = renderTarget->GetParentTexture();
  1342. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1343. {
  1344. if (textures_[i] == parentTexture)
  1345. SetTexture(i, textures_[i]->GetBackupTexture());
  1346. }
  1347. }
  1348. impl_->fboDirty_ = true;
  1349. }
  1350. }
  1351. void Graphics::SetRenderTarget(unsigned index, Texture2D* texture)
  1352. {
  1353. RenderSurface* renderTarget = 0;
  1354. if (texture)
  1355. renderTarget = texture->GetRenderSurface();
  1356. SetRenderTarget(index, renderTarget);
  1357. }
  1358. void Graphics::SetDepthStencil(RenderSurface* depthStencil)
  1359. {
  1360. // If we are using a rendertarget texture, it is required in OpenGL to also have an own depth-stencil
  1361. // Create a new depth-stencil texture as necessary to be able to provide similar behaviour as Direct3D9
  1362. if (renderTargets_[0] && !depthStencil)
  1363. {
  1364. int width = renderTargets_[0]->GetWidth();
  1365. int height = renderTargets_[0]->GetHeight();
  1366. // Direct3D9 default depth-stencil can not be used when rendertarget is larger than the window.
  1367. // Check size similarly
  1368. if (width <= width_ && height <= height_)
  1369. {
  1370. int searchKey = (width << 16) | height;
  1371. HashMap<int, SharedPtr<Texture2D> >::Iterator i = depthTextures_.Find(searchKey);
  1372. if (i != depthTextures_.End())
  1373. depthStencil = i->second_->GetRenderSurface();
  1374. else
  1375. {
  1376. SharedPtr<Texture2D> newDepthTexture(new Texture2D(context_));
  1377. newDepthTexture->SetSize(width, height, GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  1378. depthTextures_[searchKey] = newDepthTexture;
  1379. depthStencil = newDepthTexture->GetRenderSurface();
  1380. }
  1381. }
  1382. }
  1383. if (depthStencil != depthStencil_)
  1384. {
  1385. depthStencil_ = depthStencil;
  1386. impl_->fboDirty_ = true;
  1387. }
  1388. }
  1389. void Graphics::SetDepthStencil(Texture2D* texture)
  1390. {
  1391. RenderSurface* depthStencil = 0;
  1392. if (texture)
  1393. depthStencil = texture->GetRenderSurface();
  1394. SetDepthStencil(depthStencil);
  1395. }
  1396. void Graphics::SetViewport(const IntRect& rect)
  1397. {
  1398. if (impl_->fboDirty_)
  1399. CommitFramebuffer();
  1400. IntVector2 rtSize = GetRenderTargetDimensions();
  1401. IntRect rectCopy = rect;
  1402. if (rectCopy.right_ <= rectCopy.left_)
  1403. rectCopy.right_ = rectCopy.left_ + 1;
  1404. if (rectCopy.bottom_ <= rectCopy.top_)
  1405. rectCopy.bottom_ = rectCopy.top_ + 1;
  1406. rectCopy.left_ = Clamp(rectCopy.left_, 0, rtSize.x_);
  1407. rectCopy.top_ = Clamp(rectCopy.top_, 0, rtSize.y_);
  1408. rectCopy.right_ = Clamp(rectCopy.right_, 0, rtSize.x_);
  1409. rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, rtSize.y_);
  1410. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1411. glViewport(rectCopy.left_, rtSize.y_ - rectCopy.bottom_, rectCopy.Width(), rectCopy.Height());
  1412. viewport_ = rectCopy;
  1413. // Disable scissor test, needs to be re-enabled by the user
  1414. SetScissorTest(false);
  1415. }
  1416. void Graphics::SetBlendMode(BlendMode mode)
  1417. {
  1418. if (mode != blendMode_)
  1419. {
  1420. if (mode == BLEND_REPLACE)
  1421. glDisable(GL_BLEND);
  1422. else
  1423. {
  1424. glEnable(GL_BLEND);
  1425. glBlendFunc(glSrcBlend[mode], glDestBlend[mode]);
  1426. glBlendEquation(glBlendOp[mode]);
  1427. }
  1428. blendMode_ = mode;
  1429. }
  1430. }
  1431. void Graphics::SetColorWrite(bool enable)
  1432. {
  1433. if (enable != colorWrite_)
  1434. {
  1435. if (enable)
  1436. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  1437. else
  1438. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  1439. colorWrite_ = enable;
  1440. }
  1441. }
  1442. void Graphics::SetCullMode(CullMode mode)
  1443. {
  1444. if (mode != cullMode_)
  1445. {
  1446. if (mode == CULL_NONE)
  1447. glDisable(GL_CULL_FACE);
  1448. else
  1449. {
  1450. // Use Direct3D convention, ie. clockwise vertices define a front face
  1451. glEnable(GL_CULL_FACE);
  1452. glCullFace(mode == CULL_CCW ? GL_FRONT : GL_BACK);
  1453. }
  1454. cullMode_ = mode;
  1455. }
  1456. }
  1457. void Graphics::SetDepthBias(float constantBias, float slopeScaledBias)
  1458. {
  1459. if (constantBias != constantDepthBias_ || slopeScaledBias != slopeScaledDepthBias_)
  1460. {
  1461. #ifndef GL_ES_VERSION_2_0
  1462. if (slopeScaledBias != 0.0f)
  1463. {
  1464. // OpenGL constant bias is unreliable and dependant on depth buffer bitdepth, apply in the projection matrix instead
  1465. float adjustedSlopeScaledBias = slopeScaledBias + 1.0f;
  1466. glEnable(GL_POLYGON_OFFSET_FILL);
  1467. glPolygonOffset(adjustedSlopeScaledBias, 0.0f);
  1468. }
  1469. else
  1470. glDisable(GL_POLYGON_OFFSET_FILL);
  1471. #endif
  1472. constantDepthBias_ = constantBias;
  1473. slopeScaledDepthBias_ = slopeScaledBias;
  1474. shaderParameterSources_[SP_CAMERA] = (const void*)M_MAX_UNSIGNED;
  1475. }
  1476. }
  1477. void Graphics::SetDepthTest(CompareMode mode)
  1478. {
  1479. if (mode != depthTestMode_)
  1480. {
  1481. glDepthFunc(glCmpFunc[mode]);
  1482. depthTestMode_ = mode;
  1483. }
  1484. }
  1485. void Graphics::SetDepthWrite(bool enable)
  1486. {
  1487. if (enable != depthWrite_)
  1488. {
  1489. glDepthMask(enable ? GL_TRUE : GL_FALSE);
  1490. depthWrite_ = enable;
  1491. }
  1492. }
  1493. void Graphics::SetDrawAntialiased(bool enable)
  1494. {
  1495. if (enable != drawAntialiased_)
  1496. {
  1497. #ifndef GL_ES_VERSION_2_0
  1498. if (enable)
  1499. glEnable(GL_MULTISAMPLE);
  1500. else
  1501. glDisable(GL_MULTISAMPLE);
  1502. #endif
  1503. drawAntialiased_ = enable;
  1504. }
  1505. }
  1506. void Graphics::SetFillMode(FillMode mode)
  1507. {
  1508. #ifndef GL_ES_VERSION_2_0
  1509. if (mode != fillMode_)
  1510. {
  1511. glPolygonMode(GL_FRONT_AND_BACK, glFillMode[mode]);
  1512. fillMode_ = mode;
  1513. }
  1514. #endif
  1515. }
  1516. void Graphics::SetScissorTest(bool enable, const Rect& rect, bool borderInclusive)
  1517. {
  1518. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1519. // Disable scissor in that case to reduce state changes
  1520. if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.x_ >= 1.0f && rect.max_.y_ >= 1.0f)
  1521. enable = false;
  1522. if (enable)
  1523. {
  1524. IntVector2 rtSize(GetRenderTargetDimensions());
  1525. IntVector2 viewSize(viewport_.Size());
  1526. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1527. IntRect intRect;
  1528. int expand = borderInclusive ? 1 : 0;
  1529. intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
  1530. intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
  1531. intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
  1532. intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
  1533. if (intRect.right_ == intRect.left_)
  1534. intRect.right_++;
  1535. if (intRect.bottom_ == intRect.top_)
  1536. intRect.bottom_++;
  1537. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1538. enable = false;
  1539. if (enable && scissorRect_ != intRect)
  1540. {
  1541. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1542. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.Width(), intRect.Height());
  1543. scissorRect_ = intRect;
  1544. }
  1545. }
  1546. else
  1547. scissorRect_ = IntRect::ZERO;
  1548. if (enable != scissorTest_)
  1549. {
  1550. if (enable)
  1551. glEnable(GL_SCISSOR_TEST);
  1552. else
  1553. glDisable(GL_SCISSOR_TEST);
  1554. scissorTest_ = enable;
  1555. }
  1556. }
  1557. void Graphics::SetScissorTest(bool enable, const IntRect& rect)
  1558. {
  1559. IntVector2 rtSize(GetRenderTargetDimensions());
  1560. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1561. if (enable)
  1562. {
  1563. IntRect intRect;
  1564. intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
  1565. intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
  1566. intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
  1567. intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
  1568. if (intRect.right_ == intRect.left_)
  1569. intRect.right_++;
  1570. if (intRect.bottom_ == intRect.top_)
  1571. intRect.bottom_++;
  1572. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1573. enable = false;
  1574. if (enable && scissorRect_ != intRect)
  1575. {
  1576. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1577. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.Width(), intRect.Height());
  1578. scissorRect_ = intRect;
  1579. }
  1580. }
  1581. else
  1582. scissorRect_ = IntRect::ZERO;
  1583. if (enable != scissorTest_)
  1584. {
  1585. if (enable)
  1586. glEnable(GL_SCISSOR_TEST);
  1587. else
  1588. glDisable(GL_SCISSOR_TEST);
  1589. scissorTest_ = enable;
  1590. }
  1591. }
  1592. void Graphics::SetClipPlane(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection)
  1593. {
  1594. #ifndef GL_ES_VERSION_2_0
  1595. if (enable != useClipPlane_)
  1596. {
  1597. if (enable)
  1598. glEnable(GL_CLIP_PLANE0);
  1599. else
  1600. glDisable(GL_CLIP_PLANE0);
  1601. useClipPlane_ = enable;
  1602. }
  1603. if (enable)
  1604. {
  1605. Matrix4 viewProj = projection * view;
  1606. Vector4 planeVec = clipPlane.Transformed(viewProj).ToVector4();
  1607. GLdouble planeData[4];
  1608. planeData[0] = planeVec.x_;
  1609. planeData[1] = planeVec.y_;
  1610. planeData[2] = planeVec.z_;
  1611. planeData[3] = planeVec.w_;
  1612. glClipPlane(GL_CLIP_PLANE0, &planeData[0]);
  1613. }
  1614. #endif
  1615. }
  1616. void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
  1617. {
  1618. }
  1619. void Graphics::ResetStreamFrequencies()
  1620. {
  1621. }
  1622. void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned compareMask, unsigned writeMask)
  1623. {
  1624. #ifndef GL_ES_VERSION_2_0
  1625. if (enable != stencilTest_)
  1626. {
  1627. if (enable)
  1628. glEnable(GL_STENCIL_TEST);
  1629. else
  1630. glDisable(GL_STENCIL_TEST);
  1631. stencilTest_ = enable;
  1632. }
  1633. if (enable)
  1634. {
  1635. if (mode != stencilTestMode_ || stencilRef != stencilRef_ || compareMask != stencilCompareMask_)
  1636. {
  1637. glStencilFunc(glCmpFunc[mode], stencilRef, compareMask);
  1638. stencilTestMode_ = mode;
  1639. stencilRef_ = stencilRef;
  1640. stencilCompareMask_ = compareMask;
  1641. }
  1642. if (writeMask != stencilWriteMask_)
  1643. {
  1644. glStencilMask(writeMask);
  1645. stencilWriteMask_ = writeMask;
  1646. }
  1647. if (pass != stencilPass_ || fail != stencilFail_ || zFail != stencilZFail_)
  1648. {
  1649. glStencilOp(glStencilOps[fail], glStencilOps[zFail], glStencilOps[pass]);
  1650. stencilPass_ = pass;
  1651. stencilFail_ = fail;
  1652. stencilZFail_ = zFail;
  1653. }
  1654. }
  1655. #endif
  1656. }
  1657. void Graphics::SetForceSM2(bool enable)
  1658. {
  1659. }
  1660. void Graphics::BeginDumpShaders(const String& fileName)
  1661. {
  1662. shaderPrecache_ = new ShaderPrecache(context_, fileName);
  1663. }
  1664. void Graphics::EndDumpShaders()
  1665. {
  1666. shaderPrecache_.Reset();
  1667. }
  1668. void Graphics::PrecacheShaders(Deserializer& source)
  1669. {
  1670. PROFILE(PrecacheShaders);
  1671. ShaderPrecache::LoadShaders(this, source);
  1672. }
  1673. bool Graphics::IsInitialized() const
  1674. {
  1675. return impl_->window_ != 0;
  1676. }
  1677. bool Graphics::IsDeviceLost() const
  1678. {
  1679. // On iOS treat window minimization as device loss, as it is forbidden to access OpenGL when minimized
  1680. #ifdef IOS
  1681. if (impl_->window_ && (SDL_GetWindowFlags(impl_->window_) & SDL_WINDOW_MINIMIZED) != 0)
  1682. return true;
  1683. #endif
  1684. return impl_->context_ == 0;
  1685. }
  1686. IntVector2 Graphics::GetWindowPosition() const
  1687. {
  1688. if (impl_->window_)
  1689. return position_;
  1690. return IntVector2::ZERO;
  1691. }
  1692. PODVector<IntVector2> Graphics::GetResolutions() const
  1693. {
  1694. PODVector<IntVector2> ret;
  1695. unsigned numModes = SDL_GetNumDisplayModes(0);
  1696. for (unsigned i = 0; i < numModes; ++i)
  1697. {
  1698. SDL_DisplayMode mode;
  1699. SDL_GetDisplayMode(0, i, &mode);
  1700. int width = mode.w;
  1701. int height = mode.h;
  1702. // Store mode if unique
  1703. bool unique = true;
  1704. for (unsigned j = 0; j < ret.Size(); ++j)
  1705. {
  1706. if (ret[j].x_ == width && ret[j].y_ == height)
  1707. {
  1708. unique = false;
  1709. break;
  1710. }
  1711. }
  1712. if (unique)
  1713. ret.Push(IntVector2(width, height));
  1714. }
  1715. return ret;
  1716. }
  1717. PODVector<int> Graphics::GetMultiSampleLevels() const
  1718. {
  1719. PODVector<int> ret;
  1720. // No multisampling always supported
  1721. ret.Push(1);
  1722. /// \todo Implement properly, if possible
  1723. return ret;
  1724. }
  1725. IntVector2 Graphics::GetDesktopResolution() const
  1726. {
  1727. #if !defined(ANDROID) && !defined(IOS)
  1728. SDL_DisplayMode mode;
  1729. SDL_GetDesktopDisplayMode(0, &mode);
  1730. return IntVector2(mode.w, mode.h);
  1731. #else
  1732. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  1733. return IntVector2(width_, height_);
  1734. #endif
  1735. }
  1736. unsigned Graphics::GetFormat(CompressedFormat format) const
  1737. {
  1738. switch (format)
  1739. {
  1740. case CF_RGBA:
  1741. return GL_RGBA;
  1742. case CF_DXT1:
  1743. return dxtTextureSupport_ ? GL_COMPRESSED_RGBA_S3TC_DXT1_EXT : 0;
  1744. #ifndef GL_ES_VERSION_2_0
  1745. case CF_DXT3:
  1746. return dxtTextureSupport_ ? GL_COMPRESSED_RGBA_S3TC_DXT3_EXT : 0;
  1747. case CF_DXT5:
  1748. return dxtTextureSupport_ ? GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : 0;
  1749. #else
  1750. case CF_ETC1:
  1751. return etcTextureSupport_ ? GL_ETC1_RGB8_OES : 0;
  1752. case CF_PVRTC_RGB_2BPP:
  1753. return pvrtcTextureSupport_ ? COMPRESSED_RGB_PVRTC_2BPPV1_IMG : 0;
  1754. case CF_PVRTC_RGB_4BPP:
  1755. return pvrtcTextureSupport_ ? COMPRESSED_RGB_PVRTC_4BPPV1_IMG : 0;
  1756. case CF_PVRTC_RGBA_2BPP:
  1757. return pvrtcTextureSupport_ ? COMPRESSED_RGBA_PVRTC_2BPPV1_IMG : 0;
  1758. case CF_PVRTC_RGBA_4BPP:
  1759. return pvrtcTextureSupport_ ? COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : 0;
  1760. #endif
  1761. default:
  1762. return 0;
  1763. }
  1764. }
  1765. ShaderVariation* Graphics::GetShader(ShaderType type, const String& name, const String& defines) const
  1766. {
  1767. return GetShader(type, name.CString(), defines.CString());
  1768. }
  1769. ShaderVariation* Graphics::GetShader(ShaderType type, const char* name, const char* defines) const
  1770. {
  1771. if (lastShaderName_ != name || !lastShader_)
  1772. {
  1773. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1774. String fullShaderName = shaderPath_ + name + shaderExtension_;
  1775. // Try to reduce repeated error log prints because of missing shaders
  1776. if (lastShaderName_ == name && !cache->Exists(fullShaderName))
  1777. return 0;
  1778. lastShader_ = cache->GetResource<Shader>(fullShaderName);
  1779. lastShaderName_ = name;
  1780. }
  1781. return lastShader_ ? lastShader_->GetVariation(type, defines) : (ShaderVariation*)0;
  1782. }
  1783. VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
  1784. {
  1785. return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
  1786. }
  1787. TextureUnit Graphics::GetTextureUnit(const String& name)
  1788. {
  1789. HashMap<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
  1790. if (i != textureUnits_.End())
  1791. return i->second_;
  1792. else
  1793. return MAX_TEXTURE_UNITS;
  1794. }
  1795. const String& Graphics::GetTextureUnitName(TextureUnit unit)
  1796. {
  1797. for (HashMap<String, TextureUnit>::Iterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
  1798. {
  1799. if (i->second_ == unit)
  1800. return i->first_;
  1801. }
  1802. return String::EMPTY;
  1803. }
  1804. Texture* Graphics::GetTexture(unsigned index) const
  1805. {
  1806. return index < MAX_TEXTURE_UNITS ? textures_[index] : 0;
  1807. }
  1808. RenderSurface* Graphics::GetRenderTarget(unsigned index) const
  1809. {
  1810. return index < MAX_RENDERTARGETS ? renderTargets_[index] : 0;
  1811. }
  1812. unsigned Graphics::GetStreamFrequency(unsigned index) const
  1813. {
  1814. return 0;
  1815. }
  1816. IntVector2 Graphics::GetRenderTargetDimensions() const
  1817. {
  1818. int width, height;
  1819. if (renderTargets_[0])
  1820. {
  1821. width = renderTargets_[0]->GetWidth();
  1822. height = renderTargets_[0]->GetHeight();
  1823. }
  1824. else if (depthStencil_)
  1825. {
  1826. width = depthStencil_->GetWidth();
  1827. height = depthStencil_->GetHeight();
  1828. }
  1829. else
  1830. {
  1831. width = width_;
  1832. height = height_;
  1833. }
  1834. return IntVector2(width, height);
  1835. }
  1836. void Graphics::WindowResized()
  1837. {
  1838. if (!impl_->window_)
  1839. return;
  1840. int newWidth, newHeight;
  1841. SDL_GetWindowSize(impl_->window_, &newWidth, &newHeight);
  1842. if (newWidth == width_ && newHeight == height_)
  1843. return;
  1844. width_ = newWidth;
  1845. height_ = newHeight;
  1846. // Reset rendertargets and viewport for the new screen size
  1847. ResetRenderTargets();
  1848. LOGDEBUGF("Window was resized to %dx%d", width_, height_);
  1849. using namespace ScreenMode;
  1850. VariantMap& eventData = GetEventDataMap();
  1851. eventData[P_WIDTH] = width_;
  1852. eventData[P_HEIGHT] = height_;
  1853. eventData[P_FULLSCREEN] = fullscreen_;
  1854. eventData[P_RESIZABLE] = resizable_;
  1855. eventData[P_BORDERLESS] = borderless_;
  1856. SendEvent(E_SCREENMODE, eventData);
  1857. }
  1858. void Graphics::WindowMoved()
  1859. {
  1860. if (!impl_->window_)
  1861. return;
  1862. int newX, newY;
  1863. SDL_GetWindowPosition(impl_->window_, &newX, &newY);
  1864. if (newX == position_.x_ && newY == position_.y_)
  1865. return;
  1866. position_.x_ = newX;
  1867. position_.y_ = newY;
  1868. LOGDEBUGF("Window was moved to %d,%d", position_.x_, position_.y_);
  1869. using namespace WindowPos;
  1870. VariantMap& eventData = GetEventDataMap();
  1871. eventData[P_X] = position_.x_;
  1872. eventData[P_Y] = position_.y_;
  1873. SendEvent(E_WINDOWPOS, eventData);
  1874. }
  1875. void Graphics::AddGPUObject(GPUObject* object)
  1876. {
  1877. MutexLock lock(gpuObjectMutex_);
  1878. gpuObjects_.Push(object);
  1879. }
  1880. void Graphics::RemoveGPUObject(GPUObject* object)
  1881. {
  1882. MutexLock lock(gpuObjectMutex_);
  1883. gpuObjects_.Remove(object);
  1884. }
  1885. void* Graphics::ReserveScratchBuffer(unsigned size)
  1886. {
  1887. if (!size)
  1888. return 0;
  1889. if (size > maxScratchBufferRequest_)
  1890. maxScratchBufferRequest_ = size;
  1891. // First check for a free buffer that is large enough
  1892. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1893. {
  1894. if (!i->reserved_ && i->size_ >= size)
  1895. {
  1896. i->reserved_ = true;
  1897. return i->data_.Get();
  1898. }
  1899. }
  1900. // Then check if a free buffer can be resized
  1901. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1902. {
  1903. if (!i->reserved_)
  1904. {
  1905. i->data_ = new unsigned char[size];
  1906. i->size_ = size;
  1907. i->reserved_ = true;
  1908. return i->data_.Get();
  1909. }
  1910. }
  1911. // Finally allocate a new buffer
  1912. ScratchBuffer newBuffer;
  1913. newBuffer.data_ = new unsigned char[size];
  1914. newBuffer.size_ = size;
  1915. newBuffer.reserved_ = true;
  1916. scratchBuffers_.Push(newBuffer);
  1917. return newBuffer.data_.Get();
  1918. }
  1919. void Graphics::FreeScratchBuffer(void* buffer)
  1920. {
  1921. if (!buffer)
  1922. return;
  1923. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1924. {
  1925. if (i->reserved_ && i->data_.Get() == buffer)
  1926. {
  1927. i->reserved_ = false;
  1928. return;
  1929. }
  1930. }
  1931. LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  1932. }
  1933. void Graphics::CleanupScratchBuffers()
  1934. {
  1935. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  1936. {
  1937. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2)
  1938. {
  1939. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  1940. i->size_ = maxScratchBufferRequest_;
  1941. }
  1942. }
  1943. maxScratchBufferRequest_ = 0;
  1944. }
  1945. void Graphics::Release(bool clearGPUObjects, bool closeWindow)
  1946. {
  1947. if (!impl_->window_)
  1948. return;
  1949. releasingGPUObjects_ = true;
  1950. {
  1951. MutexLock lock(gpuObjectMutex_);
  1952. if (clearGPUObjects)
  1953. {
  1954. // Shutting down: release all GPU objects that still exist
  1955. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1956. (*i)->Release();
  1957. gpuObjects_.Clear();
  1958. }
  1959. else
  1960. {
  1961. // We are not shutting down, but recreating the context: mark GPU objects lost
  1962. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1963. (*i)->OnDeviceLost();
  1964. SendEvent(E_DEVICELOST);
  1965. }
  1966. }
  1967. releasingGPUObjects_ = false;
  1968. CleanupFramebuffers(true);
  1969. depthTextures_.Clear();
  1970. shaderPrograms_.Clear();
  1971. // End fullscreen mode first to counteract transition and getting stuck problems on OS X
  1972. #if defined(__APPLE__) && !defined(IOS)
  1973. if (closeWindow && fullscreen_ && !externalWindow_)
  1974. SDL_SetWindowFullscreen(impl_->window_, SDL_FALSE);
  1975. #endif
  1976. if (impl_->context_)
  1977. {
  1978. // Do not log this message if we are exiting
  1979. if (!clearGPUObjects)
  1980. LOGINFO("OpenGL context lost");
  1981. SDL_GL_DeleteContext(impl_->context_);
  1982. impl_->context_ = 0;
  1983. }
  1984. if (closeWindow)
  1985. {
  1986. SDL_ShowCursor(SDL_TRUE);
  1987. // Do not destroy external window except when shutting down
  1988. if (!externalWindow_ || clearGPUObjects)
  1989. {
  1990. SDL_DestroyWindow(impl_->window_);
  1991. impl_->window_ = 0;
  1992. }
  1993. }
  1994. }
  1995. void Graphics::Restore()
  1996. {
  1997. if (!impl_->window_)
  1998. return;
  1999. #ifdef ANDROID
  2000. // On Android the context may be lost behind the scenes as the application is minimized
  2001. if (impl_->context_ && !SDL_GL_GetCurrentContext())
  2002. {
  2003. impl_->context_ = 0;
  2004. // Mark GPU objects lost without a current context. In this case they just mark their internal state lost
  2005. // but do not perform OpenGL commands to delete the GL objects
  2006. Release(false, false);
  2007. }
  2008. #endif
  2009. // Ensure first that the context exists
  2010. if (!impl_->context_)
  2011. {
  2012. impl_->context_ = SDL_GL_CreateContext(impl_->window_);
  2013. #ifdef IOS
  2014. glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&impl_->systemFbo_);
  2015. #endif
  2016. ResetCachedState();
  2017. }
  2018. if (!impl_->context_)
  2019. return;
  2020. // Set up texture data read/write alignment. It is important that this is done before uploading any texture data
  2021. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  2022. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  2023. {
  2024. MutexLock lock(gpuObjectMutex_);
  2025. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  2026. (*i)->OnDeviceReset();
  2027. }
  2028. SendEvent(E_DEVICERESET);
  2029. }
  2030. void Graphics::Maximize()
  2031. {
  2032. if (!impl_->window_)
  2033. return;
  2034. SDL_MaximizeWindow(impl_->window_);
  2035. }
  2036. void Graphics::Minimize()
  2037. {
  2038. if (!impl_->window_)
  2039. return;
  2040. SDL_MinimizeWindow(impl_->window_);
  2041. }
  2042. void Graphics::CleanupRenderSurface(RenderSurface* surface)
  2043. {
  2044. if (!surface)
  2045. return;
  2046. // Flush pending FBO changes first if any
  2047. CommitFramebuffer();
  2048. unsigned currentFbo = impl_->boundFbo_;
  2049. // Go through all FBOs and clean up the surface from them
  2050. for (HashMap<unsigned long long, FrameBufferObject>::Iterator i = impl_->frameBuffers_.Begin();
  2051. i != impl_->frameBuffers_.End(); ++i)
  2052. {
  2053. for (unsigned j = 0; j < MAX_RENDERTARGETS; ++j)
  2054. {
  2055. if (i->second_.colorAttachments_[j] == surface)
  2056. {
  2057. if (currentFbo != i->second_.fbo_)
  2058. {
  2059. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, i->second_.fbo_);
  2060. currentFbo = i->second_.fbo_;
  2061. }
  2062. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + j, GL_TEXTURE_2D, 0, 0);
  2063. i->second_.colorAttachments_[j] = 0;
  2064. // Mark drawbuffer bits to need recalculation
  2065. i->second_.drawBuffers_ = M_MAX_UNSIGNED;
  2066. }
  2067. }
  2068. if (i->second_.depthAttachment_ == surface)
  2069. {
  2070. if (currentFbo != i->second_.fbo_)
  2071. {
  2072. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, i->second_.fbo_);
  2073. currentFbo = i->second_.fbo_;
  2074. }
  2075. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2076. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2077. i->second_.depthAttachment_ = 0;
  2078. }
  2079. }
  2080. // Restore previously bound FBO now if needed
  2081. if (currentFbo != impl_->boundFbo_)
  2082. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->boundFbo_);
  2083. }
  2084. void Graphics::MarkFBODirty()
  2085. {
  2086. impl_->fboDirty_ = true;
  2087. }
  2088. unsigned Graphics::GetAlphaFormat()
  2089. {
  2090. return GL_ALPHA;
  2091. }
  2092. unsigned Graphics::GetLuminanceFormat()
  2093. {
  2094. return GL_LUMINANCE;
  2095. }
  2096. unsigned Graphics::GetLuminanceAlphaFormat()
  2097. {
  2098. return GL_LUMINANCE_ALPHA;
  2099. }
  2100. unsigned Graphics::GetRGBFormat()
  2101. {
  2102. return GL_RGB;
  2103. }
  2104. unsigned Graphics::GetRGBAFormat()
  2105. {
  2106. return GL_RGBA;
  2107. }
  2108. unsigned Graphics::GetRGBA16Format()
  2109. {
  2110. #ifndef GL_ES_VERSION_2_0
  2111. return GL_RGBA16;
  2112. #else
  2113. return GL_RGBA;
  2114. #endif
  2115. }
  2116. unsigned Graphics::GetRGBAFloat16Format()
  2117. {
  2118. #ifndef GL_ES_VERSION_2_0
  2119. return GL_RGBA16F_ARB;
  2120. #else
  2121. return GL_RGBA;
  2122. #endif
  2123. }
  2124. unsigned Graphics::GetRGBAFloat32Format()
  2125. {
  2126. #ifndef GL_ES_VERSION_2_0
  2127. return GL_RGBA32F_ARB;
  2128. #else
  2129. return GL_RGBA;
  2130. #endif
  2131. }
  2132. unsigned Graphics::GetRG16Format()
  2133. {
  2134. #ifndef GL_ES_VERSION_2_0
  2135. return GL_RG16;
  2136. #else
  2137. return GL_RGBA;
  2138. #endif
  2139. }
  2140. unsigned Graphics::GetRGFloat16Format()
  2141. {
  2142. #ifndef GL_ES_VERSION_2_0
  2143. return GL_RG16F;
  2144. #else
  2145. return GL_RGBA;
  2146. #endif
  2147. }
  2148. unsigned Graphics::GetRGFloat32Format()
  2149. {
  2150. #ifndef GL_ES_VERSION_2_0
  2151. return GL_RG32F;
  2152. #else
  2153. return GL_RGBA;
  2154. #endif
  2155. }
  2156. unsigned Graphics::GetFloat16Format()
  2157. {
  2158. #ifndef GL_ES_VERSION_2_0
  2159. return GL_LUMINANCE16F_ARB;
  2160. #else
  2161. return GL_LUMINANCE;
  2162. #endif
  2163. }
  2164. unsigned Graphics::GetFloat32Format()
  2165. {
  2166. #ifndef GL_ES_VERSION_2_0
  2167. return GL_LUMINANCE32F_ARB;
  2168. #else
  2169. return GL_LUMINANCE;
  2170. #endif
  2171. }
  2172. unsigned Graphics::GetLinearDepthFormat()
  2173. {
  2174. // OpenGL FBO specs state that color attachments must have the same format; therefore must encode linear depth to RGBA
  2175. // manually if not using a readable hardware depth texture
  2176. return GL_RGBA;
  2177. }
  2178. unsigned Graphics::GetDepthStencilFormat()
  2179. {
  2180. #ifndef GL_ES_VERSION_2_0
  2181. return GL_DEPTH24_STENCIL8_EXT;
  2182. #else
  2183. return glesDepthStencilFormat;
  2184. #endif
  2185. }
  2186. unsigned Graphics::GetFormat(const String& formatName)
  2187. {
  2188. String nameLower = formatName.ToLower().Trimmed();
  2189. if (nameLower == "a")
  2190. return GetAlphaFormat();
  2191. if (nameLower == "l")
  2192. return GetLuminanceFormat();
  2193. if (nameLower == "la")
  2194. return GetLuminanceAlphaFormat();
  2195. if (nameLower == "rgb")
  2196. return GetRGBFormat();
  2197. if (nameLower == "rgba")
  2198. return GetRGBAFormat();
  2199. if (nameLower == "rgba16")
  2200. return GetRGBA16Format();
  2201. if (nameLower == "rgba16f")
  2202. return GetRGBAFloat16Format();
  2203. if (nameLower == "rgba32f")
  2204. return GetRGBAFloat32Format();
  2205. if (nameLower == "rg16")
  2206. return GetRG16Format();
  2207. if (nameLower == "rg16f")
  2208. return GetRGFloat16Format();
  2209. if (nameLower == "rg32f")
  2210. return GetRGFloat32Format();
  2211. if (nameLower == "r16f")
  2212. return GetFloat16Format();
  2213. if (nameLower == "r32f" || nameLower == "float")
  2214. return GetFloat32Format();
  2215. if (nameLower == "lineardepth" || nameLower == "depth")
  2216. return GetLinearDepthFormat();
  2217. if (nameLower == "d24s8")
  2218. return GetDepthStencilFormat();
  2219. return GetRGBFormat();
  2220. }
  2221. void Graphics::CreateWindowIcon()
  2222. {
  2223. if (windowIcon_)
  2224. {
  2225. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  2226. if (surface)
  2227. {
  2228. SDL_SetWindowIcon(impl_->window_, surface);
  2229. SDL_FreeSurface(surface);
  2230. }
  2231. }
  2232. }
  2233. void Graphics::CheckFeatureSupport(String& extensions)
  2234. {
  2235. // Check supported features: light pre-pass, deferred rendering and hardware depth texture
  2236. lightPrepassSupport_ = false;
  2237. deferredSupport_ = false;
  2238. int numSupportedRTs = 1;
  2239. #ifndef GL_ES_VERSION_2_0
  2240. glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &numSupportedRTs);
  2241. // If hardware depth is not supported, must support 2 rendertargets for light pre-pass, and 4 for deferred
  2242. if (numSupportedRTs >= 2)
  2243. lightPrepassSupport_ = true;
  2244. if (numSupportedRTs >= 4)
  2245. deferredSupport_ = true;
  2246. #if defined(__APPLE__) && !defined(IOS)
  2247. // On OS X check for an Intel driver and use shadow map RGBA dummy color textures, because mixing
  2248. // depth-only FBO rendering and backbuffer rendering will bug, resulting in a black screen in full
  2249. // screen mode, and incomplete shadow maps in windowed mode
  2250. String renderer((const char*)glGetString(GL_RENDERER));
  2251. if (renderer.Contains("Intel", false))
  2252. dummyColorFormat_ = GetRGBAFormat();
  2253. #endif
  2254. #else
  2255. // Check for best supported depth renderbuffer format for GLES2
  2256. if (CheckExtension(extensions, "GL_OES_depth24"))
  2257. glesDepthStencilFormat = GL_DEPTH_COMPONENT24_OES;
  2258. if (CheckExtension(extensions, "GL_OES_packed_depth_stencil"))
  2259. glesDepthStencilFormat = GL_DEPTH24_STENCIL8_OES;
  2260. if (!CheckExtension(extensions, "GL_OES_depth_texture"))
  2261. {
  2262. shadowMapFormat_ = 0;
  2263. hiresShadowMapFormat_ = 0;
  2264. }
  2265. else
  2266. {
  2267. #ifdef IOS
  2268. // iOS hack: depth renderbuffer seems to fail, so use depth textures for everything
  2269. // if supported
  2270. glesDepthStencilFormat = GL_DEPTH_COMPONENT;
  2271. #endif
  2272. shadowMapFormat_ = GL_DEPTH_COMPONENT;
  2273. hiresShadowMapFormat_ = 0;
  2274. }
  2275. #endif
  2276. }
  2277. void Graphics::CommitFramebuffer()
  2278. {
  2279. if (!impl_->fboDirty_)
  2280. return;
  2281. impl_->fboDirty_ = false;
  2282. // First check if no framebuffer is needed. In that case simply return to backbuffer rendering
  2283. bool noFbo = !depthStencil_;
  2284. if (noFbo)
  2285. {
  2286. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  2287. {
  2288. if (renderTargets_[i])
  2289. {
  2290. noFbo = false;
  2291. break;
  2292. }
  2293. }
  2294. }
  2295. if (noFbo)
  2296. {
  2297. if (impl_->boundFbo_ != impl_->systemFbo_)
  2298. {
  2299. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->systemFbo_);
  2300. impl_->boundFbo_ = impl_->systemFbo_;
  2301. }
  2302. #ifndef GL_ES_VERSION_2_0
  2303. // Disable/enable sRGB write
  2304. if (sRGBWriteSupport_)
  2305. {
  2306. bool sRGBWrite = sRGB_;
  2307. if (sRGBWrite != impl_->sRGBWrite_)
  2308. {
  2309. if (sRGBWrite)
  2310. glEnable(GL_FRAMEBUFFER_SRGB_EXT);
  2311. else
  2312. glDisable(GL_FRAMEBUFFER_SRGB_EXT);
  2313. impl_->sRGBWrite_ = sRGBWrite;
  2314. }
  2315. }
  2316. #endif
  2317. return;
  2318. }
  2319. // Search for a new framebuffer based on format & size, or create new
  2320. IntVector2 rtSize = Graphics::GetRenderTargetDimensions();
  2321. unsigned format = 0;
  2322. if (renderTargets_[0])
  2323. format = renderTargets_[0]->GetParentTexture()->GetFormat();
  2324. else if (depthStencil_)
  2325. format = depthStencil_->GetParentTexture()->GetFormat();
  2326. unsigned long long fboKey = (rtSize.x_ << 16 | rtSize.y_) | (((unsigned long long)format) << 32);
  2327. HashMap<unsigned long long, FrameBufferObject>::Iterator i = impl_->frameBuffers_.Find(fboKey);
  2328. if (i == impl_->frameBuffers_.End())
  2329. {
  2330. FrameBufferObject newFbo;
  2331. glGenFramebuffersEXT(1, &newFbo.fbo_);
  2332. i = impl_->frameBuffers_.Insert(MakePair(fboKey, newFbo));
  2333. }
  2334. i->second_.useTimer_.Reset();
  2335. if (impl_->boundFbo_ != i->second_.fbo_)
  2336. {
  2337. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, i->second_.fbo_);
  2338. impl_->boundFbo_ = i->second_.fbo_;
  2339. }
  2340. #ifndef GL_ES_VERSION_2_0
  2341. // Setup readbuffers & drawbuffers if needed
  2342. if (i->second_.readBuffers_ != GL_NONE)
  2343. {
  2344. glReadBuffer(GL_NONE);
  2345. i->second_.readBuffers_ = GL_NONE;
  2346. }
  2347. // Calculate the bit combination of non-zero color rendertargets to first check if the combination changed
  2348. unsigned newDrawBuffers = 0;
  2349. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  2350. {
  2351. if (renderTargets_[i])
  2352. newDrawBuffers |= 1 << i;
  2353. }
  2354. if (newDrawBuffers != i->second_.drawBuffers_)
  2355. {
  2356. // Check for no color rendertargets (depth rendering only)
  2357. if (!newDrawBuffers)
  2358. glDrawBuffer(GL_NONE);
  2359. else
  2360. {
  2361. int drawBufferIds[MAX_RENDERTARGETS];
  2362. unsigned drawBufferCount = 0;
  2363. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  2364. {
  2365. if (renderTargets_[i])
  2366. drawBufferIds[drawBufferCount++] = GL_COLOR_ATTACHMENT0_EXT + i;
  2367. }
  2368. glDrawBuffers(drawBufferCount, (const GLenum*)drawBufferIds);
  2369. }
  2370. i->second_.drawBuffers_ = newDrawBuffers;
  2371. }
  2372. #endif
  2373. for (unsigned j = 0; j < MAX_RENDERTARGETS; ++j)
  2374. {
  2375. if (renderTargets_[j])
  2376. {
  2377. Texture* texture = renderTargets_[j]->GetParentTexture();
  2378. // If texture's parameters are dirty, update before attaching
  2379. if (texture->GetParametersDirty())
  2380. {
  2381. SetTextureForUpdate(texture);
  2382. texture->UpdateParameters();
  2383. SetTexture(0, 0);
  2384. }
  2385. if (i->second_.colorAttachments_[j] != renderTargets_[j])
  2386. {
  2387. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + j, renderTargets_[j]->GetTarget(),
  2388. texture->GetGPUObject(), 0);
  2389. i->second_.colorAttachments_[j] = renderTargets_[j];
  2390. }
  2391. }
  2392. else
  2393. {
  2394. if (i->second_.colorAttachments_[j])
  2395. {
  2396. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + j, GL_TEXTURE_2D, 0, 0);
  2397. i->second_.colorAttachments_[j] = 0;
  2398. }
  2399. }
  2400. }
  2401. if (depthStencil_)
  2402. {
  2403. // Bind either a renderbuffer or a depth texture, depending on what is available
  2404. Texture* texture = depthStencil_->GetParentTexture();
  2405. #ifndef GL_ES_VERSION_2_0
  2406. bool hasStencil = texture->GetFormat() == GL_DEPTH24_STENCIL8_EXT;
  2407. #else
  2408. bool hasStencil = texture->GetFormat() == GL_DEPTH24_STENCIL8_OES;
  2409. #endif
  2410. unsigned renderBufferID = depthStencil_->GetRenderBuffer();
  2411. if (!renderBufferID)
  2412. {
  2413. // If texture's parameters are dirty, update before attaching
  2414. if (texture->GetParametersDirty())
  2415. {
  2416. SetTextureForUpdate(texture);
  2417. texture->UpdateParameters();
  2418. SetTexture(0, 0);
  2419. }
  2420. if (i->second_.depthAttachment_ != depthStencil_)
  2421. {
  2422. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texture->GetGPUObject(), 0);
  2423. if (hasStencil)
  2424. {
  2425. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D,
  2426. texture->GetGPUObject(), 0);
  2427. }
  2428. else
  2429. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2430. i->second_.depthAttachment_ = depthStencil_;
  2431. }
  2432. }
  2433. else
  2434. {
  2435. if (i->second_.depthAttachment_ != depthStencil_)
  2436. {
  2437. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
  2438. if (hasStencil)
  2439. {
  2440. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
  2441. renderBufferID);
  2442. }
  2443. else
  2444. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2445. i->second_.depthAttachment_ = depthStencil_;
  2446. }
  2447. }
  2448. }
  2449. else
  2450. {
  2451. if (i->second_.depthAttachment_)
  2452. {
  2453. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2454. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  2455. i->second_.depthAttachment_ = 0;
  2456. }
  2457. }
  2458. #ifndef GL_ES_VERSION_2_0
  2459. // Disable/enable sRGB write
  2460. if (sRGBWriteSupport_)
  2461. {
  2462. bool sRGBWrite = renderTargets_[0] ? renderTargets_[0]->GetParentTexture()->GetSRGB() : sRGB_;
  2463. if (sRGBWrite != impl_->sRGBWrite_)
  2464. {
  2465. if (sRGBWrite)
  2466. glEnable(GL_FRAMEBUFFER_SRGB_EXT);
  2467. else
  2468. glDisable(GL_FRAMEBUFFER_SRGB_EXT);
  2469. impl_->sRGBWrite_ = sRGBWrite;
  2470. }
  2471. }
  2472. #endif
  2473. }
  2474. bool Graphics::CheckFramebuffer()
  2475. {
  2476. return glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT;
  2477. }
  2478. void Graphics::CleanupFramebuffers(bool force)
  2479. {
  2480. if (!IsDeviceLost())
  2481. {
  2482. for (HashMap<unsigned long long, FrameBufferObject>::Iterator i = impl_->frameBuffers_.Begin();
  2483. i != impl_->frameBuffers_.End();)
  2484. {
  2485. if (i->second_.fbo_ != impl_->boundFbo_ && (force || i->second_.useTimer_.GetMSec(false) >
  2486. MAX_FRAMEBUFFER_AGE))
  2487. {
  2488. glDeleteFramebuffersEXT(1, &i->second_.fbo_);
  2489. i = impl_->frameBuffers_.Erase(i);
  2490. }
  2491. else
  2492. ++i;
  2493. }
  2494. }
  2495. else
  2496. {
  2497. impl_->boundFbo_ = 0;
  2498. impl_->frameBuffers_.Clear();
  2499. }
  2500. }
  2501. void Graphics::ResetCachedState()
  2502. {
  2503. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  2504. {
  2505. vertexBuffers_[i] = 0;
  2506. elementMasks_[i] = 0;
  2507. }
  2508. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  2509. {
  2510. textures_[i] = 0;
  2511. textureTypes_[i] = 0;
  2512. }
  2513. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  2514. renderTargets_[i] = 0;
  2515. depthStencil_ = 0;
  2516. viewport_ = IntRect(0, 0, 0, 0);
  2517. indexBuffer_ = 0;
  2518. vertexShader_ = 0;
  2519. pixelShader_ = 0;
  2520. shaderProgram_ = 0;
  2521. blendMode_ = BLEND_REPLACE;
  2522. textureAnisotropy_ = 1;
  2523. colorWrite_ = true;
  2524. cullMode_ = CULL_NONE;
  2525. constantDepthBias_ = 0.0f;
  2526. slopeScaledDepthBias_ = 0.0f;
  2527. depthTestMode_ = CMP_ALWAYS;
  2528. depthWrite_ = false;
  2529. fillMode_ = FILL_SOLID;
  2530. scissorTest_ = false;
  2531. scissorRect_ = IntRect::ZERO;
  2532. stencilTest_ = false;
  2533. stencilTestMode_ = CMP_ALWAYS;
  2534. stencilPass_ = OP_KEEP;
  2535. stencilFail_ = OP_KEEP;
  2536. stencilZFail_ = OP_KEEP;
  2537. stencilRef_ = 0;
  2538. stencilCompareMask_ = M_MAX_UNSIGNED;
  2539. stencilWriteMask_ = M_MAX_UNSIGNED;
  2540. useClipPlane_ = false;
  2541. drawAntialiased_ = true;
  2542. lastInstanceOffset_ = 0;
  2543. impl_->activeTexture_ = 0;
  2544. impl_->enabledAttributes_ = 0;
  2545. impl_->boundFbo_ = impl_->systemFbo_;
  2546. impl_->sRGBWrite_ = false;
  2547. // Set initial state to match Direct3D
  2548. if (impl_->context_)
  2549. {
  2550. glEnable(GL_DEPTH_TEST);
  2551. SetCullMode(CULL_CCW);
  2552. SetDepthTest(CMP_LESSEQUAL);
  2553. SetDepthWrite(true);
  2554. }
  2555. }
  2556. void Graphics::SetTextureUnitMappings()
  2557. {
  2558. textureUnits_["DiffMap"] = TU_DIFFUSE;
  2559. textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
  2560. textureUnits_["NormalMap"] = TU_NORMAL;
  2561. textureUnits_["SpecMap"] = TU_SPECULAR;
  2562. textureUnits_["EmissiveMap"] = TU_EMISSIVE;
  2563. textureUnits_["EnvMap"] = TU_ENVIRONMENT;
  2564. textureUnits_["EnvCubeMap"] = TU_ENVIRONMENT;
  2565. textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
  2566. textureUnits_["LightSpotMap"] = TU_LIGHTSHAPE;
  2567. textureUnits_["LightCubeMap"] = TU_LIGHTSHAPE;
  2568. textureUnits_["ShadowMap"] = TU_SHADOWMAP;
  2569. textureUnits_["FaceSelectCubeMap"] = TU_FACESELECT;
  2570. textureUnits_["IndirectionCubeMap"] = TU_INDIRECTION;
  2571. textureUnits_["AlbedoBuffer"] = TU_ALBEDOBUFFER;
  2572. textureUnits_["NormalBuffer"] = TU_NORMALBUFFER;
  2573. textureUnits_["DepthBuffer"] = TU_DEPTHBUFFER;
  2574. textureUnits_["LightBuffer"] = TU_LIGHTBUFFER;
  2575. textureUnits_["VolumeMap"] = TU_VOLUMEMAP;
  2576. textureUnits_["ZoneCubeMap"] = TU_ZONE;
  2577. textureUnits_["ZoneVolumeMap"] = TU_ZONE;
  2578. }
  2579. void RegisterGraphicsLibrary(Context* context)
  2580. {
  2581. Animation::RegisterObject(context);
  2582. Material::RegisterObject(context);
  2583. Model::RegisterObject(context);
  2584. Shader::RegisterObject(context);
  2585. Technique::RegisterObject(context);
  2586. Texture2D::RegisterObject(context);
  2587. Texture3D::RegisterObject(context);
  2588. TextureCube::RegisterObject(context);
  2589. Camera::RegisterObject(context);
  2590. Drawable::RegisterObject(context);
  2591. Light::RegisterObject(context);
  2592. StaticModel::RegisterObject(context);
  2593. StaticModelGroup::RegisterObject(context);
  2594. Skybox::RegisterObject(context);
  2595. AnimatedModel::RegisterObject(context);
  2596. AnimationController::RegisterObject(context);
  2597. BillboardSet::RegisterObject(context);
  2598. ParticleEffect::RegisterObject(context);
  2599. ParticleEmitter::RegisterObject(context);
  2600. CustomGeometry::RegisterObject(context);
  2601. DecalSet::RegisterObject(context);
  2602. Terrain::RegisterObject(context);
  2603. TerrainPatch::RegisterObject(context);
  2604. DebugRenderer::RegisterObject(context);
  2605. Octree::RegisterObject(context);
  2606. Zone::RegisterObject(context);
  2607. }
  2608. }