OGLGraphics.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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::ClearParameterSource(StringHash param)
  822. {
  823. if (shaderProgram_)
  824. shaderProgram_->ClearParameterSource(param);
  825. }
  826. void Graphics::ClearParameterSources()
  827. {
  828. ++shaderParameterFrame_;
  829. }
  830. void Graphics::ClearTransformSources()
  831. {
  832. if (shaderProgram_)
  833. {
  834. shaderProgram_->ClearParameterSource(VSP_MODEL);
  835. shaderProgram_->ClearParameterSource(VSP_VIEWPROJ);
  836. }
  837. }
  838. void Graphics::CleanupShaderPrograms()
  839. {
  840. for (ShaderProgramMap::Iterator i = shaderPrograms_.Begin(); i != shaderPrograms_.End();)
  841. {
  842. ShaderProgramMap::Iterator current = i++;
  843. ShaderVariation* vs = current->second_->GetVertexShader();
  844. ShaderVariation* ps = current->second_->GetPixelShader();
  845. if (!vs || !ps || !vs->GetGPUObject() || !ps->GetGPUObject())
  846. shaderPrograms_.Erase(current);
  847. }
  848. }
  849. void Graphics::SetTexture(unsigned index, Texture* texture)
  850. {
  851. if (index >= MAX_TEXTURE_UNITS)
  852. return;
  853. // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
  854. if (texture)
  855. {
  856. if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
  857. texture = texture->GetBackupTexture();
  858. }
  859. if (textures_[index] != texture)
  860. {
  861. if (impl_->activeTexture_ != index)
  862. {
  863. glActiveTexture(GL_TEXTURE0 + index);
  864. impl_->activeTexture_ = index;
  865. }
  866. if (texture)
  867. {
  868. unsigned glType = texture->GetTarget();
  869. if (glType != textureTypes_[index])
  870. {
  871. if (textureTypes_[index])
  872. glDisable(textureTypes_[index]);
  873. glEnable(glType);
  874. textureTypes_[index] = glType;
  875. }
  876. glBindTexture(glType, texture->GetGPUObject());
  877. if (texture->GetParametersDirty())
  878. texture->UpdateParameters();
  879. }
  880. else
  881. {
  882. if (textureTypes_[index])
  883. glBindTexture(textureTypes_[index], 0);
  884. }
  885. textures_[index] = texture;
  886. }
  887. else
  888. {
  889. if (texture && texture->GetParametersDirty())
  890. {
  891. if (impl_->activeTexture_ != index)
  892. {
  893. glActiveTexture(GL_TEXTURE0 + index);
  894. impl_->activeTexture_ = index;
  895. }
  896. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  897. texture->UpdateParameters();
  898. }
  899. }
  900. }
  901. void Graphics::SetTextureForUpdate(Texture* texture)
  902. {
  903. if (impl_->activeTexture_ != 0)
  904. {
  905. glActiveTexture(GL_TEXTURE0);
  906. impl_->activeTexture_ = 0;
  907. }
  908. glBindTexture(texture->GetTarget(), texture->GetGPUObject());
  909. textures_[0] = texture;
  910. }
  911. void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
  912. {
  913. if (mode != defaultTextureFilterMode_)
  914. {
  915. defaultTextureFilterMode_ = mode;
  916. SetTextureParametersDirty();
  917. }
  918. }
  919. void Graphics::SetTextureAnisotropy(unsigned level)
  920. {
  921. if (level != textureAnisotropy_)
  922. {
  923. textureAnisotropy_ = level;
  924. SetTextureParametersDirty();
  925. }
  926. }
  927. void Graphics::SetTextureParametersDirty()
  928. {
  929. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  930. {
  931. Texture* texture = dynamic_cast<Texture*>(*i);
  932. if (texture)
  933. texture->SetParametersDirty();
  934. }
  935. }
  936. void Graphics::ResetRenderTargets()
  937. {
  938. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  939. SetRenderTarget(i, (RenderSurface*)0);
  940. SetDepthStencil((RenderSurface*)0);
  941. }
  942. void Graphics::ResetRenderTarget(unsigned index)
  943. {
  944. SetRenderTarget(index, (RenderSurface*)0);
  945. }
  946. void Graphics::ResetDepthStencil()
  947. {
  948. SetDepthStencil((RenderSurface*)0);
  949. }
  950. void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
  951. {
  952. if (index >= MAX_RENDERTARGETS)
  953. return;
  954. if (renderTarget != renderTargets_[index])
  955. {
  956. renderTargets_[index] = renderTarget;
  957. // If the render target is also bound as a texture, replace with backup texture or null
  958. if (renderTarget)
  959. {
  960. Texture* parentTexture = renderTarget->GetParentTexture();
  961. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  962. {
  963. if (textures_[i] == parentTexture)
  964. SetTexture(i, textures_[i]->GetBackupTexture());
  965. }
  966. }
  967. // Bind the FBO to be able to make changes to it
  968. if (!impl_->fboBound_)
  969. {
  970. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
  971. impl_->fboBound_ = true;
  972. }
  973. if (renderTarget)
  974. {
  975. Texture* texture = renderTarget->GetParentTexture();
  976. // If texture's parameters are dirty, update before attaching
  977. if (texture->GetParametersDirty())
  978. {
  979. SetTextureForUpdate(texture);
  980. texture->UpdateParameters();
  981. SetTexture(0, 0);
  982. }
  983. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, renderTarget->GetTarget(), texture->GetGPUObject(), 0);
  984. }
  985. else
  986. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_2D, 0, 0);
  987. // Disable color buffer writing/reading if only a depth texture is to be used:
  988. // otherwise it is an OpenGL error (incomplete framebuffer)
  989. SetDrawBuffers();
  990. // If all render targets and the depth buffer are not textures, revert to backbuffer rendering
  991. bool noFBO = (depthStencil_ == 0);
  992. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  993. {
  994. if (renderTargets_[i])
  995. {
  996. noFBO = false;
  997. break;
  998. }
  999. }
  1000. if (noFBO && impl_->fboBound_)
  1001. {
  1002. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  1003. impl_->fboBound_ = false;
  1004. }
  1005. }
  1006. }
  1007. void Graphics::SetRenderTarget(unsigned index, Texture2D* renderTexture)
  1008. {
  1009. RenderSurface* renderTarget = 0;
  1010. if (renderTexture)
  1011. renderTarget = renderTexture->GetRenderSurface();
  1012. SetRenderTarget(index, renderTarget);
  1013. }
  1014. void Graphics::SetDepthStencil(RenderSurface* depthStencil)
  1015. {
  1016. if (depthStencil != depthStencil_)
  1017. {
  1018. // If we are using a render target texture, it is required in OpenGL to also have an own depth stencil
  1019. // Create a new depth stencil texture as necessary to be able to provide similar behaviour as Direct3D9
  1020. if (renderTargets_[0] && !depthStencil)
  1021. {
  1022. int width = renderTargets_[0]->GetWidth();
  1023. int height = renderTargets_[0]->GetHeight();
  1024. // Direct3D9 default depth stencil can not be used when render target is larger than the window.
  1025. // Check size similarly
  1026. if (width <= width_ && height <= height_)
  1027. {
  1028. int searchKey = (width << 16) | height;
  1029. HashMap<int, SharedPtr<Texture2D> >::Iterator i = depthTextures_.Find(searchKey);
  1030. if (i != depthTextures_.End())
  1031. depthStencil = i->second_->GetRenderSurface();
  1032. else
  1033. {
  1034. SharedPtr<Texture2D> newDepthTexture(new Texture2D(context_));
  1035. newDepthTexture->SetSize(width, height, GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  1036. depthTextures_[searchKey] = newDepthTexture;
  1037. depthStencil = newDepthTexture->GetRenderSurface();
  1038. }
  1039. }
  1040. }
  1041. /// \todo Should check that the texture actually is in depth format
  1042. depthStencil_ = depthStencil;
  1043. // Bind the FBO to be able to make changes to it
  1044. if (!impl_->fboBound_)
  1045. {
  1046. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
  1047. impl_->fboBound_ = true;
  1048. }
  1049. if (depthStencil)
  1050. {
  1051. // Bind either a renderbuffer or a depth texture, depending on what is available
  1052. unsigned renderBufferID = depthStencil->GetRenderBuffer();
  1053. if (!renderBufferID)
  1054. {
  1055. Texture* texture = depthStencil->GetParentTexture();
  1056. // If texture's parameters are dirty, update before attaching
  1057. if (texture->GetParametersDirty())
  1058. {
  1059. SetTextureForUpdate(texture);
  1060. texture->UpdateParameters();
  1061. SetTexture(0, 0);
  1062. }
  1063. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texture->GetGPUObject(), 0);
  1064. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1065. impl_->depthBits_ = texture->GetDepthBits();
  1066. }
  1067. else
  1068. {
  1069. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
  1070. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
  1071. impl_->depthBits_ = 24;
  1072. }
  1073. }
  1074. else
  1075. {
  1076. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1077. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
  1078. impl_->depthBits_ = impl_->windowDepthBits_;
  1079. }
  1080. // Disable color buffer writing/reading if only a depth texture is to be used:
  1081. // otherwise it is an OpenGL error (incomplete framebuffer)
  1082. SetDrawBuffers();
  1083. // If all render targets and the depth buffer are not textures, revert to backbuffer rendering
  1084. bool noFBO = (depthStencil_ == 0);
  1085. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1086. {
  1087. if (renderTargets_[i])
  1088. {
  1089. noFBO = false;
  1090. break;
  1091. }
  1092. }
  1093. if (noFBO && impl_->fboBound_)
  1094. {
  1095. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  1096. impl_->fboBound_ = false;
  1097. }
  1098. }
  1099. // Reset viewport and scissor test
  1100. IntVector2 viewSize = GetRenderTargetDimensions();
  1101. SetViewport(IntRect(0, 0, viewSize.x_, viewSize.y_));
  1102. }
  1103. void Graphics::SetDepthStencil(Texture2D* depthTexture)
  1104. {
  1105. RenderSurface* depthStencil = 0;
  1106. if (depthTexture)
  1107. depthStencil = depthTexture->GetRenderSurface();
  1108. SetDepthStencil(depthStencil);
  1109. }
  1110. void Graphics::SetViewport(const IntRect& rect)
  1111. {
  1112. IntVector2 rtSize = GetRenderTargetDimensions();
  1113. IntRect rectCopy = rect;
  1114. if (rectCopy.right_ <= rectCopy.left_)
  1115. rectCopy.right_ = rectCopy.left_ + 1;
  1116. if (rectCopy.bottom_ <= rectCopy.top_)
  1117. rectCopy.bottom_ = rectCopy.top_ + 1;
  1118. rectCopy.left_ = Clamp(rectCopy.left_, 0, rtSize.x_);
  1119. rectCopy.top_ = Clamp(rectCopy.top_, 0, rtSize.y_);
  1120. rectCopy.right_ = Clamp(rectCopy.right_, 0, rtSize.x_);
  1121. rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, rtSize.y_);
  1122. if (rectCopy != viewport_)
  1123. {
  1124. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1125. glViewport(rectCopy.left_, rtSize.y_ - rectCopy.bottom_, rectCopy.right_ - rectCopy.left_, rectCopy.bottom_ - rectCopy.top_);
  1126. viewport_ = rectCopy;
  1127. }
  1128. // Disable scissor test, needs to be re-enabled by the user
  1129. SetScissorTest(false);
  1130. }
  1131. void Graphics::SetAlphaTest(bool enable, CompareMode mode, float alphaRef)
  1132. {
  1133. if (enable != alphaTest_)
  1134. {
  1135. if (enable)
  1136. glEnable(GL_ALPHA_TEST);
  1137. else
  1138. glDisable(GL_ALPHA_TEST);
  1139. alphaTest_ = enable;
  1140. }
  1141. if (enable)
  1142. {
  1143. alphaRef = Clamp(alphaRef, 0.0f, 1.0f);
  1144. if (mode != alphaTestMode_ || alphaRef != alphaRef_)
  1145. {
  1146. glAlphaFunc(glCmpFunc[mode], alphaRef);
  1147. alphaTestMode_ = mode;
  1148. alphaRef_ = alphaRef;
  1149. }
  1150. }
  1151. }
  1152. void Graphics::SetBlendMode(BlendMode mode)
  1153. {
  1154. if (mode != blendMode_)
  1155. {
  1156. if (mode == BLEND_REPLACE)
  1157. glDisable(GL_BLEND);
  1158. else
  1159. {
  1160. glEnable(GL_BLEND);
  1161. glBlendFunc(glSrcBlend[mode], glDestBlend[mode]);
  1162. }
  1163. blendMode_ = mode;
  1164. }
  1165. }
  1166. void Graphics::SetColorWrite(bool enable)
  1167. {
  1168. if (enable != colorWrite_)
  1169. {
  1170. if (enable)
  1171. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  1172. else
  1173. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  1174. colorWrite_ = enable;
  1175. }
  1176. }
  1177. void Graphics::SetCullMode(CullMode mode)
  1178. {
  1179. if (mode != cullMode_)
  1180. {
  1181. if (mode == CULL_NONE)
  1182. glDisable(GL_CULL_FACE);
  1183. else
  1184. {
  1185. // Use Direct3D convention, ie. clockwise vertices define a front face
  1186. glEnable(GL_CULL_FACE);
  1187. glCullFace(mode == CULL_CCW ? GL_FRONT : GL_BACK);
  1188. }
  1189. cullMode_ = mode;
  1190. }
  1191. }
  1192. void Graphics::SetDepthBias(float constantBias, float slopeScaledBias)
  1193. {
  1194. if (constantBias != constantDepthBias_ || slopeScaledBias != slopeScaledDepthBias_)
  1195. {
  1196. if (constantBias != 0.0f || slopeScaledBias != 0.0f)
  1197. {
  1198. // Bring the constant bias from Direct3D9 scale to OpenGL (depends on depth buffer bitdepth)
  1199. // Zero depth bits may be returned if using the packed depth stencil format. Assume 24bit in that case
  1200. int depthBits = Min(impl_->depthBits_, 23);
  1201. if (!depthBits)
  1202. depthBits = 23;
  1203. float adjustedConstantBias = constantBias * (float)(1 << (depthBits - 1));
  1204. float adjustedSlopeScaledBias = slopeScaledBias + 1.0f;
  1205. glEnable(GL_POLYGON_OFFSET_FILL);
  1206. glEnable(GL_POLYGON_OFFSET_LINE);
  1207. glPolygonOffset(adjustedSlopeScaledBias, adjustedConstantBias);
  1208. }
  1209. else
  1210. {
  1211. glDisable(GL_POLYGON_OFFSET_FILL);
  1212. glDisable(GL_POLYGON_OFFSET_LINE);
  1213. }
  1214. constantDepthBias_ = constantBias;
  1215. slopeScaledDepthBias_ = slopeScaledBias;
  1216. }
  1217. }
  1218. void Graphics::SetDepthTest(CompareMode mode)
  1219. {
  1220. if (mode != depthTestMode_)
  1221. {
  1222. glDepthFunc(glCmpFunc[mode]);
  1223. depthTestMode_ = mode;
  1224. }
  1225. }
  1226. void Graphics::SetDepthWrite(bool enable)
  1227. {
  1228. if (enable != depthWrite_)
  1229. {
  1230. glDepthMask(enable ? GL_TRUE : GL_FALSE);
  1231. depthWrite_ = enable;
  1232. }
  1233. }
  1234. void Graphics::SetFillMode(FillMode mode)
  1235. {
  1236. if (mode != fillMode_)
  1237. {
  1238. glPolygonMode(GL_FRONT_AND_BACK, mode == FILL_SOLID ? GL_FILL : GL_LINE);
  1239. fillMode_ = mode;
  1240. }
  1241. }
  1242. void Graphics::SetScissorTest(bool enable, const Rect& rect, bool borderInclusive)
  1243. {
  1244. // During some light rendering loops, a full rect is toggled on/off repeatedly.
  1245. // Disable scissor in that case to reduce state changes
  1246. if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.x_ >= 1.0f && rect.max_.y_ >= 1.0f)
  1247. enable = false;
  1248. if (enable)
  1249. {
  1250. IntVector2 rtSize(GetRenderTargetDimensions());
  1251. IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
  1252. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1253. IntRect intRect;
  1254. int expand = borderInclusive ? 1 : 0;
  1255. intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
  1256. intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
  1257. intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
  1258. intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
  1259. if (intRect.right_ == intRect.left_)
  1260. intRect.right_++;
  1261. if (intRect.bottom_ == intRect.top_)
  1262. intRect.bottom_++;
  1263. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1264. enable = false;
  1265. if (enable && scissorRect_ != intRect)
  1266. {
  1267. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1268. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
  1269. scissorRect_ = intRect;
  1270. }
  1271. }
  1272. else
  1273. scissorRect_ = IntRect::ZERO;
  1274. if (enable != scissorTest_)
  1275. {
  1276. if (enable)
  1277. glEnable(GL_SCISSOR_TEST);
  1278. else
  1279. glDisable(GL_SCISSOR_TEST);
  1280. scissorTest_ = enable;
  1281. }
  1282. }
  1283. void Graphics::SetScissorTest(bool enable, const IntRect& rect)
  1284. {
  1285. IntVector2 rtSize(GetRenderTargetDimensions());
  1286. IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
  1287. IntVector2 viewPos(viewport_.left_, viewport_.top_);
  1288. // Full scissor is same as disabling the test
  1289. if (rect.left_ <= 0 && rect.right_ >= viewSize.x_ && rect.top_ <= 0 && rect.bottom_ >= viewSize.y_)
  1290. enable = false;
  1291. if (enable)
  1292. {
  1293. IntRect intRect;
  1294. intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
  1295. intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
  1296. intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
  1297. intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
  1298. if (intRect.right_ == intRect.left_)
  1299. intRect.right_++;
  1300. if (intRect.bottom_ == intRect.top_)
  1301. intRect.bottom_++;
  1302. if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
  1303. enable = false;
  1304. if (enable && scissorRect_ != intRect)
  1305. {
  1306. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  1307. glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
  1308. scissorRect_ = intRect;
  1309. }
  1310. }
  1311. else
  1312. scissorRect_ = IntRect::ZERO;
  1313. if (enable != scissorTest_)
  1314. {
  1315. if (enable)
  1316. glEnable(GL_SCISSOR_TEST);
  1317. else
  1318. glDisable(GL_SCISSOR_TEST);
  1319. scissorTest_ = enable;
  1320. }
  1321. }
  1322. void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
  1323. {
  1324. }
  1325. void Graphics::ResetStreamFrequencies()
  1326. {
  1327. }
  1328. void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned stencilMask)
  1329. {
  1330. if (enable != stencilTest_)
  1331. {
  1332. if (enable)
  1333. glEnable(GL_STENCIL_TEST);
  1334. else
  1335. glDisable(GL_STENCIL_TEST);
  1336. stencilTest_ = enable;
  1337. }
  1338. if (enable)
  1339. {
  1340. if (mode != stencilTestMode_ || stencilRef != stencilRef_ || stencilMask != stencilMask_)
  1341. {
  1342. glStencilFunc(glCmpFunc[mode], stencilRef, stencilMask);
  1343. stencilTestMode_ = mode;
  1344. stencilRef_ = stencilRef;
  1345. stencilMask_ = stencilMask;
  1346. }
  1347. if (pass != stencilPass_ || fail != stencilFail_ || zFail != stencilZFail_)
  1348. {
  1349. glStencilOp(glStencilOps[fail], glStencilOps[zFail], glStencilOps[pass]);
  1350. stencilPass_ = pass;
  1351. stencilFail_ = fail;
  1352. stencilZFail_ = zFail;
  1353. }
  1354. }
  1355. }
  1356. void Graphics::SetForceSM2(bool enable)
  1357. {
  1358. }
  1359. void Graphics::SetForceFallback(bool enable)
  1360. {
  1361. }
  1362. bool Graphics::IsInitialized() const
  1363. {
  1364. return impl_->window_ != 0;
  1365. }
  1366. void* Graphics::GetWindowHandle() const
  1367. {
  1368. return impl_->window_;
  1369. }
  1370. PODVector<IntVector2> Graphics::GetResolutions() const
  1371. {
  1372. static const unsigned MAX_MODES = 256;
  1373. GLFWvidmode modes[MAX_MODES];
  1374. unsigned count = glfwGetVideoModes(modes, MAX_MODES);
  1375. PODVector<IntVector2> ret;
  1376. for (unsigned i = 0; i < count; ++i)
  1377. {
  1378. int width = modes[i].width;
  1379. int height = modes[i].height;
  1380. // Store mode if unique
  1381. bool unique = true;
  1382. for (unsigned j = 0; j < ret.Size(); ++i)
  1383. {
  1384. if (ret[j].x_ == width && ret[j].y_ == height)
  1385. {
  1386. unique = false;
  1387. break;
  1388. }
  1389. }
  1390. if (unique)
  1391. ret.Push(IntVector2(width, height));
  1392. }
  1393. return ret;
  1394. }
  1395. PODVector<int> Graphics::GetMultiSampleLevels() const
  1396. {
  1397. PODVector<int> ret;
  1398. // No multisampling always supported
  1399. ret.Push(1);
  1400. /// \todo Implement properly, if possible
  1401. return ret;
  1402. }
  1403. VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
  1404. {
  1405. return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
  1406. }
  1407. TextureUnit Graphics::GetTextureUnit(const String& name)
  1408. {
  1409. Map<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
  1410. if (i != textureUnits_.End())
  1411. return i->second_;
  1412. else
  1413. return MAX_TEXTURE_UNITS;
  1414. }
  1415. const String& Graphics::GetTextureUnitName(TextureUnit unit)
  1416. {
  1417. for (Map<String, TextureUnit>::Iterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
  1418. {
  1419. if (i->second_ == unit)
  1420. return i->first_;
  1421. }
  1422. return noParameter;
  1423. }
  1424. Texture* Graphics::GetTexture(unsigned index) const
  1425. {
  1426. return index < MAX_TEXTURE_UNITS ? textures_[index] : 0;
  1427. }
  1428. RenderSurface* Graphics::GetRenderTarget(unsigned index) const
  1429. {
  1430. return index < MAX_RENDERTARGETS ? renderTargets_[index] : 0;
  1431. }
  1432. IntVector2 Graphics::GetRenderTargetDimensions() const
  1433. {
  1434. int width, height;
  1435. if (renderTargets_[0])
  1436. {
  1437. width = renderTargets_[0]->GetWidth();
  1438. height = renderTargets_[0]->GetHeight();
  1439. }
  1440. else if (depthStencil_)
  1441. {
  1442. width = depthStencil_->GetWidth();
  1443. height = depthStencil_->GetHeight();
  1444. }
  1445. else
  1446. {
  1447. width = width_;
  1448. height = height_;
  1449. }
  1450. return IntVector2(width, height);
  1451. }
  1452. void Graphics::AddGPUObject(GPUObject* object)
  1453. {
  1454. gpuObjects_.Push(object);
  1455. }
  1456. void Graphics::RemoveGPUObject(GPUObject* object)
  1457. {
  1458. Vector<GPUObject*>::Iterator i = gpuObjects_.Find(object);
  1459. if (i != gpuObjects_.End())
  1460. gpuObjects_.Erase(i);
  1461. }
  1462. void* Graphics::ReserveDiscardLockBuffer(unsigned size)
  1463. {
  1464. // First check for a free buffer that is large enough
  1465. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1466. {
  1467. if (!i->reserved_ && i->size_ >= size)
  1468. {
  1469. i->reserved_ = true;
  1470. return i->data_.Get();
  1471. }
  1472. }
  1473. // Then check if a free buffer can be resized
  1474. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1475. {
  1476. if (!i->reserved_)
  1477. {
  1478. i->data_ = new unsigned char[size];
  1479. i->size_ = size;
  1480. i->reserved_ = true;
  1481. return i->data_.Get();
  1482. }
  1483. }
  1484. // Finally allocate a new buffer
  1485. DiscardLockBuffer newBuffer;
  1486. newBuffer.data_ = new unsigned char[size];
  1487. newBuffer.size_ = size;
  1488. newBuffer.reserved_ = true;
  1489. discardLockBuffers_.Push(newBuffer);
  1490. return newBuffer.data_.Get();
  1491. }
  1492. void Graphics::FreeDiscardLockBuffer(void* buffer)
  1493. {
  1494. for (Vector<DiscardLockBuffer>::Iterator i = discardLockBuffers_.Begin(); i != discardLockBuffers_.End(); ++i)
  1495. {
  1496. if (i->reserved_ && i->data_.Get() == buffer)
  1497. {
  1498. i->reserved_ = false;
  1499. return;
  1500. }
  1501. }
  1502. LOGWARNING("Reserved discard lock buffer " + ToStringHex((unsigned)buffer) + " not found");
  1503. }
  1504. unsigned Graphics::GetAlphaFormat()
  1505. {
  1506. return GL_ALPHA;
  1507. }
  1508. unsigned Graphics::GetLuminanceFormat()
  1509. {
  1510. return GL_LUMINANCE;
  1511. }
  1512. unsigned Graphics::GetLuminanceAlphaFormat()
  1513. {
  1514. return GL_LUMINANCE_ALPHA;
  1515. }
  1516. unsigned Graphics::GetRGBFormat()
  1517. {
  1518. return GL_RGB;
  1519. }
  1520. unsigned Graphics::GetRGBAFormat()
  1521. {
  1522. return GL_RGBA;
  1523. }
  1524. unsigned Graphics::GetDepthFormat()
  1525. {
  1526. return GL_DEPTH_COMPONENT24;
  1527. }
  1528. unsigned Graphics::GetDepthStencilFormat()
  1529. {
  1530. return GL_DEPTH24_STENCIL8_EXT;
  1531. }
  1532. void Graphics::ResetCachedState()
  1533. {
  1534. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  1535. {
  1536. vertexBuffers_[i] = 0;
  1537. elementMasks_[i] = 0;
  1538. }
  1539. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  1540. {
  1541. textures_[i] = 0;
  1542. textureTypes_[i] = 0;
  1543. }
  1544. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1545. renderTargets_[i] = 0;
  1546. depthStencil_ = 0;
  1547. viewport_ = IntRect(0, 0, 0, 0);
  1548. indexBuffer_ = 0;
  1549. vertexShader_ = 0;
  1550. pixelShader_ = 0;
  1551. shaderProgram_ = 0;
  1552. blendMode_ = BLEND_REPLACE;
  1553. alphaTest_ = false;
  1554. alphaTestMode_ = CMP_ALWAYS;
  1555. alphaRef_ = 0.0f;
  1556. textureAnisotropy_ = 1;
  1557. colorWrite_ = true;
  1558. cullMode_ = CULL_NONE;
  1559. constantDepthBias_ = 0.0f;
  1560. slopeScaledDepthBias_ = 0.0f;
  1561. depthTestMode_ = CMP_ALWAYS;
  1562. depthWrite_ = true;
  1563. fillMode_ = FILL_SOLID;
  1564. scissorTest_ = false;
  1565. scissorRect_ = IntRect::ZERO;
  1566. stencilTest_ = false;
  1567. stencilTestMode_ = CMP_ALWAYS;
  1568. stencilPass_ = OP_KEEP;
  1569. stencilFail_ = OP_KEEP;
  1570. stencilZFail_ = OP_KEEP;
  1571. stencilRef_ = 0;
  1572. stencilMask_ = M_MAX_UNSIGNED;
  1573. impl_->activeTexture_ = 0;
  1574. impl_->drawBuffers_ = M_MAX_UNSIGNED;
  1575. impl_->enabledAttributes_ = 0;
  1576. impl_->fboBound_ = false;
  1577. }
  1578. void Graphics::SetDrawBuffers()
  1579. {
  1580. // Calculate the bit combination of non-zero color render targets to first check if the combination changed
  1581. unsigned newDrawBuffers = 0;
  1582. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1583. {
  1584. if (renderTargets_[i])
  1585. newDrawBuffers |= 1 << i;
  1586. }
  1587. if (newDrawBuffers == impl_->drawBuffers_)
  1588. return;
  1589. // Check for no color render targets (depth rendering only)
  1590. if (!newDrawBuffers)
  1591. glDrawBuffer(GL_NONE);
  1592. else
  1593. {
  1594. int drawBufferIds[4];
  1595. unsigned drawBufferCount = 0;
  1596. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  1597. {
  1598. if (renderTargets_[i])
  1599. drawBufferIds[drawBufferCount++] = GL_COLOR_ATTACHMENT0_EXT + i;
  1600. }
  1601. glDrawBuffers(drawBufferCount, (const GLenum*)drawBufferIds);
  1602. }
  1603. glReadBuffer(GL_NONE);
  1604. }
  1605. void Graphics::Release()
  1606. {
  1607. if (!impl_->window_)
  1608. return;
  1609. depthTextures_.Clear();
  1610. // If GPU objects exist ie. it's a context delete/recreate, not Close(), tell them to save and release themselves
  1611. for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
  1612. (*i)->OnDeviceLost();
  1613. if (impl_->fbo_)
  1614. {
  1615. glDeleteFramebuffersEXT(1, &impl_->fbo_);
  1616. impl_->fbo_ = 0;
  1617. }
  1618. // When the new context is initialized, it will have default state again
  1619. ResetCachedState();
  1620. ClearParameterSources();
  1621. {
  1622. MutexLock lock(GetStaticMutex());
  1623. SetWindowContext(impl_->window_, 0);
  1624. glfwCloseWindow(impl_->window_);
  1625. impl_->window_ = 0;
  1626. }
  1627. }
  1628. void Graphics::SetTextureUnitMappings()
  1629. {
  1630. textureUnits_["DiffMap"] = TU_DIFFUSE;
  1631. textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
  1632. textureUnits_["NormalMap"] = TU_NORMAL;
  1633. textureUnits_["EmissiveMap"] = TU_EMISSIVE;
  1634. textureUnits_["DetailMap"] = TU_DETAIL;
  1635. textureUnits_["EnvironmentMap"] = TU_ENVIRONMENT;
  1636. textureUnits_["EnvironmentCubeMap"] = TU_ENVIRONMENT;
  1637. textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
  1638. textureUnits_["LightSpotMap"] = TU_LIGHTSHAPE;
  1639. textureUnits_["LightCubeMap"] = TU_LIGHTSHAPE;
  1640. textureUnits_["ShadowMap"] = TU_SHADOWMAP;
  1641. textureUnits_["FaceSelectCubeMap"] = TU_FACESELECT;
  1642. textureUnits_["IndirectionCubeMap"] = TU_INDIRECTION;
  1643. }
  1644. void RegisterGraphicsLibrary(Context* context)
  1645. {
  1646. Animation::RegisterObject(context);
  1647. Material::RegisterObject(context);
  1648. Model::RegisterObject(context);
  1649. Shader::RegisterObject(context);
  1650. Technique::RegisterObject(context);
  1651. Texture2D::RegisterObject(context);
  1652. TextureCube::RegisterObject(context);
  1653. Camera::RegisterObject(context);
  1654. Drawable::RegisterObject(context);
  1655. Light::RegisterObject(context);
  1656. StaticModel::RegisterObject(context);
  1657. Skybox::RegisterObject(context);
  1658. AnimatedModel::RegisterObject(context);
  1659. AnimationController::RegisterObject(context);
  1660. BillboardSet::RegisterObject(context);
  1661. ParticleEmitter::RegisterObject(context);
  1662. DebugRenderer::RegisterObject(context);
  1663. Octree::RegisterObject(context);
  1664. Zone::RegisterObject(context);
  1665. }