OGLGraphics.cpp 91 KB

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