OpenGL.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "common/config.h"
  22. #include "OpenGL.h"
  23. #include "Shader.h"
  24. #include "Canvas.h"
  25. #include "common/Exception.h"
  26. // C++
  27. #include <algorithm>
  28. #include <limits>
  29. // C
  30. #include <cstring>
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. namespace opengl
  36. {
  37. OpenGL::OpenGL()
  38. : stats()
  39. , contextInitialized(false)
  40. , maxAnisotropy(1.0f)
  41. , maxTextureSize(0)
  42. , maxRenderTargets(0)
  43. , vendor(VENDOR_UNKNOWN)
  44. , state()
  45. {
  46. matrices.transform.reserve(10);
  47. matrices.projection.reserve(2);
  48. }
  49. void OpenGL::initContext()
  50. {
  51. if (contextInitialized)
  52. return;
  53. initOpenGLFunctions();
  54. initVendor();
  55. initMatrices();
  56. // Store the current color so we don't have to get it through GL later.
  57. GLfloat glcolor[4];
  58. glGetFloatv(GL_CURRENT_COLOR, glcolor);
  59. state.color.r = glcolor[0] * 255;
  60. state.color.g = glcolor[1] * 255;
  61. state.color.b = glcolor[2] * 255;
  62. state.color.a = glcolor[3] * 255;
  63. // Same with the current clear color.
  64. glGetFloatv(GL_COLOR_CLEAR_VALUE, glcolor);
  65. state.clearColor.r = glcolor[0] * 255;
  66. state.clearColor.g = glcolor[1] * 255;
  67. state.clearColor.b = glcolor[2] * 255;
  68. state.clearColor.a = glcolor[3] * 255;
  69. // Get the current viewport.
  70. glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
  71. // And the current scissor - but we need to compensate for GL scissors
  72. // starting at the bottom left instead of top left.
  73. glGetIntegerv(GL_SCISSOR_BOX, (GLint *) &state.scissor.x);
  74. state.scissor.y = state.viewport.h - (state.scissor.y + state.scissor.h);
  75. // Initialize multiple texture unit support for shaders, if available.
  76. state.textureUnits.clear();
  77. if (Shader::isSupported())
  78. {
  79. GLint maxtextureunits;
  80. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureunits);
  81. state.textureUnits.resize(maxtextureunits, 0);
  82. GLenum curgltextureunit;
  83. glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *) &curgltextureunit);
  84. state.curTextureUnit = (int) curgltextureunit - GL_TEXTURE0;
  85. // Retrieve currently bound textures for each texture unit.
  86. for (size_t i = 0; i < state.textureUnits.size(); i++)
  87. {
  88. glActiveTexture(GL_TEXTURE0 + i);
  89. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &state.textureUnits[i]);
  90. }
  91. glActiveTexture(curgltextureunit);
  92. }
  93. else
  94. {
  95. // Multitexturing not supported, so we only have 1 texture unit.
  96. state.textureUnits.resize(1, 0);
  97. state.curTextureUnit = 0;
  98. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &state.textureUnits[0]);
  99. }
  100. BlendState blend = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
  101. setBlendState(blend);
  102. initMaxValues();
  103. createDefaultTexture();
  104. state.lastPseudoInstanceID = -1;
  105. // Invalidate the cached matrices by setting some elements to NaN.
  106. float nan = std::numeric_limits<float>::quiet_NaN();
  107. state.lastProjectionMatrix.setTranslation(nan, nan);
  108. state.lastTransformMatrix.setTranslation(nan, nan);
  109. glMatrixMode(GL_MODELVIEW);
  110. contextInitialized = true;
  111. }
  112. void OpenGL::deInitContext()
  113. {
  114. if (!contextInitialized)
  115. return;
  116. contextInitialized = false;
  117. }
  118. void OpenGL::initVendor()
  119. {
  120. const char *vstr = (const char *) glGetString(GL_VENDOR);
  121. if (!vstr)
  122. {
  123. vendor = VENDOR_UNKNOWN;
  124. return;
  125. }
  126. // http://feedback.wildfiregames.com/report/opengl/feature/GL_VENDOR
  127. if (strstr(vstr, "ATI Technologies"))
  128. vendor = VENDOR_ATI_AMD;
  129. else if (strstr(vstr, "NVIDIA"))
  130. vendor = VENDOR_NVIDIA;
  131. else if (strstr(vstr, "Intel"))
  132. vendor = VENDOR_INTEL;
  133. else if (strstr(vstr, "Mesa"))
  134. vendor = VENDOR_MESA_SOFT;
  135. else if (strstr(vstr, "Apple Computer"))
  136. vendor = VENDOR_APPLE;
  137. else if (strstr(vstr, "Microsoft"))
  138. vendor = VENDOR_MICROSOFT;
  139. else
  140. vendor = VENDOR_UNKNOWN;
  141. }
  142. void OpenGL::initOpenGLFunctions()
  143. {
  144. // The functionality of the core and ARB VBOs are identical, so we can
  145. // assign the pointers of the core functions to the names of the ARB
  146. // functions, if the latter isn't supported but the former is.
  147. if (GLEE_VERSION_1_5 && !GLEE_ARB_vertex_buffer_object)
  148. {
  149. glBindBufferARB = (GLEEPFNGLBINDBUFFERARBPROC) glBindBuffer;
  150. glBufferDataARB = (GLEEPFNGLBUFFERDATAARBPROC) glBufferData;
  151. glBufferSubDataARB = (GLEEPFNGLBUFFERSUBDATAARBPROC) glBufferSubData;
  152. glDeleteBuffersARB = (GLEEPFNGLDELETEBUFFERSARBPROC) glDeleteBuffers;
  153. glGenBuffersARB = (GLEEPFNGLGENBUFFERSARBPROC) glGenBuffers;
  154. glGetBufferParameterivARB = (GLEEPFNGLGETBUFFERPARAMETERIVARBPROC) glGetBufferParameteriv;
  155. glGetBufferPointervARB = (GLEEPFNGLGETBUFFERPOINTERVARBPROC) glGetBufferPointerv;
  156. glGetBufferSubDataARB = (GLEEPFNGLGETBUFFERSUBDATAARBPROC) glGetBufferSubData;
  157. glIsBufferARB = (GLEEPFNGLISBUFFERARBPROC) glIsBuffer;
  158. glMapBufferARB = (GLEEPFNGLMAPBUFFERARBPROC) glMapBuffer;
  159. glUnmapBufferARB = (GLEEPFNGLUNMAPBUFFERARBPROC) glUnmapBuffer;
  160. }
  161. // Same deal for compressed textures.
  162. if (GLEE_VERSION_1_3 && !GLEE_ARB_texture_compression)
  163. {
  164. glCompressedTexImage2DARB = (GLEEPFNGLCOMPRESSEDTEXIMAGE2DARBPROC) glCompressedTexImage2D;
  165. glCompressedTexSubImage2DARB = (GLEEPFNGLCOMPRESSEDTEXSUBIMAGE2DARBPROC) glCompressedTexSubImage2D;
  166. glGetCompressedTexImageARB = (GLEEPFNGLGETCOMPRESSEDTEXIMAGEARBPROC) glGetCompressedTexImage;
  167. }
  168. }
  169. void OpenGL::initMaxValues()
  170. {
  171. // We'll need this value to clamp anisotropy.
  172. if (GLEE_EXT_texture_filter_anisotropic)
  173. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
  174. else
  175. maxAnisotropy = 1.0f;
  176. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
  177. if (Canvas::isSupported() && (GLEE_VERSION_2_0 || GLEE_ARB_draw_buffers))
  178. {
  179. int maxattachments = 0;
  180. glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxattachments);
  181. int maxdrawbuffers = 0;
  182. glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxdrawbuffers);
  183. maxRenderTargets = std::min(maxattachments, maxdrawbuffers);
  184. }
  185. else
  186. maxRenderTargets = 0;
  187. }
  188. void OpenGL::initMatrices()
  189. {
  190. matrices.transform.clear();
  191. matrices.projection.clear();
  192. matrices.transform.push_back(Matrix());
  193. matrices.projection.push_back(Matrix());
  194. }
  195. void OpenGL::createDefaultTexture()
  196. {
  197. // Set the 'default' texture (id 0) as a repeating white pixel. Otherwise,
  198. // texture2D calls inside a shader would return black when drawing graphics
  199. // primitives, which would create the need to use different "passthrough"
  200. // shaders for untextured primitives vs images.
  201. GLuint curtexture = state.textureUnits[state.curTextureUnit];
  202. bindTexture(0);
  203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  204. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  205. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  206. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  207. GLubyte pix = 255;
  208. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &pix);
  209. bindTexture(curtexture);
  210. }
  211. void OpenGL::pushTransform()
  212. {
  213. matrices.transform.push_back(matrices.transform.back());
  214. }
  215. void OpenGL::popTransform()
  216. {
  217. matrices.transform.pop_back();
  218. }
  219. Matrix &OpenGL::getTransform()
  220. {
  221. return matrices.transform.back();
  222. }
  223. void OpenGL::prepareDraw()
  224. {
  225. Shader *shader = Shader::current;
  226. if (shader != nullptr)
  227. {
  228. // Make sure the active shader has the correct values for its
  229. // love-provided uniforms.
  230. shader->checkSetScreenParams();
  231. // Make sure the Instance ID variable is up-to-date when
  232. // pseudo-instancing is used.
  233. if (state.lastPseudoInstanceID != 0 && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID))
  234. {
  235. glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, 0.0f);
  236. state.lastPseudoInstanceID = 0;
  237. }
  238. // We need to make sure antialiased Canvases are properly resolved
  239. // before sampling from their textures in a shader.
  240. // This is kind of a big hack. :(
  241. for (auto &r : shader->getBoundRetainables())
  242. {
  243. // Even bigger hack! D:
  244. Canvas *canvas = dynamic_cast<Canvas *>(r.second);
  245. if (canvas != nullptr)
  246. canvas->resolveMSAA();
  247. }
  248. }
  249. const float *curproj = matrices.projection.back().getElements();
  250. const float *lastproj = state.lastProjectionMatrix.getElements();
  251. // We only need to re-upload the projection matrix if it's changed.
  252. if (memcmp(curproj, lastproj, sizeof(float) * 16) != 0)
  253. {
  254. glMatrixMode(GL_PROJECTION);
  255. glLoadMatrixf(curproj);
  256. glMatrixMode(GL_MODELVIEW);
  257. state.lastProjectionMatrix = matrices.projection.back();
  258. }
  259. const float *curxform = matrices.transform.back().getElements();
  260. const float *lastxform = state.lastTransformMatrix.getElements();
  261. // Same with the transform matrix.
  262. if (memcmp(curxform, lastxform, sizeof(float) * 16) != 0)
  263. {
  264. glLoadMatrixf(curxform);
  265. state.lastTransformMatrix = matrices.transform.back();
  266. }
  267. }
  268. void OpenGL::drawArrays(GLenum mode, GLint first, GLsizei count)
  269. {
  270. glDrawArrays(mode, first, count);
  271. ++stats.drawCalls;
  272. }
  273. void OpenGL::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
  274. {
  275. glDrawElements(mode, count, type, indices);
  276. ++stats.drawCalls;
  277. }
  278. void OpenGL::drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
  279. {
  280. Shader *shader = Shader::current;
  281. if (GLEE_ARB_draw_instanced)
  282. glDrawArraysInstancedARB(mode, first, count, primcount);
  283. else
  284. {
  285. bool shaderHasID = shader && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID);
  286. // Pseudo-instancing fallback.
  287. for (int i = 0; i < primcount; i++)
  288. {
  289. if (shaderHasID)
  290. glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, (GLfloat) i);
  291. glDrawArrays(mode, first, count);
  292. }
  293. if (shaderHasID)
  294. state.lastPseudoInstanceID = primcount - 1;
  295. }
  296. ++stats.drawCalls;
  297. }
  298. void OpenGL::drawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount)
  299. {
  300. Shader *shader = Shader::current;
  301. if (GLEE_ARB_draw_instanced)
  302. glDrawElementsInstancedARB(mode, count, type, indices, primcount);
  303. else
  304. {
  305. bool shaderHasID = shader && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID);
  306. // Pseudo-instancing fallback.
  307. for (int i = 0; i < primcount; i++)
  308. {
  309. if (shaderHasID)
  310. glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, (GLfloat) i);
  311. glDrawElements(mode, count, type, indices);
  312. }
  313. if (shaderHasID)
  314. state.lastPseudoInstanceID = primcount - 1;
  315. }
  316. ++stats.drawCalls;
  317. }
  318. void OpenGL::setColor(const Color &c)
  319. {
  320. glColor4ubv(&c.r);
  321. state.color = c;
  322. }
  323. Color OpenGL::getColor() const
  324. {
  325. return state.color;
  326. }
  327. void OpenGL::setClearColor(const Color &c)
  328. {
  329. glClearColor(c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f);
  330. state.clearColor = c;
  331. }
  332. Color OpenGL::getClearColor() const
  333. {
  334. return state.clearColor;
  335. }
  336. void OpenGL::setViewport(const OpenGL::Viewport &v)
  337. {
  338. glViewport(v.x, v.y, v.w, v.h);
  339. state.viewport = v;
  340. // glScissor starts from the lower left, so we compensate when setting the
  341. // scissor. When the viewport is changed, we need to manually update the
  342. // scissor again.
  343. setScissor(state.scissor);
  344. }
  345. OpenGL::Viewport OpenGL::getViewport() const
  346. {
  347. return state.viewport;
  348. }
  349. void OpenGL::setScissor(const OpenGL::Viewport &v)
  350. {
  351. if (Canvas::current)
  352. glScissor(v.x, v.y, v.w, v.h);
  353. else
  354. {
  355. // With no Canvas active, we need to compensate for glScissor starting
  356. // from the lower left of the viewport instead of the top left.
  357. glScissor(v.x, state.viewport.h - (v.y + v.h), v.w, v.h);
  358. }
  359. state.scissor = v;
  360. }
  361. OpenGL::Viewport OpenGL::getScissor() const
  362. {
  363. return state.scissor;
  364. }
  365. void OpenGL::setBlendState(const BlendState &blend)
  366. {
  367. if (GLEE_VERSION_1_4 || GLEE_ARB_imaging)
  368. glBlendEquation(blend.func);
  369. else if (GLEE_EXT_blend_minmax && GLEE_EXT_blend_subtract)
  370. glBlendEquationEXT(blend.func);
  371. else
  372. {
  373. if (blend.func == GL_FUNC_REVERSE_SUBTRACT)
  374. throw love::Exception("This graphics card does not support the subtractive blend mode!");
  375. // GL_FUNC_ADD is the default even without access to glBlendEquation, so that'll still work.
  376. }
  377. if (blend.srcRGB == blend.srcA && blend.dstRGB == blend.dstA)
  378. glBlendFunc(blend.srcRGB, blend.dstRGB);
  379. else
  380. {
  381. if (GLEE_VERSION_1_4)
  382. glBlendFuncSeparate(blend.srcRGB, blend.dstRGB, blend.srcA, blend.dstA);
  383. else if (GLEE_EXT_blend_func_separate)
  384. glBlendFuncSeparateEXT(blend.srcRGB, blend.dstRGB, blend.srcA, blend.dstA);
  385. else
  386. throw love::Exception("This graphics card does not support separated rgb and alpha blend functions!");
  387. }
  388. state.blend = blend;
  389. }
  390. OpenGL::BlendState OpenGL::getBlendState() const
  391. {
  392. return state.blend;
  393. }
  394. void OpenGL::setTextureUnit(int textureunit)
  395. {
  396. if (textureunit < 0 || (size_t) textureunit >= state.textureUnits.size())
  397. throw love::Exception("Invalid texture unit index (%d).", textureunit);
  398. if (textureunit != state.curTextureUnit)
  399. {
  400. if (state.textureUnits.size() > 1)
  401. glActiveTexture(GL_TEXTURE0 + textureunit);
  402. else
  403. throw love::Exception("Multitexturing is not supported.");
  404. }
  405. state.curTextureUnit = textureunit;
  406. }
  407. void OpenGL::bindTexture(GLuint texture)
  408. {
  409. if (texture != state.textureUnits[state.curTextureUnit])
  410. {
  411. state.textureUnits[state.curTextureUnit] = texture;
  412. glBindTexture(GL_TEXTURE_2D, texture);
  413. }
  414. }
  415. void OpenGL::bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev)
  416. {
  417. if (textureunit < 0 || (size_t) textureunit >= state.textureUnits.size())
  418. throw love::Exception("Invalid texture unit index.");
  419. if (texture != state.textureUnits[textureunit])
  420. {
  421. int oldtextureunit = state.curTextureUnit;
  422. setTextureUnit(textureunit);
  423. state.textureUnits[textureunit] = texture;
  424. glBindTexture(GL_TEXTURE_2D, texture);
  425. if (restoreprev)
  426. setTextureUnit(oldtextureunit);
  427. }
  428. }
  429. void OpenGL::deleteTexture(GLuint texture)
  430. {
  431. // glDeleteTextures binds texture 0 to all texture units the deleted texture
  432. // was bound to before deletion.
  433. for (GLuint &texid : state.textureUnits)
  434. {
  435. if (texid == texture)
  436. texid = 0;
  437. }
  438. glDeleteTextures(1, &texture);
  439. }
  440. float OpenGL::setTextureFilter(graphics::Texture::Filter &f)
  441. {
  442. GLint gmin, gmag;
  443. if (f.mipmap == Texture::FILTER_NONE)
  444. {
  445. if (f.min == Texture::FILTER_NEAREST)
  446. gmin = GL_NEAREST;
  447. else // f.min == Texture::FILTER_LINEAR
  448. gmin = GL_LINEAR;
  449. }
  450. else
  451. {
  452. if (f.min == Texture::FILTER_NEAREST && f.mipmap == Texture::FILTER_NEAREST)
  453. gmin = GL_NEAREST_MIPMAP_NEAREST;
  454. else if (f.min == Texture::FILTER_NEAREST && f.mipmap == Texture::FILTER_LINEAR)
  455. gmin = GL_NEAREST_MIPMAP_LINEAR;
  456. else if (f.min == Texture::FILTER_LINEAR && f.mipmap == Texture::FILTER_NEAREST)
  457. gmin = GL_LINEAR_MIPMAP_NEAREST;
  458. else if (f.min == Texture::FILTER_LINEAR && f.mipmap == Texture::FILTER_LINEAR)
  459. gmin = GL_LINEAR_MIPMAP_LINEAR;
  460. else
  461. gmin = GL_LINEAR;
  462. }
  463. switch (f.mag)
  464. {
  465. case Texture::FILTER_NEAREST:
  466. gmag = GL_NEAREST;
  467. break;
  468. case Texture::FILTER_LINEAR:
  469. default:
  470. gmag = GL_LINEAR;
  471. break;
  472. }
  473. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin);
  474. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
  475. if (GLEE_EXT_texture_filter_anisotropic)
  476. {
  477. f.anisotropy = std::min(std::max(f.anisotropy, 1.0f), maxAnisotropy);
  478. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, f.anisotropy);
  479. }
  480. return f.anisotropy;
  481. }
  482. void OpenGL::setTextureWrap(const graphics::Texture::Wrap &w)
  483. {
  484. auto glWrapMode = [](Texture::WrapMode wmode) -> GLint
  485. {
  486. switch (wmode)
  487. {
  488. case Texture::WRAP_CLAMP:
  489. default:
  490. return GL_CLAMP_TO_EDGE;
  491. case Texture::WRAP_REPEAT:
  492. return GL_REPEAT;
  493. case Texture::WRAP_MIRRORED_REPEAT:
  494. return GL_MIRRORED_REPEAT;
  495. }
  496. };
  497. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glWrapMode(w.s));
  498. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glWrapMode(w.t));
  499. }
  500. int OpenGL::getMaxTextureSize() const
  501. {
  502. return maxTextureSize;
  503. }
  504. int OpenGL::getMaxRenderTargets() const
  505. {
  506. return maxRenderTargets;
  507. }
  508. void OpenGL::updateTextureMemorySize(size_t oldsize, size_t newsize)
  509. {
  510. int64 memsize = (int64) stats.textureMemory + ((int64 )newsize - (int64) oldsize);
  511. stats.textureMemory = (size_t) std::max(memsize, (int64) 0);
  512. }
  513. OpenGL::Vendor OpenGL::getVendor() const
  514. {
  515. return vendor;
  516. }
  517. const char *OpenGL::debugSeverityString(GLenum severity)
  518. {
  519. switch (severity)
  520. {
  521. case GL_DEBUG_SEVERITY_HIGH:
  522. return "high";
  523. case GL_DEBUG_SEVERITY_MEDIUM:
  524. return "medium";
  525. case GL_DEBUG_SEVERITY_LOW:
  526. return "low";
  527. default:
  528. break;
  529. }
  530. return "unknown";
  531. }
  532. const char *OpenGL::debugSourceString(GLenum source)
  533. {
  534. switch (source)
  535. {
  536. case GL_DEBUG_SOURCE_API:
  537. return "API";
  538. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  539. return "window";
  540. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  541. return "shader";
  542. case GL_DEBUG_SOURCE_THIRD_PARTY:
  543. return "external";
  544. case GL_DEBUG_SOURCE_APPLICATION:
  545. return "LOVE";
  546. case GL_DEBUG_SOURCE_OTHER:
  547. return "other";
  548. default:
  549. break;
  550. }
  551. return "unknown";
  552. }
  553. const char *OpenGL::debugTypeString(GLenum type)
  554. {
  555. switch (type)
  556. {
  557. case GL_DEBUG_TYPE_ERROR:
  558. return "error";
  559. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  560. return "deprecated behavior";
  561. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  562. return "undefined behavior";
  563. case GL_DEBUG_TYPE_PERFORMANCE:
  564. return "performance";
  565. case GL_DEBUG_TYPE_PORTABILITY:
  566. return "portability";
  567. case GL_DEBUG_TYPE_OTHER:
  568. return "other";
  569. default:
  570. break;
  571. }
  572. return "unknown";
  573. }
  574. // OpenGL class instance singleton.
  575. OpenGL gl;
  576. } // opengl
  577. } // graphics
  578. } // love