OpenGL.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  1. /**
  2. * Copyright (c) 2006-2023 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 "common/Exception.h"
  25. #include "graphics/Graphics.h"
  26. #include "graphics/Buffer.h"
  27. // C++
  28. #include <algorithm>
  29. #include <limits>
  30. // C
  31. #include <cstring>
  32. #include <cstdio>
  33. // For SDL_GL_GetProcAddress.
  34. #include <SDL_video.h>
  35. #ifdef LOVE_IOS
  36. #include <SDL_syswm.h>
  37. #endif
  38. #ifdef LOVE_ANDROID
  39. #include <dlfcn.h>
  40. #endif
  41. namespace love
  42. {
  43. namespace graphics
  44. {
  45. namespace opengl
  46. {
  47. static void *LOVEGetProcAddress(const char *name)
  48. {
  49. #ifdef LOVE_ANDROID
  50. void *proc = dlsym(RTLD_DEFAULT, name);
  51. if (proc)
  52. return proc;
  53. #endif
  54. return SDL_GL_GetProcAddress(name);
  55. }
  56. OpenGL::TempDebugGroup::TempDebugGroup(const char *name)
  57. {
  58. if (isDebugEnabled())
  59. {
  60. if (GLAD_VERSION_4_3 || (GLAD_KHR_debug && !GLAD_ES_VERSION_2_0))
  61. glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, 0, (const GLchar *) name);
  62. else if (GLAD_ES_VERSION_2_0 && GLAD_KHR_debug)
  63. glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION, 0, 0, (const GLchar *) name);
  64. else if (GLAD_EXT_debug_marker)
  65. glPushGroupMarkerEXT(0, (const GLchar *) name);
  66. }
  67. }
  68. OpenGL::TempDebugGroup::~TempDebugGroup()
  69. {
  70. if (isDebugEnabled())
  71. {
  72. if (GLAD_VERSION_4_3 || (GLAD_KHR_debug && !GLAD_ES_VERSION_2_0))
  73. glPopDebugGroup();
  74. else if (GLAD_ES_VERSION_2_0 && GLAD_KHR_debug)
  75. glPopDebugGroupKHR();
  76. else if (GLAD_EXT_debug_marker)
  77. glPopGroupMarkerEXT();
  78. }
  79. }
  80. OpenGL::OpenGL()
  81. : stats()
  82. , contextInitialized(false)
  83. , pixelShaderHighpSupported(false)
  84. , baseVertexSupported(false)
  85. , maxAnisotropy(1.0f)
  86. , max2DTextureSize(0)
  87. , max3DTextureSize(0)
  88. , maxCubeTextureSize(0)
  89. , maxTextureArrayLayers(0)
  90. , maxTexelBufferSize(0)
  91. , maxShaderStorageBufferSize(0)
  92. , maxComputeWorkGroupsX(0)
  93. , maxComputeWorkGroupsY(0)
  94. , maxComputeWorkGroupsZ(0)
  95. , maxRenderTargets(1)
  96. , maxSamples(1)
  97. , maxTextureUnits(1)
  98. , maxShaderStorageBufferBindings(0)
  99. , maxPointSize(1)
  100. , coreProfile(false)
  101. , vendor(VENDOR_UNKNOWN)
  102. , state()
  103. {
  104. }
  105. bool OpenGL::initContext()
  106. {
  107. if (contextInitialized)
  108. return true;
  109. if (!gladLoadGLLoader(LOVEGetProcAddress))
  110. return false;
  111. initVendor();
  112. bugs = {};
  113. if (GLAD_ES_VERSION_3_0 && !GLAD_ES_VERSION_3_1)
  114. {
  115. const char *device = (const char *) glGetString(GL_RENDERER);
  116. if (getVendor() == VENDOR_VIVANTE && strstr(device, "Vivante GC7000UL"))
  117. bugs.brokenGLES3 = true;
  118. }
  119. if (bugs.brokenGLES3)
  120. GLAD_ES_VERSION_3_0 = false;
  121. if (GLAD_VERSION_3_2)
  122. {
  123. GLint profileMask = 0;
  124. glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profileMask);
  125. coreProfile = (profileMask & GL_CONTEXT_CORE_PROFILE_BIT);
  126. }
  127. else
  128. coreProfile = false;
  129. initOpenGLFunctions();
  130. #if defined(LOVE_WINDOWS) || defined(LOVE_LINUX)
  131. // See the comments in OpenGL.h.
  132. if (getVendor() == VENDOR_AMD)
  133. {
  134. bugs.clearRequiresDriverTextureStateUpdate = true;
  135. if (!gl.isCoreProfile() && !GLAD_ES_VERSION_2_0)
  136. bugs.generateMipmapsRequiresTexture2DEnable = true;
  137. }
  138. #endif
  139. #ifdef LOVE_WINDOWS
  140. if (getVendor() == VENDOR_INTEL && gl.isCoreProfile())
  141. {
  142. const char *device = (const char *) glGetString(GL_RENDERER);
  143. if (strstr(device, "HD Graphics 4000") || strstr(device, "HD Graphics 2500"))
  144. bugs.clientWaitSyncStalls = true;
  145. }
  146. if (getVendor() == VENDOR_INTEL)
  147. {
  148. const char *device = (const char *) glGetString(GL_RENDERER);
  149. if (strstr(device, "HD Graphics 3000") || strstr(device, "HD Graphics 2000")
  150. || !strcmp(device, "Intel(R) HD Graphics") || !strcmp(device, "Intel(R) HD Graphics Family"))
  151. {
  152. bugs.brokenSRGB = true;
  153. }
  154. }
  155. #endif
  156. #ifdef LOVE_WINDOWS
  157. if (getVendor() == VENDOR_AMD)
  158. {
  159. // Radeon drivers switched from "ATI Radeon" to "AMD Radeon" around
  160. // the 7000 series. We'll assume this bug doesn't affect those newer
  161. // GPUs / drivers.
  162. const char *device = (const char *) glGetString(GL_RENDERER);
  163. if (strstr(device, "ATI Radeon") || strstr(device, "ATI Mobility Radeon"))
  164. bugs.texStorageBreaksSubImage = true;
  165. }
  166. #endif
  167. contextInitialized = true;
  168. return true;
  169. }
  170. void OpenGL::setupContext()
  171. {
  172. if (!contextInitialized)
  173. return;
  174. initMaxValues();
  175. GLfloat glcolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  176. glVertexAttrib4fv(ATTRIB_COLOR, glcolor);
  177. GLint maxvertexattribs = 1;
  178. glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxvertexattribs);
  179. state.enabledAttribArrays = (uint32) ((1ull << uint32(maxvertexattribs)) - 1);
  180. state.instancedAttribArrays = 0;
  181. setVertexAttributes(VertexAttributes(), BufferBindings());
  182. // Get the current viewport.
  183. glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
  184. // And the current scissor - but we need to compensate for GL scissors
  185. // starting at the bottom left instead of top left.
  186. glGetIntegerv(GL_SCISSOR_BOX, (GLint *) &state.scissor.x);
  187. state.scissor.y = state.viewport.h - (state.scissor.y + state.scissor.h);
  188. if (GLAD_VERSION_1_0)
  189. glGetFloatv(GL_POINT_SIZE, &state.pointSize);
  190. else
  191. state.pointSize = 1.0f;
  192. for (int i = 0; i < 2; i++)
  193. state.boundFramebuffers[i] = std::numeric_limits<GLuint>::max();
  194. bindFramebuffer(FRAMEBUFFER_ALL, getDefaultFBO());
  195. setEnableState(ENABLE_BLEND, state.enableState[ENABLE_BLEND]);
  196. setEnableState(ENABLE_DEPTH_TEST, state.enableState[ENABLE_DEPTH_TEST]);
  197. setEnableState(ENABLE_STENCIL_TEST, state.enableState[ENABLE_STENCIL_TEST]);
  198. setEnableState(ENABLE_SCISSOR_TEST, state.enableState[ENABLE_SCISSOR_TEST]);
  199. setEnableState(ENABLE_FACE_CULL, state.enableState[ENABLE_FACE_CULL]);
  200. if (!bugs.brokenSRGB && (GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_sRGB
  201. || GLAD_EXT_framebuffer_sRGB || GLAD_EXT_sRGB_write_control))
  202. {
  203. setEnableState(ENABLE_FRAMEBUFFER_SRGB, state.enableState[ENABLE_FRAMEBUFFER_SRGB]);
  204. }
  205. else
  206. state.enableState[ENABLE_FRAMEBUFFER_SRGB] = false;
  207. GLint faceCull = GL_BACK;
  208. glGetIntegerv(GL_CULL_FACE_MODE, &faceCull);
  209. state.faceCullMode = faceCull;
  210. for (int i = 0; i < (int) BUFFERUSAGE_MAX_ENUM; i++)
  211. {
  212. state.boundBuffers[i] = 0;
  213. if (isBufferUsageSupported((BufferUsage) i))
  214. glBindBuffer(getGLBufferType((BufferUsage) i), 0);
  215. }
  216. if (isBufferUsageSupported(BUFFERUSAGE_SHADER_STORAGE))
  217. state.boundIndexedBuffers[BUFFERUSAGE_SHADER_STORAGE].resize(maxShaderStorageBufferBindings, 0);
  218. // Initialize multiple texture unit support for shaders.
  219. for (int i = 0; i < TEXTURE_MAX_ENUM + 1; i++)
  220. {
  221. state.boundTextures[i].clear();
  222. state.boundTextures[i].resize(maxTextureUnits, 0);
  223. }
  224. for (int i = 0; i < maxTextureUnits; i++)
  225. {
  226. glActiveTexture(GL_TEXTURE0 + i);
  227. for (int j = 0; j < TEXTURE_MAX_ENUM; j++)
  228. {
  229. TextureType textype = (TextureType) j;
  230. if (isTextureTypeSupported(textype))
  231. glBindTexture(getGLTextureType(textype), 0);
  232. }
  233. }
  234. glActiveTexture(GL_TEXTURE0);
  235. state.curTextureUnit = 0;
  236. setDepthWrites(state.depthWritesEnabled);
  237. setStencilWriteMask(state.stencilWriteMask);
  238. createDefaultTexture();
  239. contextInitialized = true;
  240. #ifdef LOVE_ANDROID
  241. // This can't be done in initContext with the rest of the bug checks because
  242. // isPixelFormatSupported relies on state initialized here / after init.
  243. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  244. if (GLAD_ES_VERSION_3_0 && gfx != nullptr && !gfx->isPixelFormatSupported(PIXELFORMAT_R8_UNORM, PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_RENDERTARGET))
  245. bugs.brokenR8PixelFormat = true;
  246. #endif
  247. }
  248. void OpenGL::deInitContext()
  249. {
  250. if (!contextInitialized)
  251. return;
  252. for (int i = 0; i < TEXTURE_MAX_ENUM; i++)
  253. {
  254. for (int datatype = DATA_BASETYPE_FLOAT; datatype <= DATA_BASETYPE_UINT; datatype++)
  255. {
  256. if (state.defaultTexture[i][datatype] != 0)
  257. {
  258. gl.deleteTexture(state.defaultTexture[i][datatype]);
  259. state.defaultTexture[i][datatype] = 0;
  260. }
  261. }
  262. }
  263. contextInitialized = false;
  264. }
  265. void OpenGL::initVendor()
  266. {
  267. const char *vstr = (const char *) glGetString(GL_VENDOR);
  268. if (!vstr)
  269. {
  270. vendor = VENDOR_UNKNOWN;
  271. return;
  272. }
  273. // http://feedback.wildfiregames.com/report/opengl/feature/GL_VENDOR
  274. // http://stackoverflow.com/questions/2093594/opengl-extensions-available-on-different-android-devices
  275. // https://opengl.gpuinfo.org/displaycapability.php?name=GL_VENDOR
  276. if (strstr(vstr, "ATI Technologies") || strstr(vstr, "AMD") || strstr(vstr, "Advanced Micro Devices"))
  277. vendor = VENDOR_AMD;
  278. else if (strstr(vstr, "NVIDIA"))
  279. vendor = VENDOR_NVIDIA;
  280. else if (strstr(vstr, "Intel"))
  281. vendor = VENDOR_INTEL;
  282. else if (strstr(vstr, "Mesa"))
  283. vendor = VENDOR_MESA_SOFT;
  284. else if (strstr(vstr, "Apple Computer") || strstr(vstr, "Apple Inc."))
  285. vendor = VENDOR_APPLE;
  286. else if (strstr(vstr, "Microsoft"))
  287. vendor = VENDOR_MICROSOFT;
  288. else if (strstr(vstr, "Imagination"))
  289. vendor = VENDOR_IMGTEC;
  290. else if (strstr(vstr, "ARM"))
  291. vendor = VENDOR_ARM;
  292. else if (strstr(vstr, "Qualcomm"))
  293. vendor = VENDOR_QUALCOMM;
  294. else if (strstr(vstr, "Broadcom"))
  295. vendor = VENDOR_BROADCOM;
  296. else if (strstr(vstr, "Vivante"))
  297. vendor = VENDOR_VIVANTE;
  298. else
  299. vendor = VENDOR_UNKNOWN;
  300. }
  301. void OpenGL::initOpenGLFunctions()
  302. {
  303. // Alias extension-suffixed framebuffer functions to core versions since
  304. // there are so many different-named extensions that do the same things...
  305. if (!(GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object))
  306. {
  307. if (GLAD_VERSION_1_0 && GLAD_EXT_framebuffer_object)
  308. {
  309. fp_glBindRenderbuffer = fp_glBindRenderbufferEXT;
  310. fp_glDeleteRenderbuffers = fp_glDeleteRenderbuffersEXT;
  311. fp_glGenRenderbuffers = fp_glGenRenderbuffersEXT;
  312. fp_glRenderbufferStorage = fp_glRenderbufferStorageEXT;
  313. fp_glGetRenderbufferParameteriv = fp_glGetRenderbufferParameterivEXT;
  314. fp_glBindFramebuffer = fp_glBindFramebufferEXT;
  315. fp_glDeleteFramebuffers = fp_glDeleteFramebuffersEXT;
  316. fp_glGenFramebuffers = fp_glGenFramebuffersEXT;
  317. fp_glCheckFramebufferStatus = fp_glCheckFramebufferStatusEXT;
  318. fp_glFramebufferTexture2D = fp_glFramebufferTexture2DEXT;
  319. fp_glFramebufferTexture3D = fp_glFramebufferTexture3DEXT;
  320. fp_glFramebufferRenderbuffer = fp_glFramebufferRenderbufferEXT;
  321. fp_glGetFramebufferAttachmentParameteriv = fp_glGetFramebufferAttachmentParameterivEXT;
  322. fp_glGenerateMipmap = fp_glGenerateMipmapEXT;
  323. }
  324. if (GLAD_VERSION_1_0 && GLAD_EXT_texture_array)
  325. fp_glFramebufferTextureLayer = fp_glFramebufferTextureLayerEXT;
  326. if (GLAD_EXT_framebuffer_blit)
  327. fp_glBlitFramebuffer = fp_glBlitFramebufferEXT;
  328. else if (GLAD_ANGLE_framebuffer_blit)
  329. fp_glBlitFramebuffer = fp_glBlitFramebufferANGLE;
  330. else if (GLAD_NV_framebuffer_blit)
  331. fp_glBlitFramebuffer = fp_glBlitFramebufferNV;
  332. if (GLAD_EXT_framebuffer_multisample)
  333. fp_glRenderbufferStorageMultisample = fp_glRenderbufferStorageMultisampleEXT;
  334. else if (GLAD_APPLE_framebuffer_multisample)
  335. fp_glRenderbufferStorageMultisample = fp_glRenderbufferStorageMultisampleAPPLE;
  336. else if (GLAD_ANGLE_framebuffer_multisample)
  337. fp_glRenderbufferStorageMultisample = fp_glRenderbufferStorageMultisampleANGLE;
  338. else if (GLAD_NV_framebuffer_multisample)
  339. fp_glRenderbufferStorageMultisample = fp_glRenderbufferStorageMultisampleNV;
  340. }
  341. if (isInstancingSupported() && !(GLAD_VERSION_3_3 || GLAD_ES_VERSION_3_0))
  342. {
  343. if (GLAD_ARB_instanced_arrays)
  344. {
  345. fp_glDrawArraysInstanced = fp_glDrawArraysInstancedARB;
  346. fp_glDrawElementsInstanced = fp_glDrawElementsInstancedARB;
  347. fp_glVertexAttribDivisor = fp_glVertexAttribDivisorARB;
  348. }
  349. else if (GLAD_EXT_instanced_arrays)
  350. {
  351. fp_glDrawArraysInstanced = fp_glDrawArraysInstancedEXT;
  352. fp_glDrawElementsInstanced = fp_glDrawElementsInstancedEXT;
  353. fp_glVertexAttribDivisor = fp_glVertexAttribDivisorEXT;
  354. }
  355. else if (GLAD_ANGLE_instanced_arrays)
  356. {
  357. fp_glDrawArraysInstanced = fp_glDrawArraysInstancedANGLE;
  358. fp_glDrawElementsInstanced = fp_glDrawElementsInstancedANGLE;
  359. fp_glVertexAttribDivisor = fp_glVertexAttribDivisorANGLE;
  360. }
  361. }
  362. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0)
  363. {
  364. // The Nvidia Tegra 3 driver (used by Ouya) claims to support GL_EXT_texture_array but
  365. // segfaults if you actually try to use it. OpenGL ES 2.0 devices should use OES_texture_3D.
  366. // GL_EXT_texture_array is for desktops.
  367. GLAD_EXT_texture_array = false;
  368. if (GLAD_OES_texture_3D)
  369. {
  370. // Function signatures don't match, we'll have to conditionally call it
  371. //fp_glTexImage3D = fp_glTexImage3DOES;
  372. fp_glTexSubImage3D = fp_glTexSubImage3DOES;
  373. fp_glCopyTexSubImage3D = fp_glCopyTexSubImage3DOES;
  374. fp_glCompressedTexImage3D = fp_glCompressedTexImage3DOES;
  375. fp_glCompressedTexSubImage3D = fp_glCompressedTexSubImage3DOES;
  376. fp_glFramebufferTexture3D = fp_glFramebufferTexture3DOES;
  377. }
  378. }
  379. if (!GLAD_VERSION_3_2 && !GLAD_ES_VERSION_3_2 && !GLAD_ARB_draw_elements_base_vertex)
  380. {
  381. if (GLAD_OES_draw_elements_base_vertex)
  382. {
  383. fp_glDrawElementsBaseVertex = fp_glDrawElementsBaseVertexOES;
  384. if (GLAD_ES_VERSION_3_0)
  385. {
  386. fp_glDrawRangeElementsBaseVertex = fp_glDrawRangeElementsBaseVertexOES;
  387. fp_glDrawElementsInstancedBaseVertex = fp_glDrawElementsInstancedBaseVertexOES;
  388. }
  389. }
  390. else if (GLAD_EXT_draw_elements_base_vertex)
  391. {
  392. fp_glDrawElementsBaseVertex = fp_glDrawElementsBaseVertexEXT;
  393. if (GLAD_ES_VERSION_3_0)
  394. {
  395. fp_glDrawRangeElementsBaseVertex = fp_glDrawRangeElementsBaseVertexEXT;
  396. fp_glDrawElementsInstancedBaseVertex = fp_glDrawElementsInstancedBaseVertexEXT;
  397. }
  398. }
  399. }
  400. }
  401. void OpenGL::initMaxValues()
  402. {
  403. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0)
  404. {
  405. GLint range = 0;
  406. GLint precision = 0;
  407. glGetShaderPrecisionFormat(GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, &range, &precision);
  408. pixelShaderHighpSupported = range > 0;
  409. }
  410. else
  411. pixelShaderHighpSupported = true;
  412. baseVertexSupported = GLAD_VERSION_3_2 || GLAD_ES_VERSION_3_2 || GLAD_ARB_draw_elements_base_vertex
  413. || GLAD_OES_draw_elements_base_vertex || GLAD_EXT_draw_elements_base_vertex;
  414. // We'll need this value to clamp anisotropy.
  415. if (GLAD_EXT_texture_filter_anisotropic)
  416. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
  417. else
  418. maxAnisotropy = 1.0f;
  419. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max2DTextureSize);
  420. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxCubeTextureSize);
  421. if (isTextureTypeSupported(TEXTURE_VOLUME))
  422. glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max3DTextureSize);
  423. else
  424. max3DTextureSize = 0;
  425. if (isTextureTypeSupported(TEXTURE_2D_ARRAY))
  426. glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &maxTextureArrayLayers);
  427. else
  428. maxTextureArrayLayers = 0;
  429. if (isBufferUsageSupported(BUFFERUSAGE_TEXEL))
  430. glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTexelBufferSize);
  431. else
  432. maxTexelBufferSize = 0;
  433. if (isBufferUsageSupported(BUFFERUSAGE_SHADER_STORAGE))
  434. {
  435. glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &maxShaderStorageBufferSize);
  436. glGetIntegerv(GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &maxShaderStorageBufferBindings);
  437. }
  438. else
  439. {
  440. maxShaderStorageBufferSize = 0;
  441. maxShaderStorageBufferBindings = 0;
  442. }
  443. if (GLAD_ES_VERSION_3_1 || GLAD_VERSION_4_3)
  444. {
  445. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &maxComputeWorkGroupsX);
  446. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &maxComputeWorkGroupsY);
  447. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &maxComputeWorkGroupsZ);
  448. }
  449. else
  450. {
  451. maxComputeWorkGroupsX = 0;
  452. maxComputeWorkGroupsY = 0;
  453. maxComputeWorkGroupsZ = 0;
  454. }
  455. int maxattachments = 1;
  456. int maxdrawbuffers = 1;
  457. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_2_0)
  458. {
  459. glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxattachments);
  460. glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxdrawbuffers);
  461. }
  462. maxRenderTargets = std::max(std::min(maxattachments, maxdrawbuffers), 1);
  463. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object
  464. || GLAD_EXT_framebuffer_multisample || GLAD_APPLE_framebuffer_multisample
  465. || GLAD_ANGLE_framebuffer_multisample)
  466. {
  467. glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
  468. }
  469. else
  470. maxSamples = 1;
  471. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
  472. GLfloat limits[2];
  473. if (GLAD_VERSION_3_0)
  474. glGetFloatv(GL_POINT_SIZE_RANGE, limits);
  475. else
  476. glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, limits);
  477. maxPointSize = limits[1];
  478. if (isSamplerLODBiasSupported())
  479. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxLODBias);
  480. else
  481. maxLODBias = 0.0f;
  482. }
  483. void OpenGL::createDefaultTexture()
  484. {
  485. // Set the 'default' texture as a repeating white pixel. Otherwise, texture
  486. // calls inside a shader would return black when drawing graphics primitives
  487. // which would create the need to use different "passthrough" shaders for
  488. // untextured primitives vs images.
  489. const GLubyte pix[] = {255, 255, 255, 255};
  490. const GLubyte intpix[] = {1, 1, 1, 1};
  491. SamplerState s;
  492. s.minFilter = s.magFilter = SamplerState::FILTER_NEAREST;
  493. s.wrapU = s.wrapV = s.wrapW = SamplerState::WRAP_CLAMP;
  494. for (int i = 0; i < TEXTURE_MAX_ENUM; i++)
  495. {
  496. for (int datatype = (int)DATA_BASETYPE_FLOAT; datatype <= (int)DATA_BASETYPE_UINT; datatype++)
  497. {
  498. state.defaultTexture[i][datatype] = 0;
  499. TextureType type = (TextureType) i;
  500. if (!isTextureTypeSupported(type))
  501. continue;
  502. if (datatype != DATA_BASETYPE_FLOAT && !(GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0))
  503. continue;
  504. GLuint curtexture = state.boundTextures[type][0];
  505. glGenTextures(1, &state.defaultTexture[type][datatype]);
  506. bindTextureToUnit(type, state.defaultTexture[type][datatype], 0, false);
  507. setSamplerState(type, s);
  508. PixelFormat format = PIXELFORMAT_RGBA8_UNORM;
  509. if (datatype == DATA_BASETYPE_INT)
  510. format = PIXELFORMAT_RGBA8_INT;
  511. else if (datatype == DATA_BASETYPE_UINT)
  512. format = PIXELFORMAT_RGBA8_UINT;
  513. const GLubyte *p = datatype == DATA_BASETYPE_FLOAT ? pix : intpix;
  514. bool isSRGB = false;
  515. rawTexStorage(type, 1, format, isSRGB, 1, 1);
  516. TextureFormat fmt = convertPixelFormat(format, false, isSRGB);
  517. int slices = type == TEXTURE_CUBE ? 6 : 1;
  518. for (int slice = 0; slice < slices; slice++)
  519. {
  520. GLenum gltarget = getGLTextureType(type);
  521. if (type == TEXTURE_CUBE)
  522. gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
  523. if (type == TEXTURE_2D || type == TEXTURE_CUBE)
  524. glTexSubImage2D(gltarget, 0, 0, 0, 1, 1, fmt.externalformat, fmt.type, p);
  525. else if (type == TEXTURE_2D_ARRAY || type == TEXTURE_VOLUME)
  526. glTexSubImage3D(gltarget, 0, 0, 0, slice, 1, 1, 1, fmt.externalformat, fmt.type, p);
  527. }
  528. bindTextureToUnit(type, curtexture, 0, false);
  529. }
  530. }
  531. }
  532. void OpenGL::prepareDraw(love::graphics::Graphics *gfx)
  533. {
  534. TempDebugGroup debuggroup("Prepare OpenGL draw");
  535. // Make sure the active shader's love-provided uniforms are up to date.
  536. if (Shader::current != nullptr)
  537. {
  538. Rect viewport = getViewport();
  539. ((Shader *)Shader::current)->updateBuiltinUniforms(gfx, viewport.w, viewport.h);
  540. }
  541. }
  542. GLenum OpenGL::getGLPrimitiveType(PrimitiveType type)
  543. {
  544. switch (type)
  545. {
  546. case PRIMITIVE_TRIANGLES: return GL_TRIANGLES;
  547. case PRIMITIVE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
  548. case PRIMITIVE_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
  549. case PRIMITIVE_POINTS: return GL_POINTS;
  550. case PRIMITIVE_MAX_ENUM: return GL_ZERO;
  551. }
  552. return GL_ZERO;
  553. }
  554. GLenum OpenGL::getGLBufferType(BufferUsage usage)
  555. {
  556. switch (usage)
  557. {
  558. case BUFFERUSAGE_VERTEX: return GL_ARRAY_BUFFER;
  559. case BUFFERUSAGE_INDEX: return GL_ELEMENT_ARRAY_BUFFER;
  560. case BUFFERUSAGE_TEXEL: return GL_TEXTURE_BUFFER;
  561. case BUFFERUSAGE_SHADER_STORAGE: return GL_SHADER_STORAGE_BUFFER;
  562. case BUFFERUSAGE_INDIRECT_ARGUMENTS: return GL_DRAW_INDIRECT_BUFFER;
  563. case BUFFERUSAGE_MAX_ENUM: return GL_ZERO;
  564. }
  565. return GL_ZERO;
  566. }
  567. GLenum OpenGL::getGLTextureType(TextureType type)
  568. {
  569. switch (type)
  570. {
  571. case TEXTURE_2D: return GL_TEXTURE_2D;
  572. case TEXTURE_VOLUME: return GL_TEXTURE_3D;
  573. case TEXTURE_2D_ARRAY: return GL_TEXTURE_2D_ARRAY;
  574. case TEXTURE_CUBE: return GL_TEXTURE_CUBE_MAP;
  575. case TEXTURE_MAX_ENUM: return GL_TEXTURE_BUFFER; // Hack
  576. }
  577. return GL_ZERO;
  578. }
  579. GLenum OpenGL::getGLIndexDataType(IndexDataType type)
  580. {
  581. switch (type)
  582. {
  583. case INDEX_UINT16: return GL_UNSIGNED_SHORT;
  584. case INDEX_UINT32: return GL_UNSIGNED_INT;
  585. default: return GL_ZERO;
  586. }
  587. }
  588. GLenum OpenGL::getGLVertexDataType(DataFormat format, int &components, GLboolean &normalized, bool &intformat)
  589. {
  590. normalized = GL_FALSE;
  591. intformat = false;
  592. components = 1;
  593. switch (format)
  594. {
  595. case DATAFORMAT_FLOAT:
  596. components = 1;
  597. return GL_FLOAT;
  598. case DATAFORMAT_FLOAT_VEC2:
  599. components = 2;
  600. return GL_FLOAT;
  601. case DATAFORMAT_FLOAT_VEC3:
  602. components = 3;
  603. return GL_FLOAT;
  604. case DATAFORMAT_FLOAT_VEC4:
  605. components = 4;
  606. return GL_FLOAT;
  607. case DATAFORMAT_FLOAT_MAT2X2:
  608. case DATAFORMAT_FLOAT_MAT2X3:
  609. case DATAFORMAT_FLOAT_MAT2X4:
  610. case DATAFORMAT_FLOAT_MAT3X2:
  611. case DATAFORMAT_FLOAT_MAT3X3:
  612. case DATAFORMAT_FLOAT_MAT3X4:
  613. case DATAFORMAT_FLOAT_MAT4X2:
  614. case DATAFORMAT_FLOAT_MAT4X3:
  615. case DATAFORMAT_FLOAT_MAT4X4:
  616. return GL_ZERO;
  617. case DATAFORMAT_INT32:
  618. components = 1;
  619. intformat = true;
  620. return GL_INT;
  621. case DATAFORMAT_INT32_VEC2:
  622. components = 2;
  623. intformat = true;
  624. return GL_INT;
  625. case DATAFORMAT_INT32_VEC3:
  626. components = 3;
  627. intformat = true;
  628. return GL_INT;
  629. case DATAFORMAT_INT32_VEC4:
  630. components = 4;
  631. intformat = true;
  632. return GL_INT;
  633. case DATAFORMAT_UINT32:
  634. components = 1;
  635. intformat = true;
  636. return GL_UNSIGNED_INT;
  637. case DATAFORMAT_UINT32_VEC2:
  638. components = 2;
  639. intformat = true;
  640. return GL_UNSIGNED_INT;
  641. case DATAFORMAT_UINT32_VEC3:
  642. components = 3;
  643. intformat = true;
  644. return GL_UNSIGNED_INT;
  645. case DATAFORMAT_UINT32_VEC4:
  646. components = 4;
  647. intformat = true;
  648. return GL_UNSIGNED_INT;
  649. case DATAFORMAT_SNORM8_VEC4:
  650. components = 4;
  651. normalized = GL_TRUE;
  652. return GL_BYTE;
  653. case DATAFORMAT_UNORM8_VEC4:
  654. components = 4;
  655. normalized = GL_TRUE;
  656. return GL_UNSIGNED_BYTE;
  657. case DATAFORMAT_INT8_VEC4:
  658. components = 4;
  659. intformat = true;
  660. return GL_BYTE;
  661. case DATAFORMAT_UINT8_VEC4:
  662. components = 4;
  663. intformat = true;
  664. return GL_UNSIGNED_BYTE;
  665. case DATAFORMAT_SNORM16_VEC2:
  666. components = 2;
  667. normalized = GL_TRUE;
  668. return GL_BYTE;
  669. case DATAFORMAT_SNORM16_VEC4:
  670. components = 4;
  671. normalized = GL_TRUE;
  672. return GL_BYTE;
  673. case DATAFORMAT_UNORM16_VEC2:
  674. components = 2;
  675. normalized = GL_TRUE;
  676. return GL_UNSIGNED_SHORT;
  677. case DATAFORMAT_UNORM16_VEC4:
  678. components = 4;
  679. normalized = GL_TRUE;
  680. return GL_UNSIGNED_SHORT;
  681. case DATAFORMAT_INT16_VEC2:
  682. components = 2;
  683. intformat = true;
  684. return GL_SHORT;
  685. case DATAFORMAT_INT16_VEC4:
  686. components = 4;
  687. intformat = true;
  688. return GL_SHORT;
  689. case DATAFORMAT_UINT16:
  690. components = 1;
  691. intformat = true;
  692. return GL_UNSIGNED_SHORT;
  693. case DATAFORMAT_UINT16_VEC2:
  694. components = 2;
  695. intformat = true;
  696. return GL_UNSIGNED_SHORT;
  697. case DATAFORMAT_UINT16_VEC4:
  698. components = 4;
  699. intformat = true;
  700. return GL_UNSIGNED_SHORT;
  701. case DATAFORMAT_BOOL:
  702. case DATAFORMAT_BOOL_VEC2:
  703. case DATAFORMAT_BOOL_VEC3:
  704. case DATAFORMAT_BOOL_VEC4:
  705. return GL_ZERO;
  706. case DATAFORMAT_MAX_ENUM:
  707. return GL_ZERO;
  708. }
  709. return GL_ZERO;
  710. }
  711. GLenum OpenGL::getGLBufferDataUsage(BufferDataUsage usage)
  712. {
  713. switch (usage)
  714. {
  715. case BUFFERDATAUSAGE_STREAM: return GL_STREAM_DRAW;
  716. case BUFFERDATAUSAGE_DYNAMIC: return GL_DYNAMIC_DRAW;
  717. case BUFFERDATAUSAGE_STATIC: return GL_STATIC_DRAW;
  718. case BUFFERDATAUSAGE_READBACK:
  719. return (GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0) ? GL_STREAM_READ : GL_STREAM_DRAW;
  720. default: return 0;
  721. }
  722. }
  723. void OpenGL::bindBuffer(BufferUsage type, GLuint buffer)
  724. {
  725. if (state.boundBuffers[type] != buffer)
  726. {
  727. glBindBuffer(getGLBufferType(type), buffer);
  728. state.boundBuffers[type] = buffer;
  729. }
  730. }
  731. void OpenGL::deleteBuffer(GLuint buffer)
  732. {
  733. glDeleteBuffers(1, &buffer);
  734. for (int i = 0; i < (int) BUFFERUSAGE_MAX_ENUM; i++)
  735. {
  736. if (state.boundBuffers[i] == buffer)
  737. state.boundBuffers[i] = 0;
  738. for (GLuint &bufferid : state.boundIndexedBuffers[i])
  739. {
  740. if (bufferid == buffer)
  741. bufferid = 0;
  742. }
  743. }
  744. }
  745. void OpenGL::setVertexAttributes(const VertexAttributes &attributes, const BufferBindings &buffers)
  746. {
  747. uint32 enablediff = attributes.enableBits ^ state.enabledAttribArrays;
  748. uint32 instanceattribbits = 0;
  749. uint32 allbits = attributes.enableBits | state.enabledAttribArrays;
  750. uint32 i = 0;
  751. while (allbits)
  752. {
  753. uint32 bit = 1u << i;
  754. if (enablediff & bit)
  755. {
  756. if (attributes.enableBits & bit)
  757. glEnableVertexAttribArray(i);
  758. else
  759. glDisableVertexAttribArray(i);
  760. }
  761. if (attributes.enableBits & bit)
  762. {
  763. const auto &attrib = attributes.attribs[i];
  764. const auto &layout = attributes.bufferLayouts[attrib.bufferIndex];
  765. const auto &bufferinfo = buffers.info[attrib.bufferIndex];
  766. uint32 bufferbit = 1u << attrib.bufferIndex;
  767. uint32 divisor = (attributes.instanceBits & bufferbit) != 0 ? 1 : 0;
  768. uint32 divisorbit = divisor << i;
  769. instanceattribbits |= divisorbit;
  770. if ((state.instancedAttribArrays & bit) ^ divisorbit)
  771. glVertexAttribDivisor(i, divisor);
  772. int components = 0;
  773. GLboolean normalized = GL_FALSE;
  774. bool intformat = false;
  775. GLenum gltype = getGLVertexDataType(attrib.format, components, normalized, intformat);
  776. const void *offsetpointer = reinterpret_cast<void*>(bufferinfo.offset + attrib.offsetFromVertex);
  777. bindBuffer(BUFFERUSAGE_VERTEX, (GLuint) bufferinfo.buffer->getHandle());
  778. if (intformat)
  779. glVertexAttribIPointer(i, components, gltype, layout.stride, offsetpointer);
  780. else
  781. glVertexAttribPointer(i, components, gltype, normalized, layout.stride, offsetpointer);
  782. }
  783. i++;
  784. allbits >>= 1;
  785. }
  786. state.enabledAttribArrays = attributes.enableBits;
  787. state.instancedAttribArrays = instanceattribbits | (state.instancedAttribArrays & (~attributes.enableBits));
  788. // glDisableVertexAttribArray will make the constant value for a vertex
  789. // attribute undefined. We rely on the per-vertex color attribute being
  790. // white when no per-vertex color is used, so we set it here.
  791. // FIXME: Is there a better place to do this?
  792. if ((enablediff & ATTRIBFLAG_COLOR) && !(attributes.enableBits & ATTRIBFLAG_COLOR))
  793. glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
  794. }
  795. void OpenGL::setCullMode(CullMode mode)
  796. {
  797. bool enabled = mode != CULL_NONE;
  798. if (enabled != isStateEnabled(ENABLE_FACE_CULL))
  799. setEnableState(ENABLE_FACE_CULL, enabled);
  800. if (enabled)
  801. {
  802. GLenum glmode = mode == CULL_BACK ? GL_BACK : GL_FRONT;
  803. if (glmode != state.faceCullMode)
  804. {
  805. glCullFace(glmode);
  806. state.faceCullMode = glmode;
  807. }
  808. }
  809. }
  810. void OpenGL::clearDepth(double value)
  811. {
  812. if (GLAD_ES_VERSION_2_0)
  813. glClearDepthf((GLfloat) value);
  814. else
  815. glClearDepth(value);
  816. }
  817. void OpenGL::setViewport(const Rect &v)
  818. {
  819. glViewport(v.x, v.y, v.w, v.h);
  820. state.viewport = v;
  821. }
  822. Rect OpenGL::getViewport() const
  823. {
  824. return state.viewport;
  825. }
  826. void OpenGL::setScissor(const Rect &v, bool rtActive)
  827. {
  828. if (rtActive)
  829. glScissor(v.x, v.y, v.w, v.h);
  830. else
  831. {
  832. // With no RT active, we need to compensate for glScissor starting
  833. // from the lower left of the viewport instead of the top left.
  834. glScissor(v.x, state.viewport.h - (v.y + v.h), v.w, v.h);
  835. }
  836. state.scissor = v;
  837. }
  838. void OpenGL::setEnableState(EnableState enablestate, bool enable)
  839. {
  840. GLenum glstate = GL_NONE;
  841. switch (enablestate)
  842. {
  843. case ENABLE_BLEND:
  844. glstate = GL_BLEND;
  845. break;
  846. case ENABLE_DEPTH_TEST:
  847. glstate = GL_DEPTH_TEST;
  848. break;
  849. case ENABLE_STENCIL_TEST:
  850. glstate = GL_STENCIL_TEST;
  851. break;
  852. case ENABLE_SCISSOR_TEST:
  853. glstate = GL_SCISSOR_TEST;
  854. break;
  855. case ENABLE_FACE_CULL:
  856. glstate = GL_CULL_FACE;
  857. break;
  858. case ENABLE_FRAMEBUFFER_SRGB:
  859. glstate = GL_FRAMEBUFFER_SRGB;
  860. break;
  861. case ENABLE_MAX_ENUM:
  862. break;
  863. }
  864. if (enable)
  865. glEnable(glstate);
  866. else
  867. glDisable(glstate);
  868. state.enableState[enablestate] = enable;
  869. }
  870. bool OpenGL::isStateEnabled(EnableState enablestate) const
  871. {
  872. return state.enableState[enablestate];
  873. }
  874. void OpenGL::bindFramebuffer(FramebufferTarget target, GLuint framebuffer)
  875. {
  876. bool bindingmodified = false;
  877. if ((target & FRAMEBUFFER_DRAW) && state.boundFramebuffers[0] != framebuffer)
  878. {
  879. bindingmodified = true;
  880. state.boundFramebuffers[0] = framebuffer;
  881. }
  882. if ((target & FRAMEBUFFER_READ) && state.boundFramebuffers[1] != framebuffer)
  883. {
  884. bindingmodified = true;
  885. state.boundFramebuffers[1] = framebuffer;
  886. }
  887. if (bindingmodified)
  888. {
  889. GLenum gltarget = GL_FRAMEBUFFER;
  890. if (target == FRAMEBUFFER_DRAW)
  891. gltarget = GL_DRAW_FRAMEBUFFER;
  892. else if (target == FRAMEBUFFER_READ)
  893. gltarget = GL_READ_FRAMEBUFFER;
  894. glBindFramebuffer(gltarget, framebuffer);
  895. }
  896. }
  897. GLenum OpenGL::getFramebuffer(FramebufferTarget target) const
  898. {
  899. if (target & FRAMEBUFFER_DRAW)
  900. return state.boundFramebuffers[0];
  901. else if (target & FRAMEBUFFER_READ)
  902. return state.boundFramebuffers[1];
  903. else
  904. return 0;
  905. }
  906. void OpenGL::deleteFramebuffer(GLuint framebuffer)
  907. {
  908. glDeleteFramebuffers(1, &framebuffer);
  909. for (int i = 0; i < 2; i++)
  910. {
  911. if (state.boundFramebuffers[i] == framebuffer)
  912. state.boundFramebuffers[i] = 0;
  913. }
  914. }
  915. void OpenGL::framebufferTexture(GLenum attachment, TextureType texType, GLuint texture, int level, int layer, int face)
  916. {
  917. GLenum textarget = getGLTextureType(texType);
  918. switch (texType)
  919. {
  920. case TEXTURE_2D:
  921. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, textarget, texture, level);
  922. break;
  923. case TEXTURE_VOLUME:
  924. glFramebufferTexture3D(GL_FRAMEBUFFER, attachment, textarget, texture, level, layer);
  925. break;
  926. case TEXTURE_2D_ARRAY:
  927. glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, texture, level, layer);
  928. break;
  929. case TEXTURE_CUBE:
  930. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture, level);
  931. break;
  932. default:
  933. break;
  934. }
  935. }
  936. void OpenGL::setDepthWrites(bool enable)
  937. {
  938. glDepthMask(enable ? GL_TRUE : GL_FALSE);
  939. state.depthWritesEnabled = enable;
  940. }
  941. bool OpenGL::hasDepthWrites() const
  942. {
  943. return state.depthWritesEnabled;
  944. }
  945. void OpenGL::setStencilWriteMask(uint32 mask)
  946. {
  947. glStencilMask(mask);
  948. state.stencilWriteMask = mask;
  949. }
  950. uint32 OpenGL::getStencilWriteMask() const
  951. {
  952. return state.stencilWriteMask;
  953. }
  954. void OpenGL::useProgram(GLuint program)
  955. {
  956. glUseProgram(program);
  957. ++stats.shaderSwitches;
  958. }
  959. GLuint OpenGL::getDefaultFBO() const
  960. {
  961. #ifdef LOVE_IOS
  962. // Hack: iOS uses a custom FBO.
  963. SDL_SysWMinfo info = {};
  964. SDL_VERSION(&info.version);
  965. SDL_GetWindowWMInfo(SDL_GL_GetCurrentWindow(), &info);
  966. return info.info.uikit.framebuffer;
  967. #else
  968. return 0;
  969. #endif
  970. }
  971. GLuint OpenGL::getDefaultTexture(TextureType type, DataBaseType datatype) const
  972. {
  973. return state.defaultTexture[type][datatype];
  974. }
  975. void OpenGL::setTextureUnit(int textureunit)
  976. {
  977. if (textureunit != state.curTextureUnit)
  978. glActiveTexture(GL_TEXTURE0 + textureunit);
  979. state.curTextureUnit = textureunit;
  980. }
  981. void OpenGL::bindTextureToUnit(TextureType target, GLuint texture, int textureunit, bool restoreprev, bool bindforedit)
  982. {
  983. if (texture != state.boundTextures[target][textureunit])
  984. {
  985. int oldtextureunit = state.curTextureUnit;
  986. if (oldtextureunit != textureunit)
  987. glActiveTexture(GL_TEXTURE0 + textureunit);
  988. state.boundTextures[target][textureunit] = texture;
  989. glBindTexture(getGLTextureType(target), texture);
  990. if (restoreprev && oldtextureunit != textureunit)
  991. glActiveTexture(GL_TEXTURE0 + oldtextureunit);
  992. else
  993. state.curTextureUnit = textureunit;
  994. }
  995. else if (bindforedit && !restoreprev && textureunit != state.curTextureUnit)
  996. {
  997. glActiveTexture(GL_TEXTURE0 + textureunit);
  998. state.curTextureUnit = textureunit;
  999. }
  1000. }
  1001. void OpenGL::bindBufferTextureToUnit(GLuint texture, int textureunit, bool restoreprev, bool bindforedit)
  1002. {
  1003. bindTextureToUnit(TEXTURE_MAX_ENUM, texture, textureunit, restoreprev, bindforedit);
  1004. }
  1005. void OpenGL::bindTextureToUnit(Texture *texture, int textureunit, bool restoreprev, bool bindforedit)
  1006. {
  1007. TextureType textype = TEXTURE_2D;
  1008. GLuint handle = 0;
  1009. if (texture != nullptr)
  1010. {
  1011. textype = texture->getTextureType();
  1012. handle = (GLuint) texture->getHandle();
  1013. }
  1014. else
  1015. {
  1016. DataBaseType datatype = DATA_BASETYPE_FLOAT;
  1017. if (textureunit == 0 && Shader::current != nullptr)
  1018. {
  1019. const Shader::UniformInfo *info = Shader::current->getMainTextureInfo();
  1020. if (info != nullptr)
  1021. {
  1022. textype = info->textureType;
  1023. datatype = info->dataBaseType;
  1024. }
  1025. }
  1026. handle = getDefaultTexture(textype, datatype);
  1027. }
  1028. bindTextureToUnit(textype, handle, textureunit, restoreprev, bindforedit);
  1029. }
  1030. void OpenGL::bindIndexedBuffer(GLuint buffer, BufferUsage type, int index)
  1031. {
  1032. auto &bindings = state.boundIndexedBuffers[type];
  1033. if (bindings.size() > (size_t) index && buffer != bindings[index])
  1034. {
  1035. bindings[index] = buffer;
  1036. glBindBufferBase(getGLBufferType(type), index, buffer);
  1037. // glBindBufferBase affects glBindBuffer as well... for some reason.
  1038. state.boundBuffers[type] = buffer;
  1039. }
  1040. }
  1041. void OpenGL::deleteTexture(GLuint texture)
  1042. {
  1043. // glDeleteTextures binds texture 0 to all texture units the deleted texture
  1044. // was bound to before deletion.
  1045. for (int i = 0; i < TEXTURE_MAX_ENUM + 1; i++)
  1046. {
  1047. for (GLuint &texid : state.boundTextures[i])
  1048. {
  1049. if (texid == texture)
  1050. texid = 0;
  1051. }
  1052. }
  1053. glDeleteTextures(1, &texture);
  1054. }
  1055. GLint OpenGL::getGLWrapMode(SamplerState::WrapMode wmode)
  1056. {
  1057. switch (wmode)
  1058. {
  1059. case SamplerState::WRAP_CLAMP:
  1060. default:
  1061. return GL_CLAMP_TO_EDGE;
  1062. case SamplerState::WRAP_CLAMP_ZERO:
  1063. case SamplerState::WRAP_CLAMP_ONE:
  1064. return GL_CLAMP_TO_BORDER;
  1065. case SamplerState::WRAP_REPEAT:
  1066. return GL_REPEAT;
  1067. case SamplerState::WRAP_MIRRORED_REPEAT:
  1068. return GL_MIRRORED_REPEAT;
  1069. }
  1070. }
  1071. GLint OpenGL::getGLCompareMode(CompareMode mode)
  1072. {
  1073. switch (mode)
  1074. {
  1075. case COMPARE_LESS: return GL_LESS;
  1076. case COMPARE_LEQUAL: return GL_LEQUAL;
  1077. case COMPARE_EQUAL: return GL_EQUAL;
  1078. case COMPARE_GEQUAL: return GL_GEQUAL;
  1079. case COMPARE_GREATER: return GL_GREATER;
  1080. case COMPARE_NOTEQUAL: return GL_NOTEQUAL;
  1081. case COMPARE_ALWAYS: return GL_ALWAYS;
  1082. case COMPARE_NEVER: return GL_NEVER;
  1083. default: return GL_NEVER;
  1084. }
  1085. }
  1086. static bool isClampOne(SamplerState::WrapMode mode)
  1087. {
  1088. return mode == SamplerState::WRAP_CLAMP_ONE;
  1089. }
  1090. void OpenGL::setSamplerState(TextureType target, SamplerState &s)
  1091. {
  1092. GLenum gltarget = getGLTextureType(target);
  1093. GLint gmin = s.minFilter == SamplerState::FILTER_NEAREST ? GL_NEAREST : GL_LINEAR;
  1094. GLint gmag = s.magFilter == SamplerState::FILTER_NEAREST ? GL_NEAREST : GL_LINEAR;
  1095. if (s.mipmapFilter != SamplerState::MIPMAP_FILTER_NONE)
  1096. {
  1097. if (s.minFilter == SamplerState::FILTER_NEAREST && s.mipmapFilter == SamplerState::MIPMAP_FILTER_NEAREST)
  1098. gmin = GL_NEAREST_MIPMAP_NEAREST;
  1099. else if (s.minFilter == SamplerState::FILTER_NEAREST && s.mipmapFilter == SamplerState::MIPMAP_FILTER_LINEAR)
  1100. gmin = GL_NEAREST_MIPMAP_LINEAR;
  1101. else if (s.minFilter == SamplerState::FILTER_LINEAR && s.mipmapFilter == SamplerState::MIPMAP_FILTER_NEAREST)
  1102. gmin = GL_LINEAR_MIPMAP_NEAREST;
  1103. else if (s.minFilter == SamplerState::FILTER_LINEAR && s.mipmapFilter == SamplerState::MIPMAP_FILTER_LINEAR)
  1104. gmin = GL_LINEAR_MIPMAP_LINEAR;
  1105. }
  1106. glTexParameteri(gltarget, GL_TEXTURE_MIN_FILTER, gmin);
  1107. glTexParameteri(gltarget, GL_TEXTURE_MAG_FILTER, gmag);
  1108. if (!isClampZeroOneTextureWrapSupported())
  1109. {
  1110. if (SamplerState::isClampZeroOrOne(s.wrapU)) s.wrapU = SamplerState::WRAP_CLAMP;
  1111. if (SamplerState::isClampZeroOrOne(s.wrapV)) s.wrapV = SamplerState::WRAP_CLAMP;
  1112. if (SamplerState::isClampZeroOrOne(s.wrapW)) s.wrapW = SamplerState::WRAP_CLAMP;
  1113. }
  1114. if (SamplerState::isClampZeroOrOne(s.wrapU) || SamplerState::isClampZeroOrOne(s.wrapV) || SamplerState::isClampZeroOrOne(s.wrapW))
  1115. {
  1116. GLfloat c[] = {0.0f, 0.0f, 0.0f, 0.0f};
  1117. if (isClampOne(s.wrapU) || isClampOne(s.wrapU) || isClampOne(s.wrapV))
  1118. c[0] = c[1] = c[2] = c[3] = 1.0f;
  1119. glTexParameterfv(gltarget, GL_TEXTURE_BORDER_COLOR, c);
  1120. }
  1121. glTexParameteri(gltarget, GL_TEXTURE_WRAP_S, getGLWrapMode(s.wrapU));
  1122. glTexParameteri(gltarget, GL_TEXTURE_WRAP_T, getGLWrapMode(s.wrapV));
  1123. if (target == TEXTURE_VOLUME)
  1124. glTexParameteri(gltarget, GL_TEXTURE_WRAP_R, getGLWrapMode(s.wrapW));
  1125. if (isSamplerLODBiasSupported())
  1126. {
  1127. float maxbias = getMaxLODBias();
  1128. if (maxbias > 0.01f)
  1129. maxbias -= 0.01f;
  1130. s.lodBias = std::min(std::max(s.lodBias, -maxbias), maxbias);
  1131. glTexParameterf(gltarget, GL_TEXTURE_LOD_BIAS, s.lodBias);
  1132. }
  1133. else
  1134. {
  1135. s.lodBias = 0.0f;
  1136. }
  1137. if (GLAD_EXT_texture_filter_anisotropic)
  1138. {
  1139. uint8 maxAniso = (uint8) std::min(maxAnisotropy, (float)LOVE_UINT8_MAX);
  1140. s.maxAnisotropy = std::min(std::max(s.maxAnisotropy, (uint8)1), maxAniso);
  1141. glTexParameteri(gltarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, s.maxAnisotropy);
  1142. }
  1143. else
  1144. {
  1145. s.maxAnisotropy = 1;
  1146. }
  1147. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_1_0)
  1148. {
  1149. glTexParameterf(gltarget, GL_TEXTURE_MIN_LOD, (float)s.minLod);
  1150. glTexParameterf(gltarget, GL_TEXTURE_MAX_LOD, (float)s.maxLod);
  1151. }
  1152. else
  1153. {
  1154. s.minLod = 0;
  1155. s.maxLod = LOVE_UINT8_MAX;
  1156. }
  1157. if (isDepthCompareSampleSupported())
  1158. {
  1159. if (s.depthSampleMode.hasValue)
  1160. {
  1161. // See the comment in renderstate.h
  1162. GLenum glmode = getGLCompareMode(getReversedCompareMode(s.depthSampleMode.value));
  1163. glTexParameteri(gltarget, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  1164. glTexParameteri(gltarget, GL_TEXTURE_COMPARE_FUNC, glmode);
  1165. }
  1166. else
  1167. {
  1168. glTexParameteri(gltarget, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  1169. }
  1170. }
  1171. else
  1172. {
  1173. s.depthSampleMode.hasValue = false;
  1174. }
  1175. }
  1176. bool OpenGL::rawTexStorage(TextureType target, int levels, PixelFormat pixelformat, bool &isSRGB, int width, int height, int depth)
  1177. {
  1178. GLenum gltarget = getGLTextureType(target);
  1179. TextureFormat fmt = convertPixelFormat(pixelformat, false, isSRGB);
  1180. // This shouldn't be needed for glTexStorage, but some drivers don't follow
  1181. // the spec apparently.
  1182. // https://stackoverflow.com/questions/13859061/does-an-immutable-texture-need-a-gl-texture-max-level
  1183. if (GLAD_VERSION_1_2 || GLAD_ES_VERSION_3_0)
  1184. glTexParameteri(gltarget, GL_TEXTURE_MAX_LEVEL, levels - 1);
  1185. if (fmt.swizzled)
  1186. {
  1187. glTexParameteri(gltarget, GL_TEXTURE_SWIZZLE_R, fmt.swizzle[0]);
  1188. glTexParameteri(gltarget, GL_TEXTURE_SWIZZLE_G, fmt.swizzle[1]);
  1189. glTexParameteri(gltarget, GL_TEXTURE_SWIZZLE_B, fmt.swizzle[2]);
  1190. glTexParameteri(gltarget, GL_TEXTURE_SWIZZLE_A, fmt.swizzle[3]);
  1191. }
  1192. if (isTexStorageSupported())
  1193. {
  1194. if (target == TEXTURE_2D || target == TEXTURE_CUBE)
  1195. glTexStorage2D(gltarget, levels, fmt.internalformat, width, height);
  1196. else if (target == TEXTURE_VOLUME || target == TEXTURE_2D_ARRAY)
  1197. glTexStorage3D(gltarget, levels, fmt.internalformat, width, height, depth);
  1198. }
  1199. else
  1200. {
  1201. int w = width;
  1202. int h = height;
  1203. int d = depth;
  1204. for (int level = 0; level < levels; level++)
  1205. {
  1206. if (target == TEXTURE_2D || target == TEXTURE_CUBE)
  1207. {
  1208. int faces = target == TEXTURE_CUBE ? 6 : 1;
  1209. for (int face = 0; face < faces; face++)
  1210. {
  1211. if (target == TEXTURE_CUBE)
  1212. gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
  1213. glTexImage2D(gltarget, level, fmt.internalformat, w, h, 0,
  1214. fmt.externalformat, fmt.type, nullptr);
  1215. }
  1216. }
  1217. else if (target == TEXTURE_2D_ARRAY || target == TEXTURE_VOLUME)
  1218. {
  1219. if (target == TEXTURE_VOLUME && GLAD_ES_VERSION_2_0 && GLAD_OES_texture_3D && !GLAD_ES_VERSION_3_0)
  1220. {
  1221. glTexImage3DOES(gltarget, level, fmt.internalformat, w, h,
  1222. d, 0, fmt.externalformat, fmt.type, nullptr);
  1223. }
  1224. else
  1225. {
  1226. glTexImage3D(gltarget, level, fmt.internalformat, w, h, d,
  1227. 0, fmt.externalformat, fmt.type, nullptr);
  1228. }
  1229. }
  1230. w = std::max(w / 2, 1);
  1231. h = std::max(h / 2, 1);
  1232. if (target == TEXTURE_VOLUME)
  1233. d = std::max(d / 2, 1);
  1234. }
  1235. }
  1236. return gltarget != GL_ZERO;
  1237. }
  1238. bool OpenGL::isTexStorageSupported()
  1239. {
  1240. if (gl.bugs.texStorageBreaksSubImage)
  1241. return false;
  1242. return GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_2 || GLAD_ARB_texture_storage;
  1243. }
  1244. bool OpenGL::isTextureTypeSupported(TextureType type) const
  1245. {
  1246. switch (type)
  1247. {
  1248. case TEXTURE_2D:
  1249. return true;
  1250. case TEXTURE_VOLUME:
  1251. return GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_3D;
  1252. case TEXTURE_2D_ARRAY:
  1253. return GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_texture_array;
  1254. case TEXTURE_CUBE:
  1255. return GLAD_VERSION_1_3 || GLAD_ES_VERSION_2_0;
  1256. case TEXTURE_MAX_ENUM:
  1257. return false;
  1258. }
  1259. return false;
  1260. }
  1261. bool OpenGL::isBufferUsageSupported(BufferUsage usage) const
  1262. {
  1263. switch (usage)
  1264. {
  1265. case BUFFERUSAGE_VERTEX:
  1266. case BUFFERUSAGE_INDEX:
  1267. return true;
  1268. case BUFFERUSAGE_TEXEL:
  1269. return GLAD_VERSION_3_1 || GLAD_ES_VERSION_3_2;
  1270. case BUFFERUSAGE_SHADER_STORAGE:
  1271. return (GLAD_VERSION_4_3 && isCoreProfile()) || GLAD_ES_VERSION_3_1;
  1272. case BUFFERUSAGE_INDIRECT_ARGUMENTS:
  1273. return (GLAD_VERSION_4_0 && isCoreProfile()) || GLAD_ES_VERSION_3_1;
  1274. case BUFFERUSAGE_MAX_ENUM:
  1275. return false;
  1276. }
  1277. return false;
  1278. }
  1279. bool OpenGL::isClampZeroOneTextureWrapSupported() const
  1280. {
  1281. return GLAD_VERSION_1_3 || GLAD_EXT_texture_border_clamp || GLAD_NV_texture_border_clamp;
  1282. }
  1283. bool OpenGL::isPixelShaderHighpSupported() const
  1284. {
  1285. return pixelShaderHighpSupported;
  1286. }
  1287. bool OpenGL::isInstancingSupported() const
  1288. {
  1289. return GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_3
  1290. || GLAD_ARB_instanced_arrays || GLAD_EXT_instanced_arrays || GLAD_ANGLE_instanced_arrays;
  1291. }
  1292. bool OpenGL::isDepthCompareSampleSupported() const
  1293. {
  1294. // Our official API only supports this in GLSL3 shaders, but unofficially
  1295. // the requirements are more lax.
  1296. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_shadow_samplers;
  1297. }
  1298. bool OpenGL::isSamplerLODBiasSupported() const
  1299. {
  1300. return GLAD_VERSION_1_4;
  1301. }
  1302. bool OpenGL::isBaseVertexSupported() const
  1303. {
  1304. return baseVertexSupported;
  1305. }
  1306. bool OpenGL::isMultiFormatMRTSupported() const
  1307. {
  1308. return getMaxRenderTargets() > 1 && (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object);
  1309. }
  1310. bool OpenGL::isCopyBufferSupported() const
  1311. {
  1312. return GLAD_VERSION_3_1 || GLAD_ES_VERSION_3_0;
  1313. }
  1314. bool OpenGL::isCopyBufferToTextureSupported() const
  1315. {
  1316. // Requires pixel unpack buffer binding support.
  1317. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0;
  1318. }
  1319. bool OpenGL::isCopyTextureToBufferSupported() const
  1320. {
  1321. // Requires glGetTextureSubImage support.
  1322. return GLAD_VERSION_4_5 || GLAD_ARB_get_texture_sub_image;
  1323. }
  1324. bool OpenGL::isCopyRenderTargetToBufferSupported() const
  1325. {
  1326. // Requires pixel pack buffer binding support.
  1327. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0;
  1328. }
  1329. int OpenGL::getMax2DTextureSize() const
  1330. {
  1331. return std::max(max2DTextureSize, 1);
  1332. }
  1333. int OpenGL::getMax3DTextureSize() const
  1334. {
  1335. return std::max(max3DTextureSize, 1);
  1336. }
  1337. int OpenGL::getMaxCubeTextureSize() const
  1338. {
  1339. return std::max(maxCubeTextureSize, 1);
  1340. }
  1341. int OpenGL::getMaxTextureLayers() const
  1342. {
  1343. return std::max(maxTextureArrayLayers, 1);
  1344. }
  1345. int OpenGL::getMaxTexelBufferSize() const
  1346. {
  1347. return maxTexelBufferSize;
  1348. }
  1349. int OpenGL::getMaxShaderStorageBufferSize() const
  1350. {
  1351. return maxShaderStorageBufferSize;
  1352. }
  1353. int OpenGL::getMaxComputeWorkGroupsX() const
  1354. {
  1355. return maxComputeWorkGroupsX;
  1356. }
  1357. int OpenGL::getMaxComputeWorkGroupsY() const
  1358. {
  1359. return maxComputeWorkGroupsY;
  1360. }
  1361. int OpenGL::getMaxComputeWorkGroupsZ() const
  1362. {
  1363. return maxComputeWorkGroupsZ;
  1364. }
  1365. int OpenGL::getMaxRenderTargets() const
  1366. {
  1367. return std::min(maxRenderTargets, MAX_COLOR_RENDER_TARGETS);
  1368. }
  1369. int OpenGL::getMaxSamples() const
  1370. {
  1371. return maxSamples;
  1372. }
  1373. int OpenGL::getMaxTextureUnits() const
  1374. {
  1375. return maxTextureUnits;
  1376. }
  1377. int OpenGL::getMaxShaderStorageBufferBindings() const
  1378. {
  1379. return maxShaderStorageBufferBindings;
  1380. }
  1381. float OpenGL::getMaxPointSize() const
  1382. {
  1383. return maxPointSize;
  1384. }
  1385. float OpenGL::getMaxAnisotropy() const
  1386. {
  1387. return maxAnisotropy;
  1388. }
  1389. float OpenGL::getMaxLODBias() const
  1390. {
  1391. return maxLODBias;
  1392. }
  1393. bool OpenGL::isCoreProfile() const
  1394. {
  1395. return coreProfile;
  1396. }
  1397. OpenGL::Vendor OpenGL::getVendor() const
  1398. {
  1399. return vendor;
  1400. }
  1401. OpenGL::TextureFormat OpenGL::convertPixelFormat(PixelFormat pixelformat, bool renderbuffer, bool &isSRGB)
  1402. {
  1403. TextureFormat f;
  1404. f.framebufferAttachments[0] = GL_COLOR_ATTACHMENT0;
  1405. f.framebufferAttachments[1] = GL_NONE;
  1406. if (isSRGB)
  1407. pixelformat = getSRGBPixelFormat(pixelformat);
  1408. else if (pixelformat == PIXELFORMAT_ETC1_UNORM)
  1409. {
  1410. // The ETC2 format can load ETC1 textures.
  1411. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility)
  1412. pixelformat = PIXELFORMAT_ETC2_RGB_UNORM;
  1413. }
  1414. switch (pixelformat)
  1415. {
  1416. case PIXELFORMAT_R8_UNORM:
  1417. if ((GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_texture_rg || GLAD_EXT_texture_rg)
  1418. && !gl.bugs.brokenR8PixelFormat)
  1419. {
  1420. f.internalformat = GL_R8;
  1421. f.externalformat = GL_RED;
  1422. }
  1423. else
  1424. {
  1425. f.internalformat = GL_LUMINANCE8;
  1426. f.externalformat = GL_LUMINANCE;
  1427. }
  1428. f.type = GL_UNSIGNED_BYTE;
  1429. break;
  1430. case PIXELFORMAT_RG8_UNORM:
  1431. f.internalformat = GL_RG8;
  1432. f.externalformat = GL_RG;
  1433. f.type = GL_UNSIGNED_BYTE;
  1434. break;
  1435. case PIXELFORMAT_RGBA8_UNORM:
  1436. f.internalformat = GL_RGBA8;
  1437. f.externalformat = GL_RGBA;
  1438. f.type = GL_UNSIGNED_BYTE;
  1439. break;
  1440. case PIXELFORMAT_RGBA8_UNORM_sRGB:
  1441. f.internalformat = GL_SRGB8_ALPHA8;
  1442. f.type = GL_UNSIGNED_BYTE;
  1443. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0)
  1444. f.externalformat = GL_SRGB_ALPHA;
  1445. else
  1446. f.externalformat = GL_RGBA;
  1447. break;
  1448. case PIXELFORMAT_BGRA8_UNORM:
  1449. case PIXELFORMAT_BGRA8_UNORM_sRGB:
  1450. // Not supported right now.
  1451. break;
  1452. case PIXELFORMAT_R16_UNORM:
  1453. f.internalformat = GL_R16;
  1454. f.externalformat = GL_RED;
  1455. f.type = GL_UNSIGNED_SHORT;
  1456. break;
  1457. case PIXELFORMAT_RG16_UNORM:
  1458. f.internalformat = GL_RG16;
  1459. f.externalformat = GL_RG;
  1460. f.type = GL_UNSIGNED_SHORT;
  1461. break;
  1462. case PIXELFORMAT_RGBA16_UNORM:
  1463. f.internalformat = GL_RGBA16;
  1464. f.externalformat = GL_RGBA;
  1465. f.type = GL_UNSIGNED_SHORT;
  1466. break;
  1467. case PIXELFORMAT_R16_FLOAT:
  1468. f.internalformat = GL_R16F;
  1469. f.externalformat = GL_RED;
  1470. if (GLAD_OES_texture_half_float)
  1471. f.type = GL_HALF_FLOAT_OES;
  1472. else
  1473. f.type = GL_HALF_FLOAT;
  1474. break;
  1475. case PIXELFORMAT_RG16_FLOAT:
  1476. f.internalformat = GL_RG16F;
  1477. f.externalformat = GL_RG;
  1478. if (GLAD_OES_texture_half_float)
  1479. f.type = GL_HALF_FLOAT_OES;
  1480. else
  1481. f.type = GL_HALF_FLOAT;
  1482. break;
  1483. case PIXELFORMAT_RGBA16_FLOAT:
  1484. f.internalformat = GL_RGBA16F;
  1485. f.externalformat = GL_RGBA;
  1486. if (GLAD_OES_texture_half_float)
  1487. f.type = GL_HALF_FLOAT_OES;
  1488. else
  1489. f.type = GL_HALF_FLOAT;
  1490. break;
  1491. case PIXELFORMAT_R32_FLOAT:
  1492. f.internalformat = GL_R32F;
  1493. f.externalformat = GL_RED;
  1494. f.type = GL_FLOAT;
  1495. break;
  1496. case PIXELFORMAT_RG32_FLOAT:
  1497. f.internalformat = GL_RG32F;
  1498. f.externalformat = GL_RG;
  1499. f.type = GL_FLOAT;
  1500. break;
  1501. case PIXELFORMAT_RGBA32_FLOAT:
  1502. f.internalformat = GL_RGBA32F;
  1503. f.externalformat = GL_RGBA;
  1504. f.type = GL_FLOAT;
  1505. break;
  1506. case PIXELFORMAT_R8_INT:
  1507. f.internalformat = GL_R8I;
  1508. f.externalformat = GL_RED_INTEGER;
  1509. f.type = GL_BYTE;
  1510. break;
  1511. case PIXELFORMAT_R8_UINT:
  1512. f.internalformat = GL_R8UI;
  1513. f.externalformat = GL_RED_INTEGER;
  1514. f.type = GL_UNSIGNED_BYTE;
  1515. break;
  1516. case PIXELFORMAT_RG8_INT:
  1517. f.internalformat = GL_RG8I;
  1518. f.externalformat = GL_RG_INTEGER;
  1519. f.type = GL_BYTE;
  1520. break;
  1521. case PIXELFORMAT_RG8_UINT:
  1522. f.internalformat = GL_RG8UI;
  1523. f.externalformat = GL_RG_INTEGER;
  1524. f.type = GL_UNSIGNED_BYTE;
  1525. break;
  1526. case PIXELFORMAT_RGBA8_INT:
  1527. f.internalformat = GL_RGBA8I;
  1528. f.externalformat = GL_RGBA_INTEGER;
  1529. f.type = GL_BYTE;
  1530. break;
  1531. case PIXELFORMAT_RGBA8_UINT:
  1532. f.internalformat = GL_RGBA8UI;
  1533. f.externalformat = GL_RGBA_INTEGER;
  1534. f.type = GL_UNSIGNED_BYTE;
  1535. break;
  1536. case PIXELFORMAT_R16_INT:
  1537. f.internalformat = GL_R16I;
  1538. f.externalformat = GL_RED_INTEGER;
  1539. f.type = GL_SHORT;
  1540. break;
  1541. case PIXELFORMAT_R16_UINT:
  1542. f.internalformat = GL_R16UI;
  1543. f.externalformat = GL_RED_INTEGER;
  1544. f.type = GL_UNSIGNED_SHORT;
  1545. break;
  1546. case PIXELFORMAT_RG16_INT:
  1547. f.internalformat = GL_RG16I;
  1548. f.externalformat = GL_RG_INTEGER;
  1549. f.type = GL_SHORT;
  1550. break;
  1551. case PIXELFORMAT_RG16_UINT:
  1552. f.internalformat = GL_RG16UI;
  1553. f.externalformat = GL_RG_INTEGER;
  1554. f.type = GL_UNSIGNED_SHORT;
  1555. break;
  1556. case PIXELFORMAT_RGBA16_INT:
  1557. f.internalformat = GL_RGBA16I;
  1558. f.externalformat = GL_RGBA_INTEGER;
  1559. f.type = GL_SHORT;
  1560. break;
  1561. case PIXELFORMAT_RGBA16_UINT:
  1562. f.internalformat = GL_RGBA16UI;
  1563. f.externalformat = GL_RGBA_INTEGER;
  1564. f.type = GL_UNSIGNED_SHORT;
  1565. break;
  1566. case PIXELFORMAT_R32_INT:
  1567. f.internalformat = GL_R32I;
  1568. f.externalformat = GL_RED_INTEGER;
  1569. f.type = GL_INT;
  1570. break;
  1571. case PIXELFORMAT_R32_UINT:
  1572. f.internalformat = GL_R32UI;
  1573. f.externalformat = GL_RED_INTEGER;
  1574. f.type = GL_UNSIGNED_INT;
  1575. break;
  1576. case PIXELFORMAT_RG32_INT:
  1577. f.internalformat = GL_RG32I;
  1578. f.externalformat = GL_RG_INTEGER;
  1579. f.type = GL_INT;
  1580. break;
  1581. case PIXELFORMAT_RG32_UINT:
  1582. f.internalformat = GL_RG32UI;
  1583. f.externalformat = GL_RG_INTEGER;
  1584. f.type = GL_UNSIGNED_INT;
  1585. break;
  1586. case PIXELFORMAT_RGBA32_INT:
  1587. f.internalformat = GL_RGBA32I;
  1588. f.externalformat = GL_RGBA_INTEGER;
  1589. f.type = GL_INT;
  1590. break;
  1591. case PIXELFORMAT_RGBA32_UINT:
  1592. f.internalformat = GL_RGBA32UI;
  1593. f.externalformat = GL_RGBA_INTEGER;
  1594. f.type = GL_UNSIGNED_INT;
  1595. break;
  1596. case PIXELFORMAT_LA8_UNORM:
  1597. if (gl.isCoreProfile() || GLAD_ES_VERSION_3_0)
  1598. {
  1599. f.internalformat = GL_RG8;
  1600. f.externalformat = GL_RG;
  1601. f.type = GL_UNSIGNED_BYTE;
  1602. f.swizzled = true;
  1603. f.swizzle[0] = f.swizzle[1] = f.swizzle[2] = GL_RED;
  1604. f.swizzle[3] = GL_GREEN;
  1605. }
  1606. else
  1607. {
  1608. f.internalformat = GL_LUMINANCE8_ALPHA8;
  1609. f.externalformat = GL_LUMINANCE_ALPHA;
  1610. f.type = GL_UNSIGNED_BYTE;
  1611. }
  1612. break;
  1613. case PIXELFORMAT_RGBA4_UNORM:
  1614. f.internalformat = GL_RGBA4;
  1615. f.externalformat = GL_RGBA;
  1616. f.type = GL_UNSIGNED_SHORT_4_4_4_4;
  1617. break;
  1618. case PIXELFORMAT_RGB5A1_UNORM:
  1619. f.internalformat = GL_RGB5_A1;
  1620. f.externalformat = GL_RGBA;
  1621. f.type = GL_UNSIGNED_SHORT_5_5_5_1;
  1622. break;
  1623. case PIXELFORMAT_RGB565_UNORM:
  1624. f.internalformat = GL_RGB565;
  1625. f.externalformat = GL_RGB;
  1626. f.type = GL_UNSIGNED_SHORT_5_6_5;
  1627. break;
  1628. case PIXELFORMAT_RGB10A2_UNORM:
  1629. f.internalformat = GL_RGB10_A2;
  1630. f.externalformat = GL_RGBA;
  1631. f.type = GL_UNSIGNED_INT_2_10_10_10_REV;
  1632. break;
  1633. case PIXELFORMAT_RG11B10_FLOAT:
  1634. f.internalformat = GL_R11F_G11F_B10F;
  1635. f.externalformat = GL_RGB;
  1636. f.type = GL_UNSIGNED_INT_10F_11F_11F_REV;
  1637. break;
  1638. case PIXELFORMAT_STENCIL8:
  1639. // Prefer a combined depth/stencil buffer due to driver issues.
  1640. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object)
  1641. {
  1642. f.internalformat = GL_DEPTH24_STENCIL8;
  1643. f.externalformat = GL_DEPTH_STENCIL;
  1644. f.type = GL_UNSIGNED_INT_24_8;
  1645. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1646. }
  1647. else if (GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  1648. {
  1649. f.internalformat = GL_DEPTH24_STENCIL8;
  1650. f.externalformat = GL_DEPTH_STENCIL;
  1651. f.type = GL_UNSIGNED_INT_24_8;
  1652. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1653. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1654. }
  1655. else
  1656. {
  1657. f.internalformat = GL_STENCIL_INDEX8;
  1658. f.externalformat = GL_STENCIL;
  1659. f.type = GL_UNSIGNED_BYTE;
  1660. f.framebufferAttachments[0] = GL_STENCIL_ATTACHMENT;
  1661. }
  1662. break;
  1663. case PIXELFORMAT_DEPTH16_UNORM:
  1664. f.internalformat = GL_DEPTH_COMPONENT16;
  1665. f.externalformat = GL_DEPTH_COMPONENT;
  1666. f.type = GL_UNSIGNED_SHORT;
  1667. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1668. break;
  1669. case PIXELFORMAT_DEPTH24_UNORM:
  1670. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0 && !GLAD_OES_depth24 && GLAD_OES_packed_depth_stencil)
  1671. {
  1672. f.internalformat = GL_DEPTH24_STENCIL8;
  1673. f.externalformat = GL_DEPTH_STENCIL;
  1674. f.type = GL_UNSIGNED_INT_24_8;
  1675. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1676. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1677. }
  1678. else
  1679. {
  1680. f.internalformat = GL_DEPTH_COMPONENT24;
  1681. f.externalformat = GL_DEPTH_COMPONENT;
  1682. f.type = GL_UNSIGNED_INT;
  1683. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1684. }
  1685. break;
  1686. case PIXELFORMAT_DEPTH32_FLOAT:
  1687. f.internalformat = GL_DEPTH_COMPONENT32F;
  1688. f.externalformat = GL_DEPTH_COMPONENT;
  1689. f.type = GL_FLOAT;
  1690. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1691. break;
  1692. case PIXELFORMAT_DEPTH24_UNORM_STENCIL8:
  1693. f.internalformat = GL_DEPTH24_STENCIL8;
  1694. f.externalformat = GL_DEPTH_STENCIL;
  1695. f.type = GL_UNSIGNED_INT_24_8;
  1696. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object)
  1697. {
  1698. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1699. }
  1700. else if (GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  1701. {
  1702. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1703. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1704. }
  1705. break;
  1706. case PIXELFORMAT_DEPTH32_FLOAT_STENCIL8:
  1707. f.internalformat = GL_DEPTH32F_STENCIL8;
  1708. f.externalformat = GL_DEPTH_STENCIL;
  1709. f.type = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
  1710. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1711. break;
  1712. case PIXELFORMAT_DXT1_UNORM:
  1713. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  1714. break;
  1715. case PIXELFORMAT_DXT3_UNORM:
  1716. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT : GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  1717. break;
  1718. case PIXELFORMAT_DXT5_UNORM:
  1719. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  1720. break;
  1721. case PIXELFORMAT_BC4_UNORM:
  1722. isSRGB = false;
  1723. f.internalformat = GL_COMPRESSED_RED_RGTC1;
  1724. break;
  1725. case PIXELFORMAT_BC4_SNORM:
  1726. isSRGB = false;
  1727. f.internalformat = GL_COMPRESSED_SIGNED_RED_RGTC1;
  1728. break;
  1729. case PIXELFORMAT_BC5_UNORM:
  1730. isSRGB = false;
  1731. f.internalformat = GL_COMPRESSED_RG_RGTC2;
  1732. break;
  1733. case PIXELFORMAT_BC5_SNORM:
  1734. isSRGB = false;
  1735. f.internalformat = GL_COMPRESSED_SIGNED_RG_RGTC2;
  1736. break;
  1737. case PIXELFORMAT_BC6H_UFLOAT:
  1738. isSRGB = false;
  1739. f.internalformat = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  1740. break;
  1741. case PIXELFORMAT_BC6H_FLOAT:
  1742. isSRGB = false;
  1743. f.internalformat = GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  1744. break;
  1745. case PIXELFORMAT_BC7_UNORM:
  1746. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM : GL_COMPRESSED_RGBA_BPTC_UNORM;
  1747. break;
  1748. case PIXELFORMAT_PVR1_RGB2_UNORM:
  1749. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  1750. break;
  1751. case PIXELFORMAT_PVR1_RGB4_UNORM:
  1752. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT : GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  1753. break;
  1754. case PIXELFORMAT_PVR1_RGBA2_UNORM:
  1755. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT : GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  1756. break;
  1757. case PIXELFORMAT_PVR1_RGBA4_UNORM:
  1758. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT : GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  1759. break;
  1760. case PIXELFORMAT_ETC1_UNORM:
  1761. isSRGB = false;
  1762. f.internalformat = GL_ETC1_RGB8_OES;
  1763. break;
  1764. case PIXELFORMAT_ETC2_RGB_UNORM:
  1765. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ETC2 : GL_COMPRESSED_RGB8_ETC2;
  1766. break;
  1767. case PIXELFORMAT_ETC2_RGBA_UNORM:
  1768. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : GL_COMPRESSED_RGBA8_ETC2_EAC;
  1769. break;
  1770. case PIXELFORMAT_ETC2_RGBA1_UNORM:
  1771. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  1772. break;
  1773. case PIXELFORMAT_EAC_R_UNORM:
  1774. isSRGB = false;
  1775. f.internalformat = GL_COMPRESSED_R11_EAC;
  1776. break;
  1777. case PIXELFORMAT_EAC_R_SNORM:
  1778. isSRGB = false;
  1779. f.internalformat = GL_COMPRESSED_SIGNED_R11_EAC;
  1780. break;
  1781. case PIXELFORMAT_EAC_RG_UNORM:
  1782. isSRGB = false;
  1783. f.internalformat = GL_COMPRESSED_RG11_EAC;
  1784. break;
  1785. case PIXELFORMAT_EAC_RG_SNORM:
  1786. isSRGB = false;
  1787. f.internalformat = GL_COMPRESSED_SIGNED_RG11_EAC;
  1788. break;
  1789. case PIXELFORMAT_ASTC_4x4:
  1790. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR : GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
  1791. break;
  1792. case PIXELFORMAT_ASTC_5x4:
  1793. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR : GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
  1794. break;
  1795. case PIXELFORMAT_ASTC_5x5:
  1796. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR : GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
  1797. break;
  1798. case PIXELFORMAT_ASTC_6x5:
  1799. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR : GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
  1800. break;
  1801. case PIXELFORMAT_ASTC_6x6:
  1802. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR : GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
  1803. break;
  1804. case PIXELFORMAT_ASTC_8x5:
  1805. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR : GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
  1806. break;
  1807. case PIXELFORMAT_ASTC_8x6:
  1808. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR : GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
  1809. break;
  1810. case PIXELFORMAT_ASTC_8x8:
  1811. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR : GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
  1812. break;
  1813. case PIXELFORMAT_ASTC_10x5:
  1814. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR : GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
  1815. break;
  1816. case PIXELFORMAT_ASTC_10x6:
  1817. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR : GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
  1818. break;
  1819. case PIXELFORMAT_ASTC_10x8:
  1820. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR : GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
  1821. break;
  1822. case PIXELFORMAT_ASTC_10x10:
  1823. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR : GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
  1824. break;
  1825. case PIXELFORMAT_ASTC_12x10:
  1826. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR : GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
  1827. break;
  1828. case PIXELFORMAT_ASTC_12x12:
  1829. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR : GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
  1830. break;
  1831. default:
  1832. printf("Unhandled pixel format %d when converting to OpenGL enums!", pixelformat);
  1833. break;
  1834. }
  1835. if (!isPixelFormatCompressed(pixelformat))
  1836. {
  1837. // glTexImage in OpenGL ES 2 only accepts internal format enums that
  1838. // match the external format. GLES3 doesn't have that restriction -
  1839. // except for GL_LUMINANCE_ALPHA which doesn't have a sized version in
  1840. // ES3. However we always use RG8 for PIXELFORMAT_LA8 on GLES3 so it
  1841. // doesn't matter there.
  1842. // Also note that GLES2+extension sRGB format enums are different from
  1843. // desktop GL and GLES3+ (this is handled above).
  1844. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0
  1845. && !renderbuffer && !isTexStorageSupported())
  1846. {
  1847. f.internalformat = f.externalformat;
  1848. }
  1849. if (!isPixelFormatSRGB(pixelformat))
  1850. isSRGB = false;
  1851. }
  1852. return f;
  1853. }
  1854. uint32 OpenGL::getPixelFormatUsageFlags(PixelFormat pixelformat)
  1855. {
  1856. const uint32 commonsample = PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_LINEAR;
  1857. const uint32 commonrender = PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_BLEND | PIXELFORMATUSAGEFLAGS_MSAA;
  1858. const uint32 computewrite = PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  1859. uint32 flags = PIXELFORMATUSAGEFLAGS_NONE;
  1860. switch (pixelformat)
  1861. {
  1862. case PIXELFORMAT_R8_UNORM:
  1863. case PIXELFORMAT_RG8_UNORM:
  1864. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_texture_rg || GLAD_EXT_texture_rg)
  1865. flags |= commonsample | commonrender;
  1866. else if (pixelformat == PIXELFORMAT_R8_UNORM && (GLAD_ES_VERSION_2_0 || GLAD_VERSION_1_1))
  1867. flags |= commonsample; // We'll use OpenGL's luminance format internally.
  1868. if (GLAD_VERSION_4_3)
  1869. flags |= computewrite;
  1870. break;
  1871. case PIXELFORMAT_RGBA8_UNORM:
  1872. flags |= commonsample;
  1873. if (GLAD_VERSION_1_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_rgb8_rgba8 || GLAD_ARM_rgba8)
  1874. flags |= commonrender;
  1875. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1876. flags |= computewrite;
  1877. break;
  1878. case PIXELFORMAT_RGBA8_UNORM_sRGB:
  1879. if (gl.bugs.brokenSRGB)
  1880. break;
  1881. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_2_1 || GLAD_EXT_texture_sRGB)
  1882. flags |= commonsample;
  1883. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0
  1884. || ((GLAD_ARB_framebuffer_sRGB || GLAD_EXT_framebuffer_sRGB) && (GLAD_VERSION_2_1 || GLAD_EXT_texture_sRGB)))
  1885. flags |= commonrender;
  1886. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1887. flags |= computewrite;
  1888. break;
  1889. case PIXELFORMAT_BGRA8_UNORM:
  1890. case PIXELFORMAT_BGRA8_UNORM_sRGB:
  1891. // Not supported right now.
  1892. break;
  1893. case PIXELFORMAT_R16_UNORM:
  1894. case PIXELFORMAT_RG16_UNORM:
  1895. if (GLAD_VERSION_3_0
  1896. || (GLAD_VERSION_1_1 && GLAD_ARB_texture_rg)
  1897. || (GLAD_EXT_texture_norm16 && (GLAD_ES_VERSION_3_0 || GLAD_EXT_texture_rg)))
  1898. flags |= commonsample | commonrender;
  1899. if (GLAD_VERSION_4_3)
  1900. flags |= computewrite;
  1901. break;
  1902. case PIXELFORMAT_RGBA16_UNORM:
  1903. if (GLAD_VERSION_1_1 || GLAD_EXT_texture_norm16)
  1904. flags |= commonsample | commonrender;
  1905. if (GLAD_VERSION_4_3)
  1906. flags |= computewrite;
  1907. break;
  1908. case PIXELFORMAT_R16_FLOAT:
  1909. case PIXELFORMAT_RG16_FLOAT:
  1910. if (GLAD_VERSION_1_0 && (GLAD_VERSION_3_0 || (GLAD_ARB_texture_float && GLAD_ARB_half_float_pixel && GLAD_ARB_texture_rg)))
  1911. flags |= commonsample | commonrender;
  1912. if (GLAD_ES_VERSION_3_0 || (GLAD_OES_texture_half_float && GLAD_EXT_texture_rg))
  1913. flags |= commonsample;
  1914. if (GLAD_EXT_color_buffer_half_float && (GLAD_ES_VERSION_3_0 || GLAD_EXT_texture_rg))
  1915. flags |= commonrender;
  1916. if (!(GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float_linear))
  1917. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1918. if (GLAD_VERSION_4_3)
  1919. flags |= computewrite;
  1920. break;
  1921. case PIXELFORMAT_RGBA16_FLOAT:
  1922. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float && GLAD_ARB_half_float_pixel))
  1923. flags |= commonsample | commonrender;
  1924. if (GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float)
  1925. flags |= commonsample;
  1926. if (GLAD_EXT_color_buffer_half_float)
  1927. flags |= commonrender;
  1928. if (!(GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float_linear))
  1929. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1930. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1931. flags |= computewrite;
  1932. break;
  1933. case PIXELFORMAT_R32_FLOAT:
  1934. if (GLAD_ES_VERSION_3_1)
  1935. flags |= computewrite;
  1936. // Fallthrough.
  1937. case PIXELFORMAT_RG32_FLOAT:
  1938. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float && GLAD_ARB_texture_rg))
  1939. flags |= commonsample | commonrender;
  1940. if (GLAD_ES_VERSION_3_0 || (GLAD_OES_texture_float && GLAD_EXT_texture_rg))
  1941. flags |= commonsample;
  1942. if (!(GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float_linear))
  1943. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1944. if (GLAD_VERSION_4_3)
  1945. flags |= computewrite;
  1946. break;
  1947. case PIXELFORMAT_RGBA32_FLOAT:
  1948. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float))
  1949. flags |= commonsample | commonrender;
  1950. if (GLAD_ES_VERSION_3_0 || GLAD_OES_texture_float)
  1951. flags |= commonsample;
  1952. if (!(GLAD_VERSION_1_1 || GLAD_OES_texture_float_linear))
  1953. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1954. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1955. flags |= computewrite;
  1956. break;
  1957. case PIXELFORMAT_R8_INT:
  1958. case PIXELFORMAT_R8_UINT:
  1959. case PIXELFORMAT_RG8_INT:
  1960. case PIXELFORMAT_RG8_UINT:
  1961. case PIXELFORMAT_RGBA8_INT:
  1962. case PIXELFORMAT_RGBA8_UINT:
  1963. case PIXELFORMAT_R16_INT:
  1964. case PIXELFORMAT_R16_UINT:
  1965. case PIXELFORMAT_RG16_INT:
  1966. case PIXELFORMAT_RG16_UINT:
  1967. case PIXELFORMAT_RGBA16_INT:
  1968. case PIXELFORMAT_RGBA16_UINT:
  1969. case PIXELFORMAT_R32_INT:
  1970. case PIXELFORMAT_R32_UINT:
  1971. case PIXELFORMAT_RG32_INT:
  1972. case PIXELFORMAT_RG32_UINT:
  1973. case PIXELFORMAT_RGBA32_INT:
  1974. case PIXELFORMAT_RGBA32_UINT:
  1975. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0)
  1976. flags |= PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  1977. if (GLAD_VERSION_4_3)
  1978. flags |= computewrite;
  1979. if (GLAD_ES_VERSION_3_1)
  1980. {
  1981. switch (pixelformat)
  1982. {
  1983. case PIXELFORMAT_RGBA8_INT:
  1984. case PIXELFORMAT_RGBA8_UINT:
  1985. case PIXELFORMAT_RGBA16_INT:
  1986. case PIXELFORMAT_RGBA16_UINT:
  1987. case PIXELFORMAT_R32_INT:
  1988. case PIXELFORMAT_R32_UINT:
  1989. case PIXELFORMAT_RGBA32_INT:
  1990. case PIXELFORMAT_RGBA32_UINT:
  1991. flags |= computewrite;
  1992. break;
  1993. default:
  1994. break;
  1995. }
  1996. }
  1997. break;
  1998. case PIXELFORMAT_LA8_UNORM:
  1999. flags |= commonsample;
  2000. break;
  2001. case PIXELFORMAT_RGBA4_UNORM:
  2002. case PIXELFORMAT_RGB5A1_UNORM:
  2003. flags |= commonsample | commonrender;
  2004. break;
  2005. case PIXELFORMAT_RGB565_UNORM:
  2006. if (GLAD_ES_VERSION_2_0 || GLAD_VERSION_4_2 || GLAD_ARB_ES2_compatibility)
  2007. flags |= commonsample | commonrender;
  2008. break;
  2009. case PIXELFORMAT_RGB10A2_UNORM:
  2010. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_1_0)
  2011. flags |= commonsample | commonrender;
  2012. if (GLAD_VERSION_4_3)
  2013. flags |= computewrite;
  2014. break;
  2015. case PIXELFORMAT_RG11B10_FLOAT:
  2016. if (GLAD_VERSION_3_0 || GLAD_EXT_packed_float || GLAD_APPLE_texture_packed_float)
  2017. flags |= commonsample;
  2018. if (GLAD_VERSION_3_0 || GLAD_EXT_packed_float || GLAD_APPLE_color_buffer_packed_float)
  2019. flags |= commonrender;
  2020. if (GLAD_VERSION_4_3)
  2021. flags |= computewrite;
  2022. break;
  2023. case PIXELFORMAT_STENCIL8:
  2024. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2025. break;
  2026. case PIXELFORMAT_DEPTH16_UNORM:
  2027. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2028. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_depth_texture)
  2029. flags |= commonsample;
  2030. break;
  2031. case PIXELFORMAT_DEPTH24_UNORM:
  2032. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_depth24 || GLAD_OES_depth_texture)
  2033. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2034. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || (GLAD_OES_depth_texture && (GLAD_OES_depth24 || GLAD_OES_depth_texture)))
  2035. flags |= commonsample;
  2036. break;
  2037. case PIXELFORMAT_DEPTH24_UNORM_STENCIL8:
  2038. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  2039. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2040. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_packed_depth_stencil || (GLAD_OES_depth_texture && GLAD_OES_packed_depth_stencil))
  2041. flags |= commonsample;
  2042. break;
  2043. case PIXELFORMAT_DEPTH32_FLOAT:
  2044. case PIXELFORMAT_DEPTH32_FLOAT_STENCIL8:
  2045. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_depth_buffer_float)
  2046. flags |= commonsample | PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2047. break;
  2048. case PIXELFORMAT_DXT1_UNORM:
  2049. if (GLAD_EXT_texture_compression_s3tc || GLAD_EXT_texture_compression_dxt1)
  2050. flags |= commonsample;
  2051. break;
  2052. case PIXELFORMAT_DXT3_UNORM:
  2053. if (GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt3)
  2054. flags |= commonsample;
  2055. break;
  2056. case PIXELFORMAT_DXT5_UNORM:
  2057. if (GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt5)
  2058. flags |= commonsample;
  2059. break;
  2060. case PIXELFORMAT_BC4_UNORM:
  2061. case PIXELFORMAT_BC4_SNORM:
  2062. case PIXELFORMAT_BC5_UNORM:
  2063. case PIXELFORMAT_BC5_SNORM:
  2064. if (GLAD_VERSION_3_0 || GLAD_ARB_texture_compression_rgtc || GLAD_EXT_texture_compression_rgtc)
  2065. flags |= commonsample;
  2066. break;
  2067. case PIXELFORMAT_BC6H_UFLOAT:
  2068. case PIXELFORMAT_BC6H_FLOAT:
  2069. case PIXELFORMAT_BC7_UNORM:
  2070. if (GLAD_VERSION_4_2 || GLAD_ARB_texture_compression_bptc)
  2071. flags |= commonsample;
  2072. break;
  2073. case PIXELFORMAT_PVR1_RGB2_UNORM:
  2074. case PIXELFORMAT_PVR1_RGB4_UNORM:
  2075. case PIXELFORMAT_PVR1_RGBA2_UNORM:
  2076. case PIXELFORMAT_PVR1_RGBA4_UNORM:
  2077. if (GLAD_IMG_texture_compression_pvrtc)
  2078. flags |= commonsample;
  2079. break;
  2080. case PIXELFORMAT_ETC1_UNORM:
  2081. // ETC2 support guarantees ETC1 support as well.
  2082. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility || GLAD_OES_compressed_ETC1_RGB8_texture)
  2083. flags |= commonsample;
  2084. break;
  2085. case PIXELFORMAT_ETC2_RGB_UNORM:
  2086. case PIXELFORMAT_ETC2_RGBA_UNORM:
  2087. case PIXELFORMAT_ETC2_RGBA1_UNORM:
  2088. case PIXELFORMAT_EAC_R_UNORM:
  2089. case PIXELFORMAT_EAC_R_SNORM:
  2090. case PIXELFORMAT_EAC_RG_UNORM:
  2091. case PIXELFORMAT_EAC_RG_SNORM:
  2092. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility)
  2093. flags |= commonsample;
  2094. break;
  2095. case PIXELFORMAT_ASTC_4x4:
  2096. case PIXELFORMAT_ASTC_5x4:
  2097. case PIXELFORMAT_ASTC_5x5:
  2098. case PIXELFORMAT_ASTC_6x5:
  2099. case PIXELFORMAT_ASTC_6x6:
  2100. case PIXELFORMAT_ASTC_8x5:
  2101. case PIXELFORMAT_ASTC_8x6:
  2102. case PIXELFORMAT_ASTC_8x8:
  2103. case PIXELFORMAT_ASTC_10x5:
  2104. case PIXELFORMAT_ASTC_10x6:
  2105. case PIXELFORMAT_ASTC_10x8:
  2106. case PIXELFORMAT_ASTC_10x10:
  2107. case PIXELFORMAT_ASTC_12x10:
  2108. case PIXELFORMAT_ASTC_12x12:
  2109. if (GLAD_ES_VERSION_3_2 || GLAD_KHR_texture_compression_astc_ldr)
  2110. flags |= commonsample;
  2111. break;
  2112. case PIXELFORMAT_UNKNOWN:
  2113. case PIXELFORMAT_NORMAL:
  2114. case PIXELFORMAT_HDR:
  2115. case PIXELFORMAT_MAX_ENUM:
  2116. break;
  2117. }
  2118. return flags;
  2119. }
  2120. const char *OpenGL::errorString(GLenum errorcode)
  2121. {
  2122. switch (errorcode)
  2123. {
  2124. case GL_NO_ERROR:
  2125. return "no error";
  2126. case GL_INVALID_ENUM:
  2127. return "invalid enum";
  2128. case GL_INVALID_VALUE:
  2129. return "invalid value";
  2130. case GL_INVALID_OPERATION:
  2131. return "invalid operation";
  2132. case GL_OUT_OF_MEMORY:
  2133. return "out of memory";
  2134. case GL_INVALID_FRAMEBUFFER_OPERATION:
  2135. return "invalid framebuffer operation";
  2136. case GL_CONTEXT_LOST:
  2137. return "OpenGL context has been lost";
  2138. default:
  2139. break;
  2140. }
  2141. static char text[64] = {};
  2142. snprintf(text, sizeof(text), "0x%x", errorcode);
  2143. return text;
  2144. }
  2145. const char *OpenGL::framebufferStatusString(GLenum status)
  2146. {
  2147. switch (status)
  2148. {
  2149. case GL_FRAMEBUFFER_COMPLETE:
  2150. return "complete (success)";
  2151. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  2152. return "Texture format cannot be rendered to on this system.";
  2153. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  2154. return "Error in graphics driver (missing render texture attachment)";
  2155. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  2156. return "Error in graphics driver (incomplete draw buffer)";
  2157. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  2158. return "Error in graphics driver (incomplete read buffer)";
  2159. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  2160. return "Texture with the specified MSAA count cannot be rendered to on this system.";
  2161. case GL_FRAMEBUFFER_UNSUPPORTED:
  2162. return "Renderable textures are unsupported";
  2163. default:
  2164. break;
  2165. }
  2166. static char text[64] = {};
  2167. snprintf(text, sizeof(text), "0x%x", status);
  2168. return text;
  2169. }
  2170. const char *OpenGL::debugSeverityString(GLenum severity)
  2171. {
  2172. switch (severity)
  2173. {
  2174. case GL_DEBUG_SEVERITY_HIGH:
  2175. return "high";
  2176. case GL_DEBUG_SEVERITY_MEDIUM:
  2177. return "medium";
  2178. case GL_DEBUG_SEVERITY_LOW:
  2179. return "low";
  2180. default:
  2181. return "unknown";
  2182. }
  2183. }
  2184. const char *OpenGL::debugSourceString(GLenum source)
  2185. {
  2186. switch (source)
  2187. {
  2188. case GL_DEBUG_SOURCE_API:
  2189. return "API";
  2190. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  2191. return "window";
  2192. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  2193. return "shader";
  2194. case GL_DEBUG_SOURCE_THIRD_PARTY:
  2195. return "external";
  2196. case GL_DEBUG_SOURCE_APPLICATION:
  2197. return "LOVE";
  2198. case GL_DEBUG_SOURCE_OTHER:
  2199. return "other";
  2200. default:
  2201. return "unknown";
  2202. }
  2203. }
  2204. const char *OpenGL::debugTypeString(GLenum type)
  2205. {
  2206. switch (type)
  2207. {
  2208. case GL_DEBUG_TYPE_ERROR:
  2209. return "error";
  2210. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  2211. return "deprecated behavior";
  2212. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  2213. return "undefined behavior";
  2214. case GL_DEBUG_TYPE_PERFORMANCE:
  2215. return "performance";
  2216. case GL_DEBUG_TYPE_PORTABILITY:
  2217. return "portability";
  2218. case GL_DEBUG_TYPE_OTHER:
  2219. return "other";
  2220. default:
  2221. return "unknown";
  2222. }
  2223. }
  2224. // OpenGL class instance singleton.
  2225. OpenGL gl;
  2226. } // opengl
  2227. } // graphics
  2228. } // love