OGLGraphics.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "DebugRenderer.h"
  31. #include "Graphics.h"
  32. #include "GraphicsEvents.h"
  33. #include "GraphicsImpl.h"
  34. #include "IndexBuffer.h"
  35. #include "Light.h"
  36. #include "Log.h"
  37. #include "Material.h"
  38. #include "Mutex.h"
  39. #include "Octree.h"
  40. #include "ParticleEmitter.h"
  41. #include "ProcessUtils.h"
  42. #include "Profiler.h"
  43. #include "RenderSurface.h"
  44. #include "Shader.h"
  45. #include "ShaderProgram.h"
  46. #include "ShaderVariation.h"
  47. #include "Skybox.h"
  48. #include "StringUtils.h"
  49. #include "Technique.h"
  50. #include "Texture2D.h"
  51. #include "TextureCube.h"
  52. #include "VertexBuffer.h"
  53. #include "Zone.h"
  54. #include <stdio.h>
  55. #include "DebugNew.h"
  56. static const unsigned glCmpFunc[] =
  57. {
  58. GL_ALWAYS,
  59. GL_EQUAL,
  60. GL_NOTEQUAL,
  61. GL_LESS,
  62. GL_LEQUAL,
  63. GL_GREATER,
  64. GL_GEQUAL
  65. };
  66. static const unsigned glSrcBlend[] =
  67. {
  68. GL_ONE,
  69. GL_ONE,
  70. GL_DST_COLOR,
  71. GL_SRC_ALPHA,
  72. GL_SRC_ALPHA,
  73. GL_ONE,
  74. GL_ONE_MINUS_DST_ALPHA
  75. };
  76. static const unsigned glDestBlend[] =
  77. {
  78. GL_ZERO,
  79. GL_ONE,
  80. GL_ZERO,
  81. GL_ONE_MINUS_SRC_ALPHA,
  82. GL_ONE,
  83. GL_ONE_MINUS_SRC_ALPHA,
  84. GL_DST_ALPHA
  85. };
  86. static const unsigned glStencilOps[] =
  87. {
  88. GL_KEEP,
  89. GL_ZERO,
  90. GL_REPLACE,
  91. GL_INCR_WRAP,
  92. GL_DECR_WRAP
  93. };
  94. static unsigned numInstances = 0;
  95. static const String noParameter;
  96. int CloseCallback(GLFWwindow window)
  97. {
  98. // Do not let GLFW close the window, rather do it ourselves in a controlled fashion
  99. Context* context = GetWindowContext(window);
  100. if (context)
  101. {
  102. Graphics* graphics = context->GetSubsystem<Graphics>();
  103. if (graphics)
  104. graphics->Close();
  105. }
  106. return GL_FALSE;
  107. }
  108. OBJECTTYPESTATIC(Graphics);
  109. Graphics::Graphics(Context* context_) :
  110. Object(context_),
  111. impl_(new GraphicsImpl()),
  112. width_(0),
  113. height_(0),
  114. multiSample_(1),
  115. fullscreen_(false),
  116. vsync_(false),
  117. tripleBuffer_(false),
  118. flushGPU_(true),
  119. numPrimitives_(0),
  120. numBatches_(0),
  121. defaultTextureFilterMode_(FILTER_BILINEAR),
  122. shadowMapFormat_(GL_DEPTH_COMPONENT16),
  123. hiresShadowMapFormat_(GL_DEPTH_COMPONENT24),
  124. shaderParameterFrame_(0)
  125. {
  126. ResetCachedState();
  127. SetTextureUnitMappings();
  128. {
  129. MutexLock lock(GetStaticMutex());
  130. if (!numInstances)
  131. glfwInit();
  132. ++numInstances;
  133. }
  134. }
  135. Graphics::~Graphics()
  136. {
  137. Close();
  138. delete impl_;
  139. impl_ = 0;
  140. {
  141. MutexLock lock(GetStaticMutex());
  142. --numInstances;
  143. if (!numInstances)
  144. glfwTerminate();
  145. }
  146. }
  147. void Graphics::SetWindowTitle(const String& windowTitle)
  148. {
  149. windowTitle_ = windowTitle;
  150. if (impl_->window_)
  151. glfwSetWindowTitle(impl_->window_, windowTitle_.CString());
  152. }
  153. bool Graphics::SetMode(int width, int height, bool fullscreen, bool vsync, bool tripleBuffer, int multiSample)
  154. {
  155. PROFILE(SetScreenMode);
  156. multiSample = Clamp(multiSample, 1, 16);
  157. if (IsInitialized() && width == width_ && height == height_ && fullscreen == fullscreen_ &&
  158. vsync == vsync_ && tripleBuffer == tripleBuffer_ && multiSample == multiSample_)
  159. return true;
  160. // If only vsync changes, do not destroy/recreate the context
  161. if (IsInitialized() && width == width_ && height == height_ && fullscreen == fullscreen_ &&
  162. tripleBuffer == tripleBuffer_ && multiSample == multiSample_ && vsync != vsync_)
  163. {
  164. glfwSwapInterval(vsync ? 1 : 0);
  165. vsync_ = vsync;
  166. return true;
  167. }
  168. // If zero dimensions in windowed mode, set default. If zero in fullscreen, use desktop mode
  169. if (!width || !height)
  170. {
  171. if (!fullscreen)
  172. {
  173. width = 800;
  174. height = 600;
  175. }
  176. else
  177. {
  178. GLFWvidmode mode;
  179. glfwGetDesktopMode(&mode);
  180. width = mode.width;
  181. height = mode.height;
  182. }
  183. }
  184. // Close the existing window
  185. Release();
  186. {
  187. // GLFW window parameters and the window list are static, so need to operate under static lock
  188. MutexLock lock(GetStaticMutex());
  189. glfwOpenWindowHint(GLFW_RED_BITS, 8);
  190. glfwOpenWindowHint(GLFW_GREEN_BITS, 8);
  191. glfwOpenWindowHint(GLFW_BLUE_BITS, 8);
  192. glfwOpenWindowHint(GLFW_ALPHA_BITS, 0);
  193. glfwOpenWindowHint(GLFW_DEPTH_BITS, 24);
  194. glfwOpenWindowHint(GLFW_STENCIL_BITS, 8);
  195. glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
  196. if (multiSample > 1)
  197. glfwOpenWindowHint(GLFW_FSAA_SAMPLES, multiSample);
  198. else
  199. glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 0);
  200. impl_->window_ = glfwOpenWindow(width, height, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOWED, windowTitle_.CString(), 0);
  201. if (!impl_->window_)
  202. {
  203. LOGERROR("Could not open window");
  204. return false;
  205. }
  206. // If OpenGL extensions not yet initialized, initialize now
  207. if (!GLeeInitialized())
  208. GLeeInit();
  209. if (!_GLEE_VERSION_2_0)
  210. {
  211. LOGERROR("OpenGL 2.0 is required");
  212. glfwCloseWindow(impl_->window_);
  213. return false;
  214. }
  215. if (!_GLEE_EXT_framebuffer_object || !_GLEE_EXT_packed_depth_stencil)
  216. {
  217. LOGERROR("EXT_framebuffer_object and EXT_packed_depth_stencil OpenGL extensions are required");
  218. glfwCloseWindow(impl_->window_);
  219. return false;
  220. }
  221. // Set window close callback
  222. glfwSetWindowCloseCallback(CloseCallback);
  223. // Associate GLFW window with the execution context
  224. SetWindowContext(impl_->window_, context_);
  225. }
  226. // Set vsync
  227. glfwSwapInterval(vsync ? 1 : 0);
  228. // Query for system backbuffer depth
  229. glGetIntegerv(GL_DEPTH_BITS, &impl_->windowDepthBits_);
  230. impl_->depthBits_ = impl_->windowDepthBits_;
  231. // Create the FBO
  232. glGenFramebuffersEXT(1, &impl_->fbo_);
  233. // Set initial state to match Direct3D
  234. glEnable(GL_DEPTH_TEST);
  235. SetCullMode(CULL_CCW);
  236. SetDepthTest(CMP_LESSEQUAL);
  237. glfwGetWindowSize(impl_->window_, &width_, &height_);
  238. fullscreen_ = fullscreen;
  239. vsync_ = vsync;
  240. tripleBuffer_ = tripleBuffer;
  241. multiSample_ = multiSample;
  242. // Reset render targets and viewport for the new screen mode
  243. ResetRenderTargets();
  244. // Clear the window to black now, because GPU object restore may take time
  245. Clear(CLEAR_COLOR);
  246. glfwSwapBuffers();
  247. // Let GPU objects restore themselves
  248. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  249. (*i)->OnDeviceReset();
  250. if (multiSample > 1)
  251. LOGINFO("Set screen mode " + String(width_) + "x" + String(height_) + " " + (fullscreen_ ? "fullscreen" : "windowed") +
  252. " multisample " + String(multiSample));
  253. else
  254. LOGINFO("Set screen mode " + String(width_) + "x" + String(height_) + " " + (fullscreen_ ? "fullscreen" : "windowed"));
  255. using namespace ScreenMode;
  256. VariantMap eventData;
  257. eventData[P_WIDTH] = width_;
  258. eventData[P_HEIGHT] = height_;
  259. eventData[P_FULLSCREEN] = fullscreen_;
  260. SendEvent(E_SCREENMODE, eventData);
  261. return true;
  262. }
  263. bool Graphics::SetMode(int width, int height)
  264. {
  265. return SetMode(width, height, fullscreen_, vsync_, tripleBuffer_, multiSample_);
  266. }
  267. bool Graphics::ToggleFullscreen()
  268. {
  269. return SetMode(width_, height_, !fullscreen_, vsync_, tripleBuffer_, multiSample_);
  270. }
  271. void Graphics::Close()
  272. {
  273. if (!IsInitialized())
  274. return;
  275. // Release all GPU objects that still exist
  276. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  277. (*i)->Release();
  278. gpuObjects_.Clear();
  279. // Actually close the window
  280. Release();
  281. }
  282. bool Graphics::TakeScreenShot(Image& destImage)
  283. {
  284. PROFILE(TakeScreenShot);
  285. ResetRenderTargets();
  286. destImage.SetSize(width_, height_, 3);
  287. glReadPixels(0, 0, width_, height_, GL_RGB, GL_UNSIGNED_BYTE, destImage.GetData());
  288. return true;
  289. }
  290. void Graphics::SetFlushGPU(bool enable)
  291. {
  292. flushGPU_ = enable;
  293. }
  294. bool Graphics::BeginFrame()
  295. {
  296. PROFILE(BeginRendering);
  297. if (!IsInitialized())
  298. return false;
  299. // If we should be fullscreen, but are not currently active, do not render
  300. if (fullscreen_ && (!glfwGetWindowParam(impl_->window_, GLFW_ACTIVE) || glfwGetWindowParam(impl_->window_, GLFW_ICONIFIED)))
  301. return false;
  302. // Set default render target and depth buffer
  303. ResetRenderTargets();
  304. // Cleanup textures from previous frame
  305. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  306. SetTexture(i, 0);
  307. // Enable color and depth write
  308. SetColorWrite(true);
  309. SetDepthWrite(true);
  310. numPrimitives_ = 0;
  311. numBatches_ = 0;
  312. SendEvent(E_BEGINRENDERING);
  313. return true;
  314. }
  315. void Graphics::EndFrame()
  316. {
  317. PROFILE(EndRendering);
  318. if (!IsInitialized())
  319. return;
  320. SendEvent(E_ENDRENDERING);
  321. glfwSwapBuffers();
  322. }
  323. void Graphics::Clear(unsigned flags, const Color& color, float depth, unsigned stencil)
  324. {
  325. bool oldColorWrite = colorWrite_;
  326. bool oldDepthWrite = depthWrite_;
  327. if (flags & CLEAR_COLOR && !oldColorWrite)
  328. SetColorWrite(true);
  329. if (flags & CLEAR_DEPTH && !oldDepthWrite)
  330. SetDepthWrite(true);
  331. unsigned glFlags = 0;
  332. if (flags & CLEAR_COLOR)
  333. {
  334. glFlags |= GL_COLOR_BUFFER_BIT;
  335. glClearColor(color.r_, color.g_, color.b_, color.a_);
  336. }
  337. if (flags & CLEAR_DEPTH)
  338. {
  339. glFlags |= GL_DEPTH_BUFFER_BIT;
  340. glClearDepth(depth);
  341. }
  342. if (flags & CLEAR_STENCIL)
  343. {
  344. glFlags |= GL_STENCIL_BUFFER_BIT;
  345. glClearStencil(stencil);
  346. }
  347. glClear(glFlags);
  348. SetColorWrite(oldColorWrite);
  349. SetDepthWrite(oldDepthWrite);
  350. }
  351. void Graphics::Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount)
  352. {
  353. if (!vertexCount)
  354. return;
  355. unsigned primitiveCount = 0;
  356. switch (type)
  357. {
  358. case TRIANGLE_LIST:
  359. primitiveCount = vertexCount / 3;
  360. glDrawArrays(GL_TRIANGLES, vertexStart, vertexCount);
  361. break;
  362. case LINE_LIST:
  363. primitiveCount = vertexCount / 2;
  364. glDrawArrays(GL_LINES, vertexStart, vertexCount);
  365. break;
  366. }
  367. numPrimitives_ += primitiveCount;
  368. ++numBatches_;
  369. }
  370. void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  371. {
  372. if (!indexCount || !indexBuffer_)
  373. return;
  374. unsigned primitiveCount = 0;
  375. unsigned indexSize = indexBuffer_->GetIndexSize();
  376. switch (type)
  377. {
  378. case TRIANGLE_LIST:
  379. primitiveCount = indexCount / 3;
  380. if (indexSize == sizeof(unsigned short))
  381. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (const GLvoid*)(indexStart * indexSize));
  382. else
  383. glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (const GLvoid*)(indexStart * indexSize));
  384. break;
  385. case LINE_LIST:
  386. primitiveCount = indexCount / 2;
  387. if (indexSize == sizeof(unsigned short))
  388. glDrawElements(GL_LINES, indexCount, GL_UNSIGNED_SHORT, (const GLvoid*)(indexStart * indexSize));
  389. else
  390. glDrawElements(GL_LINES, indexCount, GL_UNSIGNED_INT, (const GLvoid*)(indexStart * indexSize));
  391. break;
  392. }
  393. numPrimitives_ += primitiveCount;
  394. ++numBatches_;
  395. }
  396. void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount)
  397. {
  398. }
  399. void Graphics::SetVertexBuffer(VertexBuffer* buffer)
  400. {
  401. Vector<VertexBuffer*> vertexBuffers(1);
  402. PODVector<unsigned> elementMasks(1);
  403. vertexBuffers[0] = buffer;
  404. elementMasks[0] = MASK_DEFAULT;
  405. SetVertexBuffers(vertexBuffers, elementMasks);
  406. }
  407. bool Graphics::SetVertexBuffers(const Vector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks,
  408. unsigned instanceOffset)
  409. {
  410. if (buffers.Size() > MAX_VERTEX_STREAMS)
  411. {
  412. LOGERROR("Too many vertex buffers");
  413. return false;
  414. }
  415. if (buffers.Size() != elementMasks.Size())
  416. {
  417. LOGERROR("Amount of element masks and vertex buffers does not match");
  418. return false;
  419. }
  420. // If no valid shader to determine the attribute bindings, can not set vertex buffers
  421. if (!shaderProgram_)
  422. return false;
  423. const int* attributeLocations = shaderProgram_->GetAttributeLocations();
  424. bool changed = false;
  425. unsigned newAttributes = 0;
  426. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  427. {
  428. VertexBuffer* buffer = 0;
  429. unsigned elementMask = 0;
  430. if (i < buffers.Size())
  431. {
  432. buffer = buffers[i];
  433. elementMask = elementMasks[i];
  434. if (elementMask == MASK_DEFAULT && buffer)
  435. elementMask = buffers[i]->GetElementMask();
  436. }
  437. // If buffer and element mask have stayed the same, skip to the next buffer
  438. if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i])
  439. continue;
  440. vertexBuffers_[i] = buffer;
  441. elementMasks_[i] = elementMask;
  442. changed = true;
  443. if (!buffer)
  444. continue;
  445. glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
  446. unsigned vertexSize = buffer->GetVertexSize();
  447. for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
  448. {
  449. // If shader does not use the attribute, do not bind it (bandwidth optimization)
  450. int attributeIndex = attributeLocations[j];
  451. if (attributeIndex < 0)
  452. continue;
  453. unsigned elementBit = 1 << j;
  454. unsigned attributeBit = 1 << attributeIndex;
  455. if (elementMask & elementBit)
  456. {
  457. newAttributes |= attributeBit;
  458. // Enable attribute if not enabled yet
  459. if ((impl_->enabledAttributes_ & attributeBit) == 0)
  460. {
  461. glEnableVertexAttribArray(attributeIndex);
  462. impl_->enabledAttributes_ |= attributeBit;
  463. }
  464. // Set the attribute pointer
  465. glVertexAttribPointer(attributeIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
  466. VertexBuffer::elementNormalize[j], vertexSize, (const GLvoid*)(buffer->GetElementOffset((VertexElement)j)));
  467. }
  468. }
  469. }
  470. if (!changed)
  471. return true;
  472. // Now check which vertex attributes should be disabled
  473. unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
  474. int disableIndex = 0;
  475. while (disableAttributes)
  476. {
  477. if (disableAttributes & 1)
  478. {
  479. glDisableVertexAttribArray(disableIndex);
  480. impl_->enabledAttributes_ &= ~(1 << disableIndex);
  481. }
  482. disableAttributes >>= 1;
  483. ++disableIndex;
  484. }
  485. return true;
  486. }
  487. bool Graphics::SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>&
  488. elementMasks, unsigned instanceOffset)
  489. {
  490. if (buffers.Size() > MAX_VERTEX_STREAMS)
  491. {
  492. LOGERROR("Too many vertex buffers");
  493. return false;
  494. }
  495. if (buffers.Size() != elementMasks.Size())
  496. {
  497. LOGERROR("Amount of element masks and vertex buffers does not match");
  498. return false;
  499. }
  500. // If no valid shader to determine the attribute bindings, can not set vertex buffers
  501. if (!shaderProgram_)
  502. return false;
  503. const int* attributeLocations = shaderProgram_->GetAttributeLocations();
  504. bool changed = false;
  505. unsigned newAttributes = 0;
  506. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  507. {
  508. VertexBuffer* buffer = 0;
  509. unsigned elementMask = 0;
  510. if (i < buffers.Size())
  511. {
  512. buffer = buffers[i];
  513. elementMask = elementMasks[i];
  514. if (elementMask == MASK_DEFAULT && buffer)
  515. elementMask = buffers[i]->GetElementMask();
  516. }
  517. // If buffer and element mask have stayed the same, skip to the next buffer
  518. if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i])
  519. continue;
  520. vertexBuffers_[i] = buffer;
  521. elementMasks_[i] = elementMask;
  522. changed = true;
  523. if (!buffer)
  524. continue;
  525. glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
  526. unsigned vertexSize = buffer->GetVertexSize();
  527. for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
  528. {
  529. // If shader does not use the attribute, do not bind it (bandwidth optimization)
  530. int attributeIndex = attributeLocations[j];
  531. if (attributeIndex < 0)
  532. continue;
  533. unsigned elementBit = 1 << j;
  534. unsigned attributeBit = 1 << attributeIndex;
  535. if (elementMask & elementBit)
  536. {
  537. newAttributes |= attributeBit;
  538. // Enable attribute if not enabled yet
  539. if ((impl_->enabledAttributes_ & attributeBit) == 0)
  540. {
  541. glEnableVertexAttribArray(attributeIndex);
  542. impl_->enabledAttributes_ |= attributeBit;
  543. }
  544. // Set the attribute pointer
  545. glVertexAttribPointer(attributeIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
  546. VertexBuffer::elementNormalize[j], vertexSize, (const GLvoid*)(buffer->GetElementOffset((VertexElement)j)));
  547. }
  548. }
  549. }
  550. if (!changed)
  551. return true;
  552. // Now check which vertex attributes should be disabled
  553. unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
  554. int disableIndex = 0;
  555. while (disableAttributes)
  556. {
  557. if (disableAttributes & 1)
  558. {
  559. glDisableVertexAttribArray(disableIndex);
  560. impl_->enabledAttributes_ &= ~(1 << disableIndex);
  561. }
  562. disableAttributes >>= 1;
  563. ++disableIndex;
  564. }
  565. return true;
  566. }
  567. void Graphics::SetIndexBuffer(IndexBuffer* buffer)
  568. {
  569. if (indexBuffer_ == buffer)
  570. return;
  571. if (buffer)
  572. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetGPUObject());
  573. else
  574. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  575. indexBuffer_ = buffer;
  576. }
  577. void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
  578. {
  579. if (vs == vertexShader_ && ps == pixelShader_)
  580. return;
  581. // Compile the shaders now if not yet compiled. If already attempted, do not retry
  582. if (vs && !vs->IsCompiled())
  583. {
  584. if (vs->GetCompilerOutput().Empty())
  585. {
  586. PROFILE(CompileVertexShader);
  587. bool success = vs->Create();
  588. if (success)
  589. LOGDEBUG("Compiled vertex shader " + vs->GetName());
  590. else
  591. {
  592. LOGERROR("Failed to compile vertex shader " + vs->GetName() + ":\n" + vs->GetCompilerOutput());
  593. vs = 0;
  594. }
  595. }
  596. else
  597. vs = 0;
  598. }
  599. if (ps && !ps->IsCompiled())
  600. {
  601. if (ps->GetCompilerOutput().Empty())
  602. {
  603. PROFILE(CompilePixelShader);
  604. bool success = ps->Create();
  605. if (success)
  606. LOGDEBUG("Compiled pixel shader " + ps->GetName());
  607. else
  608. {
  609. LOGERROR("Failed to compile pixel shader " + ps->GetName() + ":\n" + ps->GetCompilerOutput());
  610. ps = 0;
  611. }
  612. }
  613. else
  614. ps = 0;
  615. }
  616. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  617. {
  618. vertexBuffers_[i] = 0;
  619. elementMasks_[i] = 0;
  620. }
  621. if (!vs || !ps)
  622. {
  623. glUseProgram(0);
  624. vertexShader_ = 0;
  625. pixelShader_ = 0;
  626. shaderProgram_ = 0;
  627. }
  628. else
  629. {
  630. vertexShader_ = vs;
  631. pixelShader_ = ps;
  632. Pair<ShaderVariation*, ShaderVariation*> combination(vs, ps);
  633. ShaderProgramMap::Iterator i = shaderPrograms_.Find(combination);
  634. if (i != shaderPrograms_.End())
  635. {
  636. // Use the existing linked program
  637. if (i->second_->IsLinked())
  638. {
  639. glUseProgram(i->second_->GetGPUObject());
  640. shaderProgram_ = i->second_;
  641. }
  642. else
  643. {
  644. glUseProgram(0);
  645. shaderProgram_ = 0;
  646. }
  647. }
  648. else
  649. {
  650. // Link a new combination
  651. SharedPtr<ShaderProgram> newProgram(new ShaderProgram(this, vs, ps));
  652. if (newProgram->Link())
  653. {
  654. LOGDEBUG("Linked vertex shader " + vs->GetName() + " and pixel shader " + ps->GetName());
  655. // Note: Link() calls glUseProgram() to set the texture sampler uniforms,
  656. // so it is not necessary to call it again
  657. shaderProgram_ = newProgram;
  658. }
  659. else
  660. {
  661. LOGERROR("Failed to link vertex shader " + vs->GetName() + " and pixel shader " + ps->GetName() + ":\n" +
  662. newProgram->GetLinkerOutput());
  663. glUseProgram(0);
  664. shaderProgram_ = 0;
  665. }
  666. shaderPrograms_[combination] = newProgram;
  667. }
  668. }
  669. }
  670. void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
  671. {
  672. if (shaderProgram_)
  673. {
  674. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  675. if (info)
  676. {
  677. switch (info->type_)
  678. {
  679. case GL_FLOAT:
  680. glUniform1fv(info->location_, count, data);
  681. break;
  682. case GL_FLOAT_VEC2:
  683. glUniform2fv(info->location_, count / 2, data);
  684. break;
  685. case GL_FLOAT_VEC3:
  686. glUniform3fv(info->location_, count / 3, data);
  687. break;
  688. case GL_FLOAT_VEC4:
  689. glUniform4fv(info->location_, count / 4, data);
  690. break;
  691. case GL_FLOAT_MAT3:
  692. glUniformMatrix3fv(info->location_, count / 9, GL_TRUE, data);
  693. break;
  694. case GL_FLOAT_MAT4:
  695. glUniformMatrix4fv(info->location_, count / 16, GL_TRUE, data);
  696. break;
  697. }
  698. }
  699. }
  700. }
  701. void Graphics::SetShaderParameter(StringHash param, float value)
  702. {
  703. if (shaderProgram_)
  704. {
  705. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  706. if (info)
  707. glUniform1fv(info->location_, 1, &value);
  708. }
  709. }
  710. void Graphics::SetShaderParameter(StringHash param, const Color& color)
  711. {
  712. SetShaderParameter(param, color.GetData(), 4);
  713. }
  714. void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
  715. {
  716. if (shaderProgram_)
  717. {
  718. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  719. if (info)
  720. glUniformMatrix3fv(info->location_, 1, GL_TRUE, matrix.GetData());
  721. }
  722. }
  723. void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
  724. {
  725. if (shaderProgram_)
  726. {
  727. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  728. if (info)
  729. {
  730. // Check the uniform type to avoid mismatch
  731. switch (info->type_)
  732. {
  733. case GL_FLOAT:
  734. glUniform1fv(info->location_, 1, vector.GetData());
  735. break;
  736. case GL_FLOAT_VEC2:
  737. glUniform2fv(info->location_, 1, vector.GetData());
  738. break;
  739. case GL_FLOAT_VEC3:
  740. glUniform3fv(info->location_, 1, vector.GetData());
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
  747. {
  748. if (shaderProgram_)
  749. {
  750. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  751. if (info)
  752. glUniformMatrix4fv(info->location_, 1, GL_TRUE, matrix.GetData());
  753. }
  754. }
  755. void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
  756. {
  757. if (shaderProgram_)
  758. {
  759. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  760. if (info)
  761. {
  762. // Check the uniform type to avoid mismatch
  763. switch (info->type_)
  764. {
  765. case GL_FLOAT:
  766. glUniform1fv(info->location_, 1, vector.GetData());
  767. break;
  768. case GL_FLOAT_VEC2:
  769. glUniform2fv(info->location_, 1, vector.GetData());
  770. break;
  771. case GL_FLOAT_VEC3:
  772. glUniform3fv(info->location_, 1, vector.GetData());
  773. break;
  774. case GL_FLOAT_VEC4:
  775. glUniform4fv(info->location_, 1, vector.GetData());
  776. break;
  777. }
  778. }
  779. }
  780. }
  781. void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
  782. {
  783. if (shaderProgram_)
  784. {
  785. const ShaderParameter* info = shaderProgram_->GetParameter(param);
  786. if (info)
  787. {
  788. float data[16];
  789. data[0] = matrix.m00_;
  790. data[1] = matrix.m01_;
  791. data[2] = matrix.m02_;
  792. data[3] = matrix.m03_;
  793. data[4] = matrix.m10_;
  794. data[5] = matrix.m11_;
  795. data[6] = matrix.m12_;
  796. data[7] = matrix.m13_;
  797. data[8] = matrix.m20_;
  798. data[9] = matrix.m21_;
  799. data[10] = matrix.m22_;
  800. data[11] = matrix.m23_;
  801. data[12] = 0.0f;
  802. data[13] = 0.0f;
  803. data[14] = 0.0f;
  804. data[15] = 1.0f;
  805. glUniformMatrix4fv(info->location_, 1, GL_TRUE, data);
  806. }
  807. }
  808. }
  809. bool Graphics::NeedParameterUpdate(StringHash param, const void* source)
  810. {
  811. if (shaderProgram_)
  812. return shaderProgram_->NeedParameterUpdate(param, source, shaderParameterFrame_);
  813. return false;
  814. }
  815. bool Graphics::NeedTextureUnit(TextureUnit unit)
  816. {
  817. if (shaderProgram_ && shaderProgram_->HasTextureUnit(unit))
  818. return true;
  819. return false;
  820. }
  821. void Graphics::ClearParameterSources()
  822. {
  823. ++shaderParameterFrame_;
  824. }
  825. void Graphics::ClearTransformSources()
  826. {
  827. if (shaderProgram_)
  828. {
  829. shaderProgram_->ClearParameterSource(VSP_MODEL);
  830. shaderProgram_->ClearParameterSource(VSP_VIEWPROJ);
  831. }
  832. }
  833. void Graphics::CleanupShaderPrograms()
  834. {
  835. for (ShaderProgramMap::Iterator i = shaderPrograms_.Begin(); i != shaderPrograms_.End();)
  836. {
  837. ShaderProgramMap::Iterator current = i++;
  838. ShaderVariation* vs = current->second_->GetVertexShader();
  839. ShaderVariation* ps = current->second_->GetPixelShader();
  840. if (!vs || !ps || !vs->GetGPUObject() || !ps->GetGPUObject())
  841. shaderPrograms_.Erase(current);
  842. }
  843. }
  844. void Graphics::SetTexture(unsigned index, Texture* texture)
  845. {
  846. if (index >= MAX_TEXTURE_UNITS)
  847. return;
  848. // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
  849. if (texture)
  850. {
  851. if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
  852. texture = texture->GetBackupTexture();
  853. }
  854. if (textures_[index] != texture)
  855. {
  856. if (impl_->activeTexture_ != index)
  857. {
  858. glActiveTexture(GL_TEXTURE0 + index);
  859. impl_->activeTexture_ = index;
  860. }
  861. if (texture)
  862. {
  863. unsigned glType = texture->GetTarget();
  864. if (glType != textureTypes_[index])
  865. {
  866. if (textureTypes_[index])
  867. glDisable(textureTypes_[index]);
  868. glEnable(glType);
  869. textureTypes_[index] = glType;
  870. }
  871. glBindTexture(glType, texture->GetGPUObject());
  872. if (texture->GetParametersDirty())
  873. texture->UpdateParameters();
  874. }
  875. else
  876. {
  877. if (textureTypes_[index])
  878. glBindTexture(textureTypes_[index], 0);
  879. }
  880. textures_[index] = texture;
  881. }
  882. else
  883. {
  884. if (texture && texture->GetParametersDirty())
  885. {
  886. if (impl_->activeTexture_ != index)
  887. {
  888. glActiveTexture(GL_TEXTURE0 + index);
  889. impl_->activeTexture_ = index;
  890. }
  891. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  892. texture->UpdateParameters();
  893. }
  894. }
  895. }
  896. void Graphics::SetTextureForUpdate(Texture* texture)
  897. {
  898. if (impl_->activeTexture_ != 0)
  899. {
  900. glActiveTexture(GL_TEXTURE0);
  901. impl_->activeTexture_ = 0;
  902. }
  903. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  904. textures_[0] = texture;
  905. }
  906. void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
  907. {
  908. if (mode != defaultTextureFilterMode_)
  909. {
  910. defaultTextureFilterMode_ = mode;
  911. SetTextureParametersDirty();
  912. }
  913. }
  914. void Graphics::SetTextureAnisotropy(unsigned level)
  915. {
  916. if (level != textureAnisotropy_)
  917. {
  918. textureAnisotropy_ = level;
  919. SetTextureParametersDirty();
  920. }
  921. }
  922. void Graphics::SetTextureParametersDirty()
  923. {
  924. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  925. {
  926. Texture* texture = dynamic_cast<Texture*>(*i);
  927. if (texture)
  928. texture->SetParametersDirty();
  929. }
  930. }
  931. void Graphics::ResetRenderTargets()
  932. {
  933. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  934. SetRenderTarget(i, (RenderSurface*)0);
  935. SetDepthStencil((RenderSurface*)0);
  936. }
  937. void Graphics::ResetRenderTarget(unsigned index)
  938. {
  939. SetRenderTarget(index, (RenderSurface*)0);
  940. }
  941. void Graphics::ResetDepthStencil()
  942. {
  943. SetDepthStencil((RenderSurface*)0);
  944. }
  945. void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
  946. {
  947. if (index >= MAX_RENDERTARGETS)
  948. return;
  949. if (renderTarget != renderTargets_[index])
  950. {
  951. renderTargets_[index] = renderTarget;
  952. // If the render target is also bound as a texture, replace with backup texture or null
  953. if (renderTarget)
  954. {
  955. Texture* parentTexture = renderTarget->GetParentTexture();
  956. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  957. {
  958. if (textures_[i] == parentTexture)
  959. SetTexture(i, textures_[i]->GetBackupTexture());
  960. }
  961. }
  962. // Bind the FBO to be able to make changes to it
  963. if (!impl_->fboBound_)
  964. {
  965. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
  966. impl_->fboBound_ = true;
  967. }
  968. if (renderTarget)
  969. {
  970. Texture* texture = renderTarget->GetParentTexture();
  971. // If texture's parameters are dirty, update before attaching
  972. if (texture->GetParametersDirty())
  973. {
  974. SetTextureForUpdate(texture);
  975. texture->UpdateParameters();
  976. SetTexture(0, 0);
  977. }
  978. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, renderTarget->GetTarget(), texture->GetGPUObject(), 0);
  979. }
  980. else
  981. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_2D, 0, 0);
  982. // Disable color buffer writing/reading if only a depth texture is to be used:
  983. // otherwise it is an OpenGL error (incomplete framebuffer)
  984. SetDrawBuffers();
  985. // If all render targets and the depth buffer are not textures, revert to backbuffer rendering
  986. bool noFBO = (depthStencil_ == 0);
  987. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  988. {
  989. if (renderTargets_[i])
  990. {
  991. noFBO = false;
  992. break;
  993. }
  994. }
  995. if (noFBO && impl_->fboBound_)
  996. {
  997. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  998. impl_->fboBound_ = false;
  999. }
  1000. }
  1001. }
  1002. void Graphics::SetRenderTarget(unsigned index, Texture2D* renderTexture)
  1003. {
  1004. RenderSurface* renderTarget = 0;
  1005. if (renderTexture)
  1006. renderTarget = renderTexture->GetRenderSurface();
  1007. SetRenderTarget(index, renderTarget);
  1008. }
  1009. void Graphics::SetDepthStencil(RenderSurface* depthStencil)
  1010. {
  1011. if (depthStencil != depthStencil_)
  1012. {
  1013. // If we are using a render target texture, it is required in OpenGL to also have an own depth stencil
  1014. // Create a new depth stencil texture as necessary to be able to provide similar behaviour as Direct3D9
  1015. if (renderTargets_[0] && !depthStencil)
  1016. {
  1017. int width = renderTargets_[0]->GetWidth();
  1018. int height = renderTargets_[0]->GetHeight();
  1019. // Direct3D9 default depth stencil can not be used when render target is larger than the window.
  1020. // Check size similarly
  1021. if (width <= width_ && height <= height_)
  1022. {
  1023. int searchKey = (width << 16) | height;
  1024. HashMap<int, SharedPtr<Texture2D> >::Iterator i = depthTextures_.Find(searchKey);
  1025. if (i != depthTextures_.End())
  1026. depthStencil = i->second_->GetRenderSurface();
  1027. else
  1028. {
  1029. SharedPtr<Texture2D> newDepthTexture(new Texture2D(context_));
  1030. newDepthTexture->SetSize(width, height, GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  1031. depthTextures_[searchKey] = newDepthTexture;
  1032. depthStencil = newDepthTexture->GetRenderSurface();
  1033. }
  1034. }
  1035. }
  1036. /// \todo Should check that the texture actually is in depth format
  1037. depthStencil_ = depthStencil;
  1038. // Bind the FBO to be able to make changes to it
  1039. if (!impl_->fboBound_)
  1040. {
  1041. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
  1042. impl_->fboBound_ = true;
  1043. }
  1044. if (depthStencil)
  1045. {
  1046. // Bind either a renderbuffer or a depth texture, depending on what is available
  1047. unsigned renderBufferID = depthStencil->GetRenderBuffer();
  1048. if (!renderBufferID)
  1049. {
  1050. Texture* texture = depthStencil->GetParentTexture();
  1051. // If texture's parameters are dirty, update before attaching
  1052. if (texture->GetParametersDirty())
  1053. {
  1054. SetTextureForUpdate(texture);
  1055. texture->UpdateParameters();
  1056. SetTexture(0, 0);
  1057. }
  1058. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texture->GetGPUObject(), 0);
  1059. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1060. impl_->depthBits_ = texture->GetDepthBits();
  1061. }
  1062. else
  1063. {
  1064. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
  1065. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
  1066. impl_->depthBits_ = 24;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1072. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1073. impl_->depthBits_ = impl_->windowDepthBits_;
  1074. }
  1075. // Disable color buffer writing/reading if only a depth texture is to be used:
  1076. // otherwise it is an OpenGL error (incomplete framebuffer)
  1077. SetDrawBuffers();
  1078. // If all render targets and the depth buffer are not textures, revert to backbuffer rendering
  1079. bool noFBO = (depthStencil_ == 0);
  1080. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1081. {
  1082. if (renderTargets_[i])
  1083. {
  1084. noFBO = false;
  1085. break;
  1086. }
  1087. }
  1088. if (noFBO && impl_->fboBound_)
  1089. {
  1090. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  1091. impl_->fboBound_ = false;
  1092. }
  1093. }
  1094. // Reset viewport and scissor test
  1095. IntVector2 viewSize = GetRenderTargetDimensions();
  1096. SetViewport(IntRect(0, 0, viewSize.x_, viewSize.y_));
  1097. }
  1098. void Graphics::SetDepthStencil(Texture2D* depthTexture)
  1099. {
  1100. RenderSurface* depthStencil = 0;
  1101. if (depthTexture)
  1102. depthStencil = depthTexture->GetRenderSurface();
  1103. SetDepthStencil(depthStencil);
  1104. }
  1105. void Graphics::SetViewport(const IntRect& rect)
  1106. {
  1107. IntVector2 rtSize = GetRenderTargetDimensions();
  1108. IntRect rectCopy = rect;
  1109. if (rectCopy.right_ <= rectCopy.left_)
  1110. rectCopy.right_ = rectCopy.left_ + 1;
  1111. if (rectCopy.bottom_ <= rectCopy.top_)
  1112. rectCopy.bottom_ = rectCopy.top_ + 1;
  1113. rectCopy.left_ = Clamp(rectCopy.left_, 0, rtSize.x_);
  1114. rectCopy.top_ = Clamp(rectCopy.top_, 0, rtSize.y_);
  1115. rectCopy.right_ = Clamp(rectCopy.right_, 0, rtSize.x_);
  1116. rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, rtSize.y_);
  1117. if (rectCopy != viewport_)
  1118. {
  1119. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1120. glViewport(rectCopy.left_, rtSize.y_ - rectCopy.bottom_, rectCopy.right_ - rectCopy.left_, rectCopy.bottom_ - rectCopy.top_);
  1121. viewport_ = rectCopy;
  1122. }
  1123. // Disable scissor test, needs to be re-enabled by the user
  1124. SetScissorTest(false);
  1125. }
  1126. void Graphics::SetAlphaTest(bool enable, CompareMode mode, float alphaRef)
  1127. {
  1128. if (enable != alphaTest_)
  1129. {
  1130. if (enable)
  1131. glEnable(GL_ALPHA_TEST);
  1132. else
  1133. glDisable(GL_ALPHA_TEST);
  1134. alphaTest_ = enable;
  1135. }
  1136. if (enable)
  1137. {
  1138. alphaRef = Clamp(alphaRef, 0.0f, 1.0f);
  1139. if (mode != alphaTestMode_ || alphaRef != alphaRef_)
  1140. {
  1141. glAlphaFunc(glCmpFunc[mode], alphaRef);
  1142. alphaTestMode_ = mode;
  1143. alphaRef_ = alphaRef;
  1144. }
  1145. }
  1146. }
  1147. void Graphics::SetBlendMode(BlendMode mode)
  1148. {
  1149. if (mode != blendMode_)
  1150. {
  1151. if (mode == BLEND_REPLACE)
  1152. glDisable(GL_BLEND);
  1153. else
  1154. {
  1155. glEnable(GL_BLEND);
  1156. glBlendFunc(glSrcBlend[mode], glDestBlend[mode]);
  1157. }
  1158. blendMode_ = mode;
  1159. }
  1160. }
  1161. void Graphics::SetColorWrite(bool enable)
  1162. {
  1163. if (enable != colorWrite_)
  1164. {
  1165. if (enable)
  1166. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  1167. else
  1168. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  1169. colorWrite_ = enable;
  1170. }
  1171. }
  1172. void Graphics::SetCullMode(CullMode mode)
  1173. {
  1174. if (mode != cullMode_)
  1175. {
  1176. if (mode == CULL_NONE)
  1177. glDisable(GL_CULL_FACE);
  1178. else
  1179. {
  1180. // Use Direct3D convention, ie. clockwise vertices define a front face
  1181. glEnable(GL_CULL_FACE);
  1182. glCullFace(mode == CULL_CCW ? GL_FRONT : GL_BACK);
  1183. }
  1184. cullMode_ = mode;
  1185. }
  1186. }
  1187. void Graphics::SetDepthBias(float constantBias, float slopeScaledBias)
  1188. {
  1189. if (constantBias != constantDepthBias_ || slopeScaledBias != slopeScaledDepthBias_)
  1190. {
  1191. if (constantBias != 0.0f || slopeScaledBias != 0.0f)
  1192. {
  1193. // Bring the constant bias from Direct3D9 scale to OpenGL (depends on depth buffer bitdepth)
  1194. // Zero depth bits may be returned if using the packed depth stencil format. Assume 24bit in that case
  1195. int depthBits = Min(impl_->depthBits_, 23);
  1196. if (!depthBits)
  1197. depthBits = 23;
  1198. float adjustedConstantBias = constantBias * (float)(1 << (depthBits - 1));
  1199. float adjustedSlopeScaledBias = slopeScaledBias + 1.0f;
  1200. glEnable(GL_POLYGON_OFFSET_FILL);
  1201. glEnable(GL_POLYGON_OFFSET_LINE);
  1202. glPolygonOffset(adjustedSlopeScaledBias, adjustedConstantBias);
  1203. }
  1204. else
  1205. {
  1206. glDisable(GL_POLYGON_OFFSET_FILL);
  1207. glDisable(GL_POLYGON_OFFSET_LINE);
  1208. }
  1209. constantDepthBias_ = constantBias;
  1210. slopeScaledDepthBias_ = slopeScaledBias;
  1211. }
  1212. }
  1213. void Graphics::SetDepthTest(CompareMode mode)
  1214. {
  1215. if (mode != depthTestMode_)
  1216. {
  1217. glDepthFunc(glCmpFunc[mode]);
  1218. depthTestMode_ = mode;
  1219. }
  1220. }
  1221. void Graphics::SetDepthWrite(bool enable)
  1222. {
  1223. if (enable != depthWrite_)
  1224. {
  1225. glDepthMask(enable ? GL_TRUE : GL_FALSE);
  1226. depthWrite_ = enable;
  1227. }
  1228. }
  1229. void Graphics::SetFillMode(FillMode mode)
  1230. {
  1231. if (mode != fillMode_)
  1232. {
  1233. glPolygonMode(GL_FRONT_AND_BACK, mode == FILL_SOLID ? GL_FILL : GL_LINE);
  1234. fillMode_ = mode;
  1235. }
  1236. }
  1237. void Graphics::SetScissorTest(bool enable, const Rect& rect, bool borderInclusive)
  1238. {
  1239. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1240. // Disable scissor in that case to reduce state changes
  1241. if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.x_ >= 1.0f && rect.max_.y_ >= 1.0f)
  1242. enable = false;
  1243. if (enable)
  1244. {
  1245. IntVector2 rtSize(GetRenderTargetDimensions());
  1246. IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
  1247. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1248. IntRect intRect;
  1249. int expand = borderInclusive ? 1 : 0;
  1250. intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
  1251. intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
  1252. intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
  1253. intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
  1254. if (intRect.right_ == intRect.left_)
  1255. intRect.right_++;
  1256. if (intRect.bottom_ == intRect.top_)
  1257. intRect.bottom_++;
  1258. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1259. enable = false;
  1260. if (enable && scissorRect_ != intRect)
  1261. {
  1262. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1263. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
  1264. scissorRect_ = intRect;
  1265. }
  1266. }
  1267. else
  1268. scissorRect_ = IntRect::ZERO;
  1269. if (enable != scissorTest_)
  1270. {
  1271. if (enable)
  1272. glEnable(GL_SCISSOR_TEST);
  1273. else
  1274. glDisable(GL_SCISSOR_TEST);
  1275. scissorTest_ = enable;
  1276. }
  1277. }
  1278. void Graphics::SetScissorTest(bool enable, const IntRect& rect)
  1279. {
  1280. IntVector2 rtSize(GetRenderTargetDimensions());
  1281. IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
  1282. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1283. // Full scissor is same as disabling the test
  1284. if (rect.left_ <= 0 && rect.right_ >= viewSize.x_ && rect.top_ <= 0 && rect.bottom_ >= viewSize.y_)
  1285. enable = false;
  1286. if (enable)
  1287. {
  1288. IntRect intRect;
  1289. intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
  1290. intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
  1291. intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
  1292. intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
  1293. if (intRect.right_ == intRect.left_)
  1294. intRect.right_++;
  1295. if (intRect.bottom_ == intRect.top_)
  1296. intRect.bottom_++;
  1297. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1298. enable = false;
  1299. if (enable && scissorRect_ != intRect)
  1300. {
  1301. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1302. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
  1303. scissorRect_ = intRect;
  1304. }
  1305. }
  1306. else
  1307. scissorRect_ = IntRect::ZERO;
  1308. if (enable != scissorTest_)
  1309. {
  1310. if (enable)
  1311. glEnable(GL_SCISSOR_TEST);
  1312. else
  1313. glDisable(GL_SCISSOR_TEST);
  1314. scissorTest_ = enable;
  1315. }
  1316. }
  1317. void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
  1318. {
  1319. }
  1320. void Graphics::ResetStreamFrequencies()
  1321. {
  1322. }
  1323. void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned stencilMask)
  1324. {
  1325. if (enable != stencilTest_)
  1326. {
  1327. if (enable)
  1328. glEnable(GL_STENCIL_TEST);
  1329. else
  1330. glDisable(GL_STENCIL_TEST);
  1331. stencilTest_ = enable;
  1332. }
  1333. if (enable)
  1334. {
  1335. if (mode != stencilTestMode_ || stencilRef != stencilRef_ || stencilMask != stencilMask_)
  1336. {
  1337. glStencilFunc(glCmpFunc[mode], stencilRef, stencilMask);
  1338. stencilTestMode_ = mode;
  1339. stencilRef_ = stencilRef;
  1340. stencilMask_ = stencilMask;
  1341. }
  1342. if (pass != stencilPass_ || fail != stencilFail_ || zFail != stencilZFail_)
  1343. {
  1344. glStencilOp(glStencilOps[fail], glStencilOps[zFail], glStencilOps[pass]);
  1345. stencilPass_ = pass;
  1346. stencilFail_ = fail;
  1347. stencilZFail_ = zFail;
  1348. }
  1349. }
  1350. }
  1351. void Graphics::SetForceSM2(bool enable)
  1352. {
  1353. }
  1354. void Graphics::SetForceFallback(bool enable)
  1355. {
  1356. }
  1357. bool Graphics::IsInitialized() const
  1358. {
  1359. return impl_->window_ != 0;
  1360. }
  1361. void* Graphics::GetWindowHandle() const
  1362. {
  1363. return impl_->window_;
  1364. }
  1365. PODVector<IntVector2> Graphics::GetResolutions() const
  1366. {
  1367. static const unsigned MAX_MODES = 256;
  1368. GLFWvidmode modes[MAX_MODES];
  1369. unsigned count = glfwGetVideoModes(modes, MAX_MODES);
  1370. PODVector<IntVector2> ret;
  1371. for (unsigned i = 0; i < count; ++i)
  1372. {
  1373. int width = modes[i].width;
  1374. int height = modes[i].height;
  1375. // Store mode if unique
  1376. bool unique = true;
  1377. for (unsigned j = 0; j < ret.Size(); ++i)
  1378. {
  1379. if (ret[j].x_ == width && ret[j].y_ == height)
  1380. {
  1381. unique = false;
  1382. break;
  1383. }
  1384. }
  1385. if (unique)
  1386. ret.Push(IntVector2(width, height));
  1387. }
  1388. return ret;
  1389. }
  1390. PODVector<int> Graphics::GetMultiSampleLevels() const
  1391. {
  1392. PODVector<int> ret;
  1393. // No multisampling always supported
  1394. ret.Push(1);
  1395. /// \todo Implement properly, if possible
  1396. return ret;
  1397. }
  1398. VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
  1399. {
  1400. return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
  1401. }
  1402. TextureUnit Graphics::GetTextureUnit(const String& name)
  1403. {
  1404. Map<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
  1405. if (i != textureUnits_.End())
  1406. return i->second_;
  1407. else
  1408. return MAX_TEXTURE_UNITS;
  1409. }
  1410. const String& Graphics::GetTextureUnitName(TextureUnit unit)
  1411. {
  1412. for (Map<String, TextureUnit>::Iterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
  1413. {
  1414. if (i->second_ == unit)
  1415. return i->first_;
  1416. }
  1417. return noParameter;
  1418. }
  1419. Texture* Graphics::GetTexture(unsigned index) const
  1420. {
  1421. return index < MAX_TEXTURE_UNITS ? textures_[index] : 0;
  1422. }
  1423. RenderSurface* Graphics::GetRenderTarget(unsigned index) const
  1424. {
  1425. return index < MAX_RENDERTARGETS ? renderTargets_[index] : 0;
  1426. }
  1427. IntVector2 Graphics::GetRenderTargetDimensions() const
  1428. {
  1429. int width, height;
  1430. if (renderTargets_[0])
  1431. {
  1432. width = renderTargets_[0]->GetWidth();
  1433. height = renderTargets_[0]->GetHeight();
  1434. }
  1435. else if (depthStencil_)
  1436. {
  1437. width = depthStencil_->GetWidth();
  1438. height = depthStencil_->GetHeight();
  1439. }
  1440. else
  1441. {
  1442. width = width_;
  1443. height = height_;
  1444. }
  1445. return IntVector2(width, height);
  1446. }
  1447. void Graphics::AddGPUObject(GPUObject* object)
  1448. {
  1449. gpuObjects_.Push(object);
  1450. }
  1451. void Graphics::RemoveGPUObject(GPUObject* object)
  1452. {
  1453. Vector<GPUObject*>::Iterator i = gpuObjects_.Find(object);
  1454. if (i != gpuObjects_.End())
  1455. gpuObjects_.Erase(i);
  1456. }
  1457. void* Graphics::ReserveDiscardLockBuffer(unsigned size)
  1458. {
  1459. // First check for a free buffer that is large enough
  1460. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1461. {
  1462. if (!i->reserved_ && i->size_ >= size)
  1463. {
  1464. i->reserved_ = true;
  1465. return i->data_.Get();
  1466. }
  1467. }
  1468. // Then check if a free buffer can be resized
  1469. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1470. {
  1471. if (!i->reserved_)
  1472. {
  1473. i->data_ = new unsigned char[size];
  1474. i->size_ = size;
  1475. i->reserved_ = true;
  1476. return i->data_.Get();
  1477. }
  1478. }
  1479. // Finally allocate a new buffer
  1480. DiscardLockBuffer newBuffer;
  1481. newBuffer.data_ = new unsigned char[size];
  1482. newBuffer.size_ = size;
  1483. newBuffer.reserved_ = true;
  1484. discardLockBuffers_.Push(newBuffer);
  1485. return newBuffer.data_.Get();
  1486. }
  1487. void Graphics::FreeDiscardLockBuffer(void* buffer)
  1488. {
  1489. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1490. {
  1491. if (i->reserved_ && i->data_.Get() == buffer)
  1492. {
  1493. i->reserved_ = false;
  1494. return;
  1495. }
  1496. }
  1497. LOGWARNING("Reserved discard lock buffer " + ToStringHex((unsigned)buffer) + " not found");
  1498. }
  1499. unsigned Graphics::GetAlphaFormat()
  1500. {
  1501. return GL_ALPHA;
  1502. }
  1503. unsigned Graphics::GetLuminanceFormat()
  1504. {
  1505. return GL_LUMINANCE;
  1506. }
  1507. unsigned Graphics::GetLuminanceAlphaFormat()
  1508. {
  1509. return GL_LUMINANCE_ALPHA;
  1510. }
  1511. unsigned Graphics::GetRGBFormat()
  1512. {
  1513. return GL_RGB;
  1514. }
  1515. unsigned Graphics::GetRGBAFormat()
  1516. {
  1517. return GL_RGBA;
  1518. }
  1519. unsigned Graphics::GetDepthFormat()
  1520. {
  1521. return GL_DEPTH_COMPONENT24;
  1522. }
  1523. unsigned Graphics::GetDepthStencilFormat()
  1524. {
  1525. return GL_DEPTH24_STENCIL8_EXT;
  1526. }
  1527. void Graphics::ResetCachedState()
  1528. {
  1529. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1530. {
  1531. vertexBuffers_[i] = 0;
  1532. elementMasks_[i] = 0;
  1533. }
  1534. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1535. {
  1536. textures_[i] = 0;
  1537. textureTypes_[i] = 0;
  1538. }
  1539. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1540. renderTargets_[i] = 0;
  1541. depthStencil_ = 0;
  1542. viewport_ = IntRect(0, 0, 0, 0);
  1543. indexBuffer_ = 0;
  1544. vertexShader_ = 0;
  1545. pixelShader_ = 0;
  1546. shaderProgram_ = 0;
  1547. blendMode_ = BLEND_REPLACE;
  1548. alphaTest_ = false;
  1549. alphaTestMode_ = CMP_ALWAYS;
  1550. alphaRef_ = 0.0f;
  1551. textureAnisotropy_ = 1;
  1552. colorWrite_ = true;
  1553. cullMode_ = CULL_NONE;
  1554. constantDepthBias_ = 0.0f;
  1555. slopeScaledDepthBias_ = 0.0f;
  1556. depthTestMode_ = CMP_ALWAYS;
  1557. depthWrite_ = true;
  1558. fillMode_ = FILL_SOLID;
  1559. scissorTest_ = false;
  1560. scissorRect_ = IntRect::ZERO;
  1561. stencilTest_ = false;
  1562. stencilTestMode_ = CMP_ALWAYS;
  1563. stencilPass_ = OP_KEEP;
  1564. stencilFail_ = OP_KEEP;
  1565. stencilZFail_ = OP_KEEP;
  1566. stencilRef_ = 0;
  1567. stencilMask_ = M_MAX_UNSIGNED;
  1568. impl_->activeTexture_ = 0;
  1569. impl_->drawBuffers_ = M_MAX_UNSIGNED;
  1570. impl_->enabledAttributes_ = 0;
  1571. impl_->fboBound_ = false;
  1572. }
  1573. void Graphics::SetDrawBuffers()
  1574. {
  1575. // Calculate the bit combination of non-zero color render targets to first check if the combination changed
  1576. unsigned newDrawBuffers = 0;
  1577. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1578. {
  1579. if (renderTargets_[i])
  1580. newDrawBuffers |= 1 << i;
  1581. }
  1582. if (newDrawBuffers == impl_->drawBuffers_)
  1583. return;
  1584. // Check for no color render targets (depth rendering only)
  1585. if (!newDrawBuffers)
  1586. glDrawBuffer(GL_NONE);
  1587. else
  1588. {
  1589. int drawBufferIds[4];
  1590. unsigned drawBufferCount = 0;
  1591. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1592. {
  1593. if (renderTargets_[i])
  1594. drawBufferIds[drawBufferCount++] = GL_COLOR_ATTACHMENT0_EXT + i;
  1595. }
  1596. glDrawBuffers(drawBufferCount, (const GLenum*)drawBufferIds);
  1597. }
  1598. glReadBuffer(GL_NONE);
  1599. }
  1600. void Graphics::Release()
  1601. {
  1602. if (!impl_->window_)
  1603. return;
  1604. depthTextures_.Clear();
  1605. // If GPU objects exist ie. it's a context delete/recreate, not Close(), tell them to save and release themselves
  1606. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1607. (*i)->OnDeviceLost();
  1608. if (impl_->fbo_)
  1609. {
  1610. glDeleteFramebuffersEXT(1, &impl_->fbo_);
  1611. impl_->fbo_ = 0;
  1612. }
  1613. // When the new context is initialized, it will have default state again
  1614. ResetCachedState();
  1615. ClearParameterSources();
  1616. {
  1617. MutexLock lock(GetStaticMutex());
  1618. SetWindowContext(impl_->window_, 0);
  1619. glfwCloseWindow(impl_->window_);
  1620. impl_->window_ = 0;
  1621. }
  1622. }
  1623. void Graphics::SetTextureUnitMappings()
  1624. {
  1625. textureUnits_["DiffMap"] = TU_DIFFUSE;
  1626. textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
  1627. textureUnits_["NormalMap"] = TU_NORMAL;
  1628. textureUnits_["EmissiveMap"] = TU_EMISSIVE;
  1629. textureUnits_["DetailMap"] = TU_DETAIL;
  1630. textureUnits_["EnvironmentMap"] = TU_ENVIRONMENT;
  1631. textureUnits_["EnvironmentCubeMap"] = TU_ENVIRONMENT;
  1632. textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
  1633. textureUnits_["LightSpotMap"] = TU_LIGHTSHAPE;
  1634. textureUnits_["LightCubeMap"] = TU_LIGHTSHAPE;
  1635. textureUnits_["ShadowMap"] = TU_SHADOWMAP;
  1636. textureUnits_["FaceSelectCubeMap"] = TU_FACESELECT;
  1637. textureUnits_["IndirectionCubeMap"] = TU_INDIRECTION;
  1638. }
  1639. void RegisterGraphicsLibrary(Context* context)
  1640. {
  1641. Animation::RegisterObject(context);
  1642. Material::RegisterObject(context);
  1643. Model::RegisterObject(context);
  1644. Shader::RegisterObject(context);
  1645. Technique::RegisterObject(context);
  1646. Texture2D::RegisterObject(context);
  1647. TextureCube::RegisterObject(context);
  1648. Camera::RegisterObject(context);
  1649. Drawable::RegisterObject(context);
  1650. Light::RegisterObject(context);
  1651. StaticModel::RegisterObject(context);
  1652. Skybox::RegisterObject(context);
  1653. AnimatedModel::RegisterObject(context);
  1654. AnimationController::RegisterObject(context);
  1655. BillboardSet::RegisterObject(context);
  1656. ParticleEmitter::RegisterObject(context);
  1657. DebugRenderer::RegisterObject(context);
  1658. Octree::RegisterObject(context);
  1659. Zone::RegisterObject(context);
  1660. }