OpenGL.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  1. /**
  2. * Copyright (c) 2006-2022 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. return GLAD_VERSION_3_1 || GLAD_ES_VERSION_3_2;
  1261. case BUFFERUSAGE_SHADER_STORAGE:
  1262. return (GLAD_VERSION_4_3 && isCoreProfile()) || GLAD_ES_VERSION_3_1;
  1263. case BUFFERUSAGE_MAX_ENUM:
  1264. return false;
  1265. }
  1266. return false;
  1267. }
  1268. bool OpenGL::isClampZeroOneTextureWrapSupported() const
  1269. {
  1270. return GLAD_VERSION_1_3 || GLAD_EXT_texture_border_clamp || GLAD_NV_texture_border_clamp;
  1271. }
  1272. bool OpenGL::isPixelShaderHighpSupported() const
  1273. {
  1274. return pixelShaderHighpSupported;
  1275. }
  1276. bool OpenGL::isInstancingSupported() const
  1277. {
  1278. return GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_3
  1279. || GLAD_ARB_instanced_arrays || GLAD_EXT_instanced_arrays || GLAD_ANGLE_instanced_arrays;
  1280. }
  1281. bool OpenGL::isDepthCompareSampleSupported() const
  1282. {
  1283. // Our official API only supports this in GLSL3 shaders, but unofficially
  1284. // the requirements are more lax.
  1285. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_shadow_samplers;
  1286. }
  1287. bool OpenGL::isSamplerLODBiasSupported() const
  1288. {
  1289. return GLAD_VERSION_1_4;
  1290. }
  1291. bool OpenGL::isBaseVertexSupported() const
  1292. {
  1293. return baseVertexSupported;
  1294. }
  1295. bool OpenGL::isMultiFormatMRTSupported() const
  1296. {
  1297. return getMaxRenderTargets() > 1 && (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object);
  1298. }
  1299. bool OpenGL::isCopyBufferSupported() const
  1300. {
  1301. return GLAD_VERSION_3_1 || GLAD_ES_VERSION_3_0;
  1302. }
  1303. bool OpenGL::isCopyBufferToTextureSupported() const
  1304. {
  1305. // Requires pixel unpack buffer binding support.
  1306. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0;
  1307. }
  1308. bool OpenGL::isCopyTextureToBufferSupported() const
  1309. {
  1310. // Requires glGetTextureSubImage support.
  1311. return GLAD_VERSION_4_5 || GLAD_ARB_get_texture_sub_image;
  1312. }
  1313. bool OpenGL::isCopyRenderTargetToBufferSupported() const
  1314. {
  1315. // Requires pixel pack buffer binding support.
  1316. return GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0;
  1317. }
  1318. int OpenGL::getMax2DTextureSize() const
  1319. {
  1320. return std::max(max2DTextureSize, 1);
  1321. }
  1322. int OpenGL::getMax3DTextureSize() const
  1323. {
  1324. return std::max(max3DTextureSize, 1);
  1325. }
  1326. int OpenGL::getMaxCubeTextureSize() const
  1327. {
  1328. return std::max(maxCubeTextureSize, 1);
  1329. }
  1330. int OpenGL::getMaxTextureLayers() const
  1331. {
  1332. return std::max(maxTextureArrayLayers, 1);
  1333. }
  1334. int OpenGL::getMaxTexelBufferSize() const
  1335. {
  1336. return maxTexelBufferSize;
  1337. }
  1338. int OpenGL::getMaxShaderStorageBufferSize() const
  1339. {
  1340. return maxShaderStorageBufferSize;
  1341. }
  1342. int OpenGL::getMaxComputeWorkGroupsX() const
  1343. {
  1344. return maxComputeWorkGroupsX;
  1345. }
  1346. int OpenGL::getMaxComputeWorkGroupsY() const
  1347. {
  1348. return maxComputeWorkGroupsY;
  1349. }
  1350. int OpenGL::getMaxComputeWorkGroupsZ() const
  1351. {
  1352. return maxComputeWorkGroupsZ;
  1353. }
  1354. int OpenGL::getMaxRenderTargets() const
  1355. {
  1356. return std::min(maxRenderTargets, MAX_COLOR_RENDER_TARGETS);
  1357. }
  1358. int OpenGL::getMaxSamples() const
  1359. {
  1360. return maxSamples;
  1361. }
  1362. int OpenGL::getMaxTextureUnits() const
  1363. {
  1364. return maxTextureUnits;
  1365. }
  1366. int OpenGL::getMaxShaderStorageBufferBindings() const
  1367. {
  1368. return maxShaderStorageBufferBindings;
  1369. }
  1370. float OpenGL::getMaxPointSize() const
  1371. {
  1372. return maxPointSize;
  1373. }
  1374. float OpenGL::getMaxAnisotropy() const
  1375. {
  1376. return maxAnisotropy;
  1377. }
  1378. float OpenGL::getMaxLODBias() const
  1379. {
  1380. return maxLODBias;
  1381. }
  1382. bool OpenGL::isCoreProfile() const
  1383. {
  1384. return coreProfile;
  1385. }
  1386. OpenGL::Vendor OpenGL::getVendor() const
  1387. {
  1388. return vendor;
  1389. }
  1390. OpenGL::TextureFormat OpenGL::convertPixelFormat(PixelFormat pixelformat, bool renderbuffer, bool &isSRGB)
  1391. {
  1392. TextureFormat f;
  1393. f.framebufferAttachments[0] = GL_COLOR_ATTACHMENT0;
  1394. f.framebufferAttachments[1] = GL_NONE;
  1395. if (isSRGB)
  1396. pixelformat = getSRGBPixelFormat(pixelformat);
  1397. else if (pixelformat == PIXELFORMAT_ETC1_UNORM)
  1398. {
  1399. // The ETC2 format can load ETC1 textures.
  1400. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility)
  1401. pixelformat = PIXELFORMAT_ETC2_RGB_UNORM;
  1402. }
  1403. switch (pixelformat)
  1404. {
  1405. case PIXELFORMAT_R8_UNORM:
  1406. if ((GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_texture_rg || GLAD_EXT_texture_rg)
  1407. && !gl.bugs.brokenR8PixelFormat)
  1408. {
  1409. f.internalformat = GL_R8;
  1410. f.externalformat = GL_RED;
  1411. }
  1412. else
  1413. {
  1414. f.internalformat = GL_LUMINANCE8;
  1415. f.externalformat = GL_LUMINANCE;
  1416. }
  1417. f.type = GL_UNSIGNED_BYTE;
  1418. break;
  1419. case PIXELFORMAT_RG8_UNORM:
  1420. f.internalformat = GL_RG8;
  1421. f.externalformat = GL_RG;
  1422. f.type = GL_UNSIGNED_BYTE;
  1423. break;
  1424. case PIXELFORMAT_RGBA8_UNORM:
  1425. f.internalformat = GL_RGBA8;
  1426. f.externalformat = GL_RGBA;
  1427. f.type = GL_UNSIGNED_BYTE;
  1428. break;
  1429. case PIXELFORMAT_RGBA8_UNORM_sRGB:
  1430. f.internalformat = GL_SRGB8_ALPHA8;
  1431. f.type = GL_UNSIGNED_BYTE;
  1432. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0)
  1433. f.externalformat = GL_SRGB_ALPHA;
  1434. else
  1435. f.externalformat = GL_RGBA;
  1436. break;
  1437. case PIXELFORMAT_BGRA8_UNORM:
  1438. case PIXELFORMAT_BGRA8_UNORM_sRGB:
  1439. // Not supported right now.
  1440. break;
  1441. case PIXELFORMAT_R16_UNORM:
  1442. f.internalformat = GL_R16;
  1443. f.externalformat = GL_RED;
  1444. f.type = GL_UNSIGNED_SHORT;
  1445. break;
  1446. case PIXELFORMAT_RG16_UNORM:
  1447. f.internalformat = GL_RG16;
  1448. f.externalformat = GL_RG;
  1449. f.type = GL_UNSIGNED_SHORT;
  1450. break;
  1451. case PIXELFORMAT_RGBA16_UNORM:
  1452. f.internalformat = GL_RGBA16;
  1453. f.externalformat = GL_RGBA;
  1454. f.type = GL_UNSIGNED_SHORT;
  1455. break;
  1456. case PIXELFORMAT_R16_FLOAT:
  1457. f.internalformat = GL_R16F;
  1458. f.externalformat = GL_RED;
  1459. if (GLAD_OES_texture_half_float)
  1460. f.type = GL_HALF_FLOAT_OES;
  1461. else
  1462. f.type = GL_HALF_FLOAT;
  1463. break;
  1464. case PIXELFORMAT_RG16_FLOAT:
  1465. f.internalformat = GL_RG16F;
  1466. f.externalformat = GL_RG;
  1467. if (GLAD_OES_texture_half_float)
  1468. f.type = GL_HALF_FLOAT_OES;
  1469. else
  1470. f.type = GL_HALF_FLOAT;
  1471. break;
  1472. case PIXELFORMAT_RGBA16_FLOAT:
  1473. f.internalformat = GL_RGBA16F;
  1474. f.externalformat = GL_RGBA;
  1475. if (GLAD_OES_texture_half_float)
  1476. f.type = GL_HALF_FLOAT_OES;
  1477. else
  1478. f.type = GL_HALF_FLOAT;
  1479. break;
  1480. case PIXELFORMAT_R32_FLOAT:
  1481. f.internalformat = GL_R32F;
  1482. f.externalformat = GL_RED;
  1483. f.type = GL_FLOAT;
  1484. break;
  1485. case PIXELFORMAT_RG32_FLOAT:
  1486. f.internalformat = GL_RG32F;
  1487. f.externalformat = GL_RG;
  1488. f.type = GL_FLOAT;
  1489. break;
  1490. case PIXELFORMAT_RGBA32_FLOAT:
  1491. f.internalformat = GL_RGBA32F;
  1492. f.externalformat = GL_RGBA;
  1493. f.type = GL_FLOAT;
  1494. break;
  1495. case PIXELFORMAT_R8_INT:
  1496. f.internalformat = GL_R8I;
  1497. f.externalformat = GL_RED_INTEGER;
  1498. f.type = GL_BYTE;
  1499. break;
  1500. case PIXELFORMAT_R8_UINT:
  1501. f.internalformat = GL_R8UI;
  1502. f.externalformat = GL_RED_INTEGER;
  1503. f.type = GL_UNSIGNED_BYTE;
  1504. break;
  1505. case PIXELFORMAT_RG8_INT:
  1506. f.internalformat = GL_RG8I;
  1507. f.externalformat = GL_RG_INTEGER;
  1508. f.type = GL_BYTE;
  1509. break;
  1510. case PIXELFORMAT_RG8_UINT:
  1511. f.internalformat = GL_RG8UI;
  1512. f.externalformat = GL_RG_INTEGER;
  1513. f.type = GL_UNSIGNED_BYTE;
  1514. break;
  1515. case PIXELFORMAT_RGBA8_INT:
  1516. f.internalformat = GL_RGBA8I;
  1517. f.externalformat = GL_RGBA_INTEGER;
  1518. f.type = GL_BYTE;
  1519. break;
  1520. case PIXELFORMAT_RGBA8_UINT:
  1521. f.internalformat = GL_RGBA8UI;
  1522. f.externalformat = GL_RGBA_INTEGER;
  1523. f.type = GL_UNSIGNED_BYTE;
  1524. break;
  1525. case PIXELFORMAT_R16_INT:
  1526. f.internalformat = GL_R16I;
  1527. f.externalformat = GL_RED_INTEGER;
  1528. f.type = GL_SHORT;
  1529. break;
  1530. case PIXELFORMAT_R16_UINT:
  1531. f.internalformat = GL_R16UI;
  1532. f.externalformat = GL_RED_INTEGER;
  1533. f.type = GL_UNSIGNED_SHORT;
  1534. break;
  1535. case PIXELFORMAT_RG16_INT:
  1536. f.internalformat = GL_RG16I;
  1537. f.externalformat = GL_RG_INTEGER;
  1538. f.type = GL_SHORT;
  1539. break;
  1540. case PIXELFORMAT_RG16_UINT:
  1541. f.internalformat = GL_RG16UI;
  1542. f.externalformat = GL_RG_INTEGER;
  1543. f.type = GL_UNSIGNED_SHORT;
  1544. break;
  1545. case PIXELFORMAT_RGBA16_INT:
  1546. f.internalformat = GL_RGBA16I;
  1547. f.externalformat = GL_RGBA_INTEGER;
  1548. f.type = GL_SHORT;
  1549. break;
  1550. case PIXELFORMAT_RGBA16_UINT:
  1551. f.internalformat = GL_RGBA16UI;
  1552. f.externalformat = GL_RGBA_INTEGER;
  1553. f.type = GL_UNSIGNED_SHORT;
  1554. break;
  1555. case PIXELFORMAT_R32_INT:
  1556. f.internalformat = GL_R32I;
  1557. f.externalformat = GL_RED_INTEGER;
  1558. f.type = GL_INT;
  1559. break;
  1560. case PIXELFORMAT_R32_UINT:
  1561. f.internalformat = GL_R32UI;
  1562. f.externalformat = GL_RED_INTEGER;
  1563. f.type = GL_UNSIGNED_INT;
  1564. break;
  1565. case PIXELFORMAT_RG32_INT:
  1566. f.internalformat = GL_RG32I;
  1567. f.externalformat = GL_RG_INTEGER;
  1568. f.type = GL_INT;
  1569. break;
  1570. case PIXELFORMAT_RG32_UINT:
  1571. f.internalformat = GL_RG32UI;
  1572. f.externalformat = GL_RG_INTEGER;
  1573. f.type = GL_UNSIGNED_INT;
  1574. break;
  1575. case PIXELFORMAT_RGBA32_INT:
  1576. f.internalformat = GL_RGBA32I;
  1577. f.externalformat = GL_RGBA_INTEGER;
  1578. f.type = GL_INT;
  1579. break;
  1580. case PIXELFORMAT_RGBA32_UINT:
  1581. f.internalformat = GL_RGBA32UI;
  1582. f.externalformat = GL_RGBA_INTEGER;
  1583. f.type = GL_UNSIGNED_INT;
  1584. break;
  1585. case PIXELFORMAT_LA8_UNORM:
  1586. if (gl.isCoreProfile() || GLAD_ES_VERSION_3_0)
  1587. {
  1588. f.internalformat = GL_RG8;
  1589. f.externalformat = GL_RG;
  1590. f.type = GL_UNSIGNED_BYTE;
  1591. f.swizzled = true;
  1592. f.swizzle[0] = f.swizzle[1] = f.swizzle[2] = GL_RED;
  1593. f.swizzle[3] = GL_GREEN;
  1594. }
  1595. else
  1596. {
  1597. f.internalformat = GL_LUMINANCE8_ALPHA8;
  1598. f.externalformat = GL_LUMINANCE_ALPHA;
  1599. f.type = GL_UNSIGNED_BYTE;
  1600. }
  1601. break;
  1602. case PIXELFORMAT_RGBA4_UNORM:
  1603. f.internalformat = GL_RGBA4;
  1604. f.externalformat = GL_RGBA;
  1605. f.type = GL_UNSIGNED_SHORT_4_4_4_4;
  1606. break;
  1607. case PIXELFORMAT_RGB5A1_UNORM:
  1608. f.internalformat = GL_RGB5_A1;
  1609. f.externalformat = GL_RGBA;
  1610. f.type = GL_UNSIGNED_SHORT_5_5_5_1;
  1611. break;
  1612. case PIXELFORMAT_RGB565_UNORM:
  1613. f.internalformat = GL_RGB565;
  1614. f.externalformat = GL_RGB;
  1615. f.type = GL_UNSIGNED_SHORT_5_6_5;
  1616. break;
  1617. case PIXELFORMAT_RGB10A2_UNORM:
  1618. f.internalformat = GL_RGB10_A2;
  1619. f.externalformat = GL_RGBA;
  1620. f.type = GL_UNSIGNED_INT_2_10_10_10_REV;
  1621. break;
  1622. case PIXELFORMAT_RG11B10_FLOAT:
  1623. f.internalformat = GL_R11F_G11F_B10F;
  1624. f.externalformat = GL_RGB;
  1625. f.type = GL_UNSIGNED_INT_10F_11F_11F_REV;
  1626. break;
  1627. case PIXELFORMAT_STENCIL8:
  1628. // Prefer a combined depth/stencil buffer due to driver issues.
  1629. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object)
  1630. {
  1631. f.internalformat = GL_DEPTH24_STENCIL8;
  1632. f.externalformat = GL_DEPTH_STENCIL;
  1633. f.type = GL_UNSIGNED_INT_24_8;
  1634. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1635. }
  1636. else if (GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  1637. {
  1638. f.internalformat = GL_DEPTH24_STENCIL8;
  1639. f.externalformat = GL_DEPTH_STENCIL;
  1640. f.type = GL_UNSIGNED_INT_24_8;
  1641. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1642. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1643. }
  1644. else
  1645. {
  1646. f.internalformat = GL_STENCIL_INDEX8;
  1647. f.externalformat = GL_STENCIL;
  1648. f.type = GL_UNSIGNED_BYTE;
  1649. f.framebufferAttachments[0] = GL_STENCIL_ATTACHMENT;
  1650. }
  1651. break;
  1652. case PIXELFORMAT_DEPTH16_UNORM:
  1653. f.internalformat = GL_DEPTH_COMPONENT16;
  1654. f.externalformat = GL_DEPTH_COMPONENT;
  1655. f.type = GL_UNSIGNED_SHORT;
  1656. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1657. break;
  1658. case PIXELFORMAT_DEPTH24_UNORM:
  1659. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0 && !GLAD_OES_depth24 && GLAD_OES_packed_depth_stencil)
  1660. {
  1661. f.internalformat = GL_DEPTH24_STENCIL8;
  1662. f.externalformat = GL_DEPTH_STENCIL;
  1663. f.type = GL_UNSIGNED_INT_24_8;
  1664. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1665. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1666. }
  1667. else
  1668. {
  1669. f.internalformat = GL_DEPTH_COMPONENT24;
  1670. f.externalformat = GL_DEPTH_COMPONENT;
  1671. f.type = GL_UNSIGNED_INT;
  1672. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1673. }
  1674. break;
  1675. case PIXELFORMAT_DEPTH32_FLOAT:
  1676. f.internalformat = GL_DEPTH_COMPONENT32F;
  1677. f.externalformat = GL_DEPTH_COMPONENT;
  1678. f.type = GL_FLOAT;
  1679. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1680. break;
  1681. case PIXELFORMAT_DEPTH24_UNORM_STENCIL8:
  1682. f.internalformat = GL_DEPTH24_STENCIL8;
  1683. f.externalformat = GL_DEPTH_STENCIL;
  1684. f.type = GL_UNSIGNED_INT_24_8;
  1685. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object)
  1686. {
  1687. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1688. }
  1689. else if (GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  1690. {
  1691. f.framebufferAttachments[0] = GL_DEPTH_ATTACHMENT;
  1692. f.framebufferAttachments[1] = GL_STENCIL_ATTACHMENT;
  1693. }
  1694. break;
  1695. case PIXELFORMAT_DEPTH32_FLOAT_STENCIL8:
  1696. f.internalformat = GL_DEPTH32F_STENCIL8;
  1697. f.externalformat = GL_DEPTH_STENCIL;
  1698. f.type = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
  1699. f.framebufferAttachments[0] = GL_DEPTH_STENCIL_ATTACHMENT;
  1700. break;
  1701. case PIXELFORMAT_DXT1_UNORM:
  1702. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  1703. break;
  1704. case PIXELFORMAT_DXT3_UNORM:
  1705. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT : GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  1706. break;
  1707. case PIXELFORMAT_DXT5_UNORM:
  1708. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  1709. break;
  1710. case PIXELFORMAT_BC4_UNORM:
  1711. isSRGB = false;
  1712. f.internalformat = GL_COMPRESSED_RED_RGTC1;
  1713. break;
  1714. case PIXELFORMAT_BC4_SNORM:
  1715. isSRGB = false;
  1716. f.internalformat = GL_COMPRESSED_SIGNED_RED_RGTC1;
  1717. break;
  1718. case PIXELFORMAT_BC5_UNORM:
  1719. isSRGB = false;
  1720. f.internalformat = GL_COMPRESSED_RG_RGTC2;
  1721. break;
  1722. case PIXELFORMAT_BC5_SNORM:
  1723. isSRGB = false;
  1724. f.internalformat = GL_COMPRESSED_SIGNED_RG_RGTC2;
  1725. break;
  1726. case PIXELFORMAT_BC6H_UFLOAT:
  1727. isSRGB = false;
  1728. f.internalformat = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  1729. break;
  1730. case PIXELFORMAT_BC6H_FLOAT:
  1731. isSRGB = false;
  1732. f.internalformat = GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  1733. break;
  1734. case PIXELFORMAT_BC7_UNORM:
  1735. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM : GL_COMPRESSED_RGBA_BPTC_UNORM;
  1736. break;
  1737. case PIXELFORMAT_PVR1_RGB2_UNORM:
  1738. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  1739. break;
  1740. case PIXELFORMAT_PVR1_RGB4_UNORM:
  1741. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT : GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  1742. break;
  1743. case PIXELFORMAT_PVR1_RGBA2_UNORM:
  1744. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT : GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  1745. break;
  1746. case PIXELFORMAT_PVR1_RGBA4_UNORM:
  1747. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT : GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  1748. break;
  1749. case PIXELFORMAT_ETC1_UNORM:
  1750. isSRGB = false;
  1751. f.internalformat = GL_ETC1_RGB8_OES;
  1752. break;
  1753. case PIXELFORMAT_ETC2_RGB_UNORM:
  1754. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ETC2 : GL_COMPRESSED_RGB8_ETC2;
  1755. break;
  1756. case PIXELFORMAT_ETC2_RGBA_UNORM:
  1757. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : GL_COMPRESSED_RGBA8_ETC2_EAC;
  1758. break;
  1759. case PIXELFORMAT_ETC2_RGBA1_UNORM:
  1760. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  1761. break;
  1762. case PIXELFORMAT_EAC_R_UNORM:
  1763. isSRGB = false;
  1764. f.internalformat = GL_COMPRESSED_R11_EAC;
  1765. break;
  1766. case PIXELFORMAT_EAC_R_SNORM:
  1767. isSRGB = false;
  1768. f.internalformat = GL_COMPRESSED_SIGNED_R11_EAC;
  1769. break;
  1770. case PIXELFORMAT_EAC_RG_UNORM:
  1771. isSRGB = false;
  1772. f.internalformat = GL_COMPRESSED_RG11_EAC;
  1773. break;
  1774. case PIXELFORMAT_EAC_RG_SNORM:
  1775. isSRGB = false;
  1776. f.internalformat = GL_COMPRESSED_SIGNED_RG11_EAC;
  1777. break;
  1778. case PIXELFORMAT_ASTC_4x4:
  1779. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR : GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
  1780. break;
  1781. case PIXELFORMAT_ASTC_5x4:
  1782. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR : GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
  1783. break;
  1784. case PIXELFORMAT_ASTC_5x5:
  1785. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR : GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
  1786. break;
  1787. case PIXELFORMAT_ASTC_6x5:
  1788. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR : GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
  1789. break;
  1790. case PIXELFORMAT_ASTC_6x6:
  1791. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR : GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
  1792. break;
  1793. case PIXELFORMAT_ASTC_8x5:
  1794. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR : GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
  1795. break;
  1796. case PIXELFORMAT_ASTC_8x6:
  1797. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR : GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
  1798. break;
  1799. case PIXELFORMAT_ASTC_8x8:
  1800. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR : GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
  1801. break;
  1802. case PIXELFORMAT_ASTC_10x5:
  1803. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR : GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
  1804. break;
  1805. case PIXELFORMAT_ASTC_10x6:
  1806. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR : GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
  1807. break;
  1808. case PIXELFORMAT_ASTC_10x8:
  1809. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR : GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
  1810. break;
  1811. case PIXELFORMAT_ASTC_10x10:
  1812. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR : GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
  1813. break;
  1814. case PIXELFORMAT_ASTC_12x10:
  1815. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR : GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
  1816. break;
  1817. case PIXELFORMAT_ASTC_12x12:
  1818. f.internalformat = isSRGB ? GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR : GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
  1819. break;
  1820. default:
  1821. printf("Unhandled pixel format %d when converting to OpenGL enums!", pixelformat);
  1822. break;
  1823. }
  1824. if (!isPixelFormatCompressed(pixelformat))
  1825. {
  1826. // glTexImage in OpenGL ES 2 only accepts internal format enums that
  1827. // match the external format. GLES3 doesn't have that restriction -
  1828. // except for GL_LUMINANCE_ALPHA which doesn't have a sized version in
  1829. // ES3. However we always use RG8 for PIXELFORMAT_LA8 on GLES3 so it
  1830. // doesn't matter there.
  1831. // Also note that GLES2+extension sRGB format enums are different from
  1832. // desktop GL and GLES3+ (this is handled above).
  1833. if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0
  1834. && !renderbuffer && !isTexStorageSupported())
  1835. {
  1836. f.internalformat = f.externalformat;
  1837. }
  1838. if (!isPixelFormatSRGB(pixelformat))
  1839. isSRGB = false;
  1840. }
  1841. return f;
  1842. }
  1843. uint32 OpenGL::getPixelFormatUsageFlags(PixelFormat pixelformat)
  1844. {
  1845. const uint32 commonsample = PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_LINEAR;
  1846. const uint32 commonrender = PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_BLEND | PIXELFORMATUSAGEFLAGS_MSAA;
  1847. const uint32 computewrite = PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  1848. uint32 flags = PIXELFORMATUSAGEFLAGS_NONE;
  1849. switch (pixelformat)
  1850. {
  1851. case PIXELFORMAT_R8_UNORM:
  1852. case PIXELFORMAT_RG8_UNORM:
  1853. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_texture_rg || GLAD_EXT_texture_rg)
  1854. flags |= commonsample | commonrender;
  1855. else if (pixelformat == PIXELFORMAT_R8_UNORM && (GLAD_ES_VERSION_2_0 || GLAD_VERSION_1_1))
  1856. flags |= commonsample; // We'll use OpenGL's luminance format internally.
  1857. if (GLAD_VERSION_4_3)
  1858. flags |= computewrite;
  1859. break;
  1860. case PIXELFORMAT_RGBA8_UNORM:
  1861. flags |= commonsample;
  1862. if (GLAD_VERSION_1_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_rgb8_rgba8 || GLAD_ARM_rgba8)
  1863. flags |= commonrender;
  1864. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1865. flags |= computewrite;
  1866. break;
  1867. case PIXELFORMAT_RGBA8_UNORM_sRGB:
  1868. if (gl.bugs.brokenSRGB)
  1869. break;
  1870. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_2_1 || GLAD_EXT_texture_sRGB)
  1871. flags |= commonsample;
  1872. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0
  1873. || ((GLAD_ARB_framebuffer_sRGB || GLAD_EXT_framebuffer_sRGB) && (GLAD_VERSION_2_1 || GLAD_EXT_texture_sRGB)))
  1874. flags |= commonrender;
  1875. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1876. flags |= computewrite;
  1877. break;
  1878. case PIXELFORMAT_BGRA8_UNORM:
  1879. case PIXELFORMAT_BGRA8_UNORM_sRGB:
  1880. // Not supported right now.
  1881. break;
  1882. case PIXELFORMAT_R16_UNORM:
  1883. case PIXELFORMAT_RG16_UNORM:
  1884. if (GLAD_VERSION_3_0
  1885. || (GLAD_VERSION_1_1 && GLAD_ARB_texture_rg)
  1886. || (GLAD_EXT_texture_norm16 && (GLAD_ES_VERSION_3_0 || GLAD_EXT_texture_rg)))
  1887. flags |= commonsample | commonrender;
  1888. if (GLAD_VERSION_4_3)
  1889. flags |= computewrite;
  1890. break;
  1891. case PIXELFORMAT_RGBA16_UNORM:
  1892. if (GLAD_VERSION_1_1 || GLAD_EXT_texture_norm16)
  1893. flags |= commonsample | commonrender;
  1894. if (GLAD_VERSION_4_3)
  1895. flags |= computewrite;
  1896. break;
  1897. case PIXELFORMAT_R16_FLOAT:
  1898. case PIXELFORMAT_RG16_FLOAT:
  1899. if (GLAD_VERSION_1_0 && (GLAD_VERSION_3_0 || (GLAD_ARB_texture_float && GLAD_ARB_half_float_pixel && GLAD_ARB_texture_rg)))
  1900. flags |= commonsample | commonrender;
  1901. if (GLAD_ES_VERSION_3_0 || (GLAD_OES_texture_half_float && GLAD_EXT_texture_rg))
  1902. flags |= commonsample;
  1903. if (GLAD_EXT_color_buffer_half_float && (GLAD_ES_VERSION_3_0 || GLAD_EXT_texture_rg))
  1904. flags |= commonrender;
  1905. if (!(GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float_linear))
  1906. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1907. if (GLAD_VERSION_4_3)
  1908. flags |= computewrite;
  1909. break;
  1910. case PIXELFORMAT_RGBA16_FLOAT:
  1911. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float && GLAD_ARB_half_float_pixel))
  1912. flags |= commonsample | commonrender;
  1913. if (GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float)
  1914. flags |= commonsample;
  1915. if (GLAD_EXT_color_buffer_half_float)
  1916. flags |= commonrender;
  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 || GLAD_ES_VERSION_3_1)
  1920. flags |= computewrite;
  1921. break;
  1922. case PIXELFORMAT_R32_FLOAT:
  1923. if (GLAD_ES_VERSION_3_1)
  1924. flags |= computewrite;
  1925. // Fallthrough.
  1926. case PIXELFORMAT_RG32_FLOAT:
  1927. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float && GLAD_ARB_texture_rg))
  1928. flags |= commonsample | commonrender;
  1929. if (GLAD_ES_VERSION_3_0 || (GLAD_OES_texture_float && GLAD_EXT_texture_rg))
  1930. flags |= commonsample;
  1931. if (!(GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0 || GLAD_OES_texture_half_float_linear))
  1932. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1933. if (GLAD_VERSION_4_3)
  1934. flags |= computewrite;
  1935. break;
  1936. case PIXELFORMAT_RGBA32_FLOAT:
  1937. if (GLAD_VERSION_3_0 || (GLAD_VERSION_1_0 && GLAD_ARB_texture_float))
  1938. flags |= commonsample | commonrender;
  1939. if (GLAD_ES_VERSION_3_0 || GLAD_OES_texture_float)
  1940. flags |= commonsample;
  1941. if (!(GLAD_VERSION_1_1 || GLAD_OES_texture_float_linear))
  1942. flags &= ~PIXELFORMATUSAGEFLAGS_LINEAR;
  1943. if (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_1)
  1944. flags |= computewrite;
  1945. break;
  1946. case PIXELFORMAT_R8_INT:
  1947. case PIXELFORMAT_R8_UINT:
  1948. case PIXELFORMAT_RG8_INT:
  1949. case PIXELFORMAT_RG8_UINT:
  1950. case PIXELFORMAT_RGBA8_INT:
  1951. case PIXELFORMAT_RGBA8_UINT:
  1952. case PIXELFORMAT_R16_INT:
  1953. case PIXELFORMAT_R16_UINT:
  1954. case PIXELFORMAT_RG16_INT:
  1955. case PIXELFORMAT_RG16_UINT:
  1956. case PIXELFORMAT_RGBA16_INT:
  1957. case PIXELFORMAT_RGBA16_UINT:
  1958. case PIXELFORMAT_R32_INT:
  1959. case PIXELFORMAT_R32_UINT:
  1960. case PIXELFORMAT_RG32_INT:
  1961. case PIXELFORMAT_RG32_UINT:
  1962. case PIXELFORMAT_RGBA32_INT:
  1963. case PIXELFORMAT_RGBA32_UINT:
  1964. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0)
  1965. flags |= PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  1966. if (GLAD_VERSION_4_3)
  1967. flags |= computewrite;
  1968. if (GLAD_ES_VERSION_3_1)
  1969. {
  1970. switch (pixelformat)
  1971. {
  1972. case PIXELFORMAT_RGBA8_INT:
  1973. case PIXELFORMAT_RGBA8_UINT:
  1974. case PIXELFORMAT_RGBA16_INT:
  1975. case PIXELFORMAT_RGBA16_UINT:
  1976. case PIXELFORMAT_R32_INT:
  1977. case PIXELFORMAT_R32_UINT:
  1978. case PIXELFORMAT_RGBA32_INT:
  1979. case PIXELFORMAT_RGBA32_UINT:
  1980. flags |= computewrite;
  1981. break;
  1982. default:
  1983. break;
  1984. }
  1985. }
  1986. break;
  1987. case PIXELFORMAT_LA8_UNORM:
  1988. flags |= commonsample;
  1989. break;
  1990. case PIXELFORMAT_RGBA4_UNORM:
  1991. case PIXELFORMAT_RGB5A1_UNORM:
  1992. flags |= commonsample | commonrender;
  1993. break;
  1994. case PIXELFORMAT_RGB565_UNORM:
  1995. if (GLAD_ES_VERSION_2_0 || GLAD_VERSION_4_2 || GLAD_ARB_ES2_compatibility)
  1996. flags |= commonsample | commonrender;
  1997. break;
  1998. case PIXELFORMAT_RGB10A2_UNORM:
  1999. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_1_0)
  2000. flags |= commonsample | commonrender;
  2001. if (GLAD_VERSION_4_3)
  2002. flags |= computewrite;
  2003. break;
  2004. case PIXELFORMAT_RG11B10_FLOAT:
  2005. if (GLAD_VERSION_3_0 || GLAD_EXT_packed_float || GLAD_APPLE_texture_packed_float)
  2006. flags |= commonsample;
  2007. if (GLAD_VERSION_3_0 || GLAD_EXT_packed_float || GLAD_APPLE_color_buffer_packed_float)
  2008. flags |= commonrender;
  2009. if (GLAD_VERSION_4_3)
  2010. flags |= computewrite;
  2011. break;
  2012. case PIXELFORMAT_STENCIL8:
  2013. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2014. break;
  2015. case PIXELFORMAT_DEPTH16_UNORM:
  2016. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2017. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_depth_texture)
  2018. flags |= commonsample;
  2019. break;
  2020. case PIXELFORMAT_DEPTH24_UNORM:
  2021. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || GLAD_OES_depth24 || GLAD_OES_depth_texture)
  2022. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2023. if (GLAD_VERSION_2_0 || GLAD_ES_VERSION_3_0 || (GLAD_OES_depth_texture && (GLAD_OES_depth24 || GLAD_OES_depth_texture)))
  2024. flags |= commonsample;
  2025. break;
  2026. case PIXELFORMAT_DEPTH24_UNORM_STENCIL8:
  2027. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
  2028. flags |= PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2029. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_EXT_packed_depth_stencil || (GLAD_OES_depth_texture && GLAD_OES_packed_depth_stencil))
  2030. flags |= commonsample;
  2031. break;
  2032. case PIXELFORMAT_DEPTH32_FLOAT:
  2033. case PIXELFORMAT_DEPTH32_FLOAT_STENCIL8:
  2034. if (GLAD_VERSION_3_0 || GLAD_ES_VERSION_3_0 || GLAD_ARB_depth_buffer_float)
  2035. flags |= commonsample | PIXELFORMATUSAGEFLAGS_RENDERTARGET | PIXELFORMATUSAGEFLAGS_MSAA;
  2036. break;
  2037. case PIXELFORMAT_DXT1_UNORM:
  2038. if (GLAD_EXT_texture_compression_s3tc || GLAD_EXT_texture_compression_dxt1)
  2039. flags |= commonsample;
  2040. break;
  2041. case PIXELFORMAT_DXT3_UNORM:
  2042. if (GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt3)
  2043. flags |= commonsample;
  2044. break;
  2045. case PIXELFORMAT_DXT5_UNORM:
  2046. if (GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt5)
  2047. flags |= commonsample;
  2048. break;
  2049. case PIXELFORMAT_BC4_UNORM:
  2050. case PIXELFORMAT_BC4_SNORM:
  2051. case PIXELFORMAT_BC5_UNORM:
  2052. case PIXELFORMAT_BC5_SNORM:
  2053. if (GLAD_VERSION_3_0 || GLAD_ARB_texture_compression_rgtc || GLAD_EXT_texture_compression_rgtc)
  2054. flags |= commonsample;
  2055. break;
  2056. case PIXELFORMAT_BC6H_UFLOAT:
  2057. case PIXELFORMAT_BC6H_FLOAT:
  2058. case PIXELFORMAT_BC7_UNORM:
  2059. if (GLAD_VERSION_4_2 || GLAD_ARB_texture_compression_bptc)
  2060. flags |= commonsample;
  2061. break;
  2062. case PIXELFORMAT_PVR1_RGB2_UNORM:
  2063. case PIXELFORMAT_PVR1_RGB4_UNORM:
  2064. case PIXELFORMAT_PVR1_RGBA2_UNORM:
  2065. case PIXELFORMAT_PVR1_RGBA4_UNORM:
  2066. if (GLAD_IMG_texture_compression_pvrtc)
  2067. flags |= commonsample;
  2068. break;
  2069. case PIXELFORMAT_ETC1_UNORM:
  2070. // ETC2 support guarantees ETC1 support as well.
  2071. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility || GLAD_OES_compressed_ETC1_RGB8_texture)
  2072. flags |= commonsample;
  2073. break;
  2074. case PIXELFORMAT_ETC2_RGB_UNORM:
  2075. case PIXELFORMAT_ETC2_RGBA_UNORM:
  2076. case PIXELFORMAT_ETC2_RGBA1_UNORM:
  2077. case PIXELFORMAT_EAC_R_UNORM:
  2078. case PIXELFORMAT_EAC_R_SNORM:
  2079. case PIXELFORMAT_EAC_RG_UNORM:
  2080. case PIXELFORMAT_EAC_RG_SNORM:
  2081. if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility)
  2082. flags |= commonsample;
  2083. break;
  2084. case PIXELFORMAT_ASTC_4x4:
  2085. case PIXELFORMAT_ASTC_5x4:
  2086. case PIXELFORMAT_ASTC_5x5:
  2087. case PIXELFORMAT_ASTC_6x5:
  2088. case PIXELFORMAT_ASTC_6x6:
  2089. case PIXELFORMAT_ASTC_8x5:
  2090. case PIXELFORMAT_ASTC_8x6:
  2091. case PIXELFORMAT_ASTC_8x8:
  2092. case PIXELFORMAT_ASTC_10x5:
  2093. case PIXELFORMAT_ASTC_10x6:
  2094. case PIXELFORMAT_ASTC_10x8:
  2095. case PIXELFORMAT_ASTC_10x10:
  2096. case PIXELFORMAT_ASTC_12x10:
  2097. case PIXELFORMAT_ASTC_12x12:
  2098. if (GLAD_ES_VERSION_3_2 || GLAD_KHR_texture_compression_astc_ldr)
  2099. flags |= commonsample;
  2100. break;
  2101. case PIXELFORMAT_UNKNOWN:
  2102. case PIXELFORMAT_NORMAL:
  2103. case PIXELFORMAT_HDR:
  2104. case PIXELFORMAT_MAX_ENUM:
  2105. break;
  2106. }
  2107. return flags;
  2108. }
  2109. const char *OpenGL::errorString(GLenum errorcode)
  2110. {
  2111. switch (errorcode)
  2112. {
  2113. case GL_NO_ERROR:
  2114. return "no error";
  2115. case GL_INVALID_ENUM:
  2116. return "invalid enum";
  2117. case GL_INVALID_VALUE:
  2118. return "invalid value";
  2119. case GL_INVALID_OPERATION:
  2120. return "invalid operation";
  2121. case GL_OUT_OF_MEMORY:
  2122. return "out of memory";
  2123. case GL_INVALID_FRAMEBUFFER_OPERATION:
  2124. return "invalid framebuffer operation";
  2125. case GL_CONTEXT_LOST:
  2126. return "OpenGL context has been lost";
  2127. default:
  2128. break;
  2129. }
  2130. static char text[64] = {};
  2131. memset(text, 0, sizeof(text));
  2132. sprintf(text, "0x%x", errorcode);
  2133. return text;
  2134. }
  2135. const char *OpenGL::framebufferStatusString(GLenum status)
  2136. {
  2137. switch (status)
  2138. {
  2139. case GL_FRAMEBUFFER_COMPLETE:
  2140. return "complete (success)";
  2141. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  2142. return "Texture format cannot be rendered to on this system.";
  2143. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  2144. return "Error in graphics driver (missing render texture attachment)";
  2145. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  2146. return "Error in graphics driver (incomplete draw buffer)";
  2147. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  2148. return "Error in graphics driver (incomplete read buffer)";
  2149. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  2150. return "Texture with the specified MSAA count cannot be rendered to on this system.";
  2151. case GL_FRAMEBUFFER_UNSUPPORTED:
  2152. return "Renderable textures are unsupported";
  2153. default:
  2154. break;
  2155. }
  2156. static char text[64] = {};
  2157. memset(text, 0, sizeof(text));
  2158. sprintf(text, "0x%x", status);
  2159. return text;
  2160. }
  2161. const char *OpenGL::debugSeverityString(GLenum severity)
  2162. {
  2163. switch (severity)
  2164. {
  2165. case GL_DEBUG_SEVERITY_HIGH:
  2166. return "high";
  2167. case GL_DEBUG_SEVERITY_MEDIUM:
  2168. return "medium";
  2169. case GL_DEBUG_SEVERITY_LOW:
  2170. return "low";
  2171. default:
  2172. return "unknown";
  2173. }
  2174. }
  2175. const char *OpenGL::debugSourceString(GLenum source)
  2176. {
  2177. switch (source)
  2178. {
  2179. case GL_DEBUG_SOURCE_API:
  2180. return "API";
  2181. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  2182. return "window";
  2183. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  2184. return "shader";
  2185. case GL_DEBUG_SOURCE_THIRD_PARTY:
  2186. return "external";
  2187. case GL_DEBUG_SOURCE_APPLICATION:
  2188. return "LOVE";
  2189. case GL_DEBUG_SOURCE_OTHER:
  2190. return "other";
  2191. default:
  2192. return "unknown";
  2193. }
  2194. }
  2195. const char *OpenGL::debugTypeString(GLenum type)
  2196. {
  2197. switch (type)
  2198. {
  2199. case GL_DEBUG_TYPE_ERROR:
  2200. return "error";
  2201. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  2202. return "deprecated behavior";
  2203. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  2204. return "undefined behavior";
  2205. case GL_DEBUG_TYPE_PERFORMANCE:
  2206. return "performance";
  2207. case GL_DEBUG_TYPE_PORTABILITY:
  2208. return "portability";
  2209. case GL_DEBUG_TYPE_OTHER:
  2210. return "other";
  2211. default:
  2212. return "unknown";
  2213. }
  2214. }
  2215. // OpenGL class instance singleton.
  2216. OpenGL gl;
  2217. } // opengl
  2218. } // graphics
  2219. } // love