OGLGraphics.cpp 75 KB

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