OpenGL.cpp 69 KB

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