SDL_render_gles2.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
  20. #include "SDL_hints.h"
  21. #include "SDL_opengles2.h"
  22. #include "../SDL_sysrender.h"
  23. #include "../../video/SDL_blit.h"
  24. #include "SDL_shaders_gles2.h"
  25. /* To prevent unnecessary window recreation,
  26. * these should match the defaults selected in SDL_GL_ResetAttributes
  27. */
  28. #define RENDERER_CONTEXT_MAJOR 2
  29. #define RENDERER_CONTEXT_MINOR 0
  30. /* Used to re-create the window with OpenGL ES capability */
  31. extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
  32. /*************************************************************************************************
  33. * Context structures *
  34. *************************************************************************************************/
  35. typedef struct GLES2_FBOList GLES2_FBOList;
  36. struct GLES2_FBOList
  37. {
  38. Uint32 w, h;
  39. GLuint FBO;
  40. GLES2_FBOList *next;
  41. };
  42. typedef struct GLES2_TextureData
  43. {
  44. GLenum texture;
  45. GLenum texture_type;
  46. GLenum pixel_format;
  47. GLenum pixel_type;
  48. void *pixel_data;
  49. int pitch;
  50. #if SDL_HAVE_YUV
  51. /* YUV texture support */
  52. SDL_bool yuv;
  53. SDL_bool nv12;
  54. GLenum texture_v;
  55. GLenum texture_u;
  56. #endif
  57. GLES2_FBOList *fbo;
  58. } GLES2_TextureData;
  59. typedef struct GLES2_ProgramCacheEntry
  60. {
  61. GLuint id;
  62. GLuint vertex_shader;
  63. GLuint fragment_shader;
  64. GLuint uniform_locations[16];
  65. GLfloat projection[4][4];
  66. struct GLES2_ProgramCacheEntry *prev;
  67. struct GLES2_ProgramCacheEntry *next;
  68. } GLES2_ProgramCacheEntry;
  69. typedef struct GLES2_ProgramCache
  70. {
  71. int count;
  72. GLES2_ProgramCacheEntry *head;
  73. GLES2_ProgramCacheEntry *tail;
  74. } GLES2_ProgramCache;
  75. typedef enum
  76. {
  77. GLES2_ATTRIBUTE_POSITION = 0,
  78. GLES2_ATTRIBUTE_COLOR = 1,
  79. GLES2_ATTRIBUTE_TEXCOORD = 2,
  80. GLES2_ATTRIBUTE_ANGLE = 3,
  81. GLES2_ATTRIBUTE_CENTER = 4,
  82. } GLES2_Attribute;
  83. typedef enum
  84. {
  85. GLES2_UNIFORM_PROJECTION,
  86. GLES2_UNIFORM_TEXTURE,
  87. GLES2_UNIFORM_TEXTURE_U,
  88. GLES2_UNIFORM_TEXTURE_V
  89. } GLES2_Uniform;
  90. typedef enum
  91. {
  92. GLES2_IMAGESOURCE_INVALID,
  93. GLES2_IMAGESOURCE_SOLID,
  94. GLES2_IMAGESOURCE_TEXTURE_ABGR,
  95. GLES2_IMAGESOURCE_TEXTURE_ARGB,
  96. GLES2_IMAGESOURCE_TEXTURE_RGB,
  97. GLES2_IMAGESOURCE_TEXTURE_BGR,
  98. GLES2_IMAGESOURCE_TEXTURE_YUV,
  99. GLES2_IMAGESOURCE_TEXTURE_NV12,
  100. GLES2_IMAGESOURCE_TEXTURE_NV21,
  101. GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES
  102. } GLES2_ImageSource;
  103. typedef struct
  104. {
  105. SDL_Rect viewport;
  106. SDL_bool viewport_dirty;
  107. SDL_Texture *texture;
  108. SDL_Texture *target;
  109. SDL_BlendMode blend;
  110. SDL_bool cliprect_enabled_dirty;
  111. SDL_bool cliprect_enabled;
  112. SDL_bool cliprect_dirty;
  113. SDL_Rect cliprect;
  114. SDL_bool texturing;
  115. SDL_bool is_copy_ex;
  116. Uint32 clear_color;
  117. int drawablew;
  118. int drawableh;
  119. GLES2_ProgramCacheEntry *program;
  120. GLfloat projection[4][4];
  121. } GLES2_DrawStateCache;
  122. typedef struct GLES2_RenderData
  123. {
  124. SDL_GLContext *context;
  125. SDL_bool debug_enabled;
  126. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  127. #include "SDL_gles2funcs.h"
  128. #undef SDL_PROC
  129. GLES2_FBOList *framebuffers;
  130. GLuint window_framebuffer;
  131. GLuint shader_id_cache[GLES2_SHADER_COUNT];
  132. GLES2_ProgramCache program_cache;
  133. Uint8 clear_r, clear_g, clear_b, clear_a;
  134. GLuint vertex_buffers[8];
  135. size_t vertex_buffer_size[8];
  136. int current_vertex_buffer;
  137. GLES2_DrawStateCache drawstate;
  138. } GLES2_RenderData;
  139. #define GLES2_MAX_CACHED_PROGRAMS 8
  140. static const float inv255f = 1.0f / 255.0f;
  141. SDL_FORCE_INLINE const char*
  142. GL_TranslateError (GLenum error)
  143. {
  144. #define GL_ERROR_TRANSLATE(e) case e: return #e;
  145. switch (error) {
  146. GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
  147. GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
  148. GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
  149. GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
  150. GL_ERROR_TRANSLATE(GL_NO_ERROR)
  151. default:
  152. return "UNKNOWN";
  153. }
  154. #undef GL_ERROR_TRANSLATE
  155. }
  156. SDL_FORCE_INLINE void
  157. GL_ClearErrors(SDL_Renderer *renderer)
  158. {
  159. GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
  160. if (!data->debug_enabled) {
  161. return;
  162. }
  163. while (data->glGetError() != GL_NO_ERROR) {
  164. /* continue; */
  165. }
  166. }
  167. SDL_FORCE_INLINE int
  168. GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
  169. {
  170. GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
  171. int ret = 0;
  172. if (!data->debug_enabled) {
  173. return 0;
  174. }
  175. /* check gl errors (can return multiple errors) */
  176. for (;;) {
  177. GLenum error = data->glGetError();
  178. if (error != GL_NO_ERROR) {
  179. if (prefix == NULL || prefix[0] == '\0') {
  180. prefix = "generic";
  181. }
  182. SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
  183. ret = -1;
  184. } else {
  185. break;
  186. }
  187. }
  188. return ret;
  189. }
  190. #if 0
  191. #define GL_CheckError(prefix, renderer)
  192. #else
  193. #define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, SDL_FILE, SDL_LINE, SDL_FUNCTION)
  194. #endif
  195. /*************************************************************************************************
  196. * Renderer state APIs *
  197. *************************************************************************************************/
  198. static int GLES2_LoadFunctions(GLES2_RenderData * data)
  199. {
  200. #if SDL_VIDEO_DRIVER_UIKIT
  201. #define __SDL_NOGETPROCADDR__
  202. #elif SDL_VIDEO_DRIVER_ANDROID
  203. #define __SDL_NOGETPROCADDR__
  204. #elif SDL_VIDEO_DRIVER_PANDORA
  205. #define __SDL_NOGETPROCADDR__
  206. #endif
  207. #if defined __SDL_NOGETPROCADDR__
  208. #define SDL_PROC(ret,func,params) data->func=func;
  209. #else
  210. #define SDL_PROC(ret,func,params) \
  211. do { \
  212. data->func = SDL_GL_GetProcAddress(#func); \
  213. if ( ! data->func ) { \
  214. return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
  215. } \
  216. } while ( 0 );
  217. #endif /* __SDL_NOGETPROCADDR__ */
  218. #include "SDL_gles2funcs.h"
  219. #undef SDL_PROC
  220. return 0;
  221. }
  222. static GLES2_FBOList *
  223. GLES2_GetFBO(GLES2_RenderData *data, Uint32 w, Uint32 h)
  224. {
  225. GLES2_FBOList *result = data->framebuffers;
  226. while ((result) && ((result->w != w) || (result->h != h)) ) {
  227. result = result->next;
  228. }
  229. if (result == NULL) {
  230. result = SDL_malloc(sizeof(GLES2_FBOList));
  231. result->w = w;
  232. result->h = h;
  233. data->glGenFramebuffers(1, &result->FBO);
  234. result->next = data->framebuffers;
  235. data->framebuffers = result;
  236. }
  237. return result;
  238. }
  239. static int
  240. GLES2_ActivateRenderer(SDL_Renderer * renderer)
  241. {
  242. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  243. if (SDL_GL_GetCurrentContext() != data->context) {
  244. /* Null out the current program to ensure we set it again */
  245. data->drawstate.program = NULL;
  246. if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
  247. return -1;
  248. }
  249. }
  250. GL_ClearErrors(renderer);
  251. return 0;
  252. }
  253. static void
  254. GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  255. {
  256. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  257. if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
  258. /* According to Apple documentation, we need to finish drawing NOW! */
  259. data->glFinish();
  260. }
  261. }
  262. static int
  263. GLES2_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
  264. {
  265. SDL_GL_GetDrawableSize(renderer->window, w, h);
  266. return 0;
  267. }
  268. static GLenum GetBlendFunc(SDL_BlendFactor factor)
  269. {
  270. switch (factor) {
  271. case SDL_BLENDFACTOR_ZERO:
  272. return GL_ZERO;
  273. case SDL_BLENDFACTOR_ONE:
  274. return GL_ONE;
  275. case SDL_BLENDFACTOR_SRC_COLOR:
  276. return GL_SRC_COLOR;
  277. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
  278. return GL_ONE_MINUS_SRC_COLOR;
  279. case SDL_BLENDFACTOR_SRC_ALPHA:
  280. return GL_SRC_ALPHA;
  281. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
  282. return GL_ONE_MINUS_SRC_ALPHA;
  283. case SDL_BLENDFACTOR_DST_COLOR:
  284. return GL_DST_COLOR;
  285. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
  286. return GL_ONE_MINUS_DST_COLOR;
  287. case SDL_BLENDFACTOR_DST_ALPHA:
  288. return GL_DST_ALPHA;
  289. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
  290. return GL_ONE_MINUS_DST_ALPHA;
  291. default:
  292. return GL_INVALID_ENUM;
  293. }
  294. }
  295. static GLenum GetBlendEquation(SDL_BlendOperation operation)
  296. {
  297. switch (operation) {
  298. case SDL_BLENDOPERATION_ADD:
  299. return GL_FUNC_ADD;
  300. case SDL_BLENDOPERATION_SUBTRACT:
  301. return GL_FUNC_SUBTRACT;
  302. case SDL_BLENDOPERATION_REV_SUBTRACT:
  303. return GL_FUNC_REVERSE_SUBTRACT;
  304. default:
  305. return GL_INVALID_ENUM;
  306. }
  307. }
  308. static SDL_bool
  309. GLES2_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  310. {
  311. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  312. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  313. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  314. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  315. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  316. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  317. if (GetBlendFunc(srcColorFactor) == GL_INVALID_ENUM ||
  318. GetBlendFunc(srcAlphaFactor) == GL_INVALID_ENUM ||
  319. GetBlendEquation(colorOperation) == GL_INVALID_ENUM ||
  320. GetBlendFunc(dstColorFactor) == GL_INVALID_ENUM ||
  321. GetBlendFunc(dstAlphaFactor) == GL_INVALID_ENUM ||
  322. GetBlendEquation(alphaOperation) == GL_INVALID_ENUM) {
  323. return SDL_FALSE;
  324. }
  325. return SDL_TRUE;
  326. }
  327. static GLES2_ProgramCacheEntry *
  328. GLES2_CacheProgram(GLES2_RenderData *data, GLuint vertex, GLuint fragment)
  329. {
  330. GLES2_ProgramCacheEntry *entry;
  331. GLint linkSuccessful;
  332. /* Check if we've already cached this program */
  333. entry = data->program_cache.head;
  334. while (entry) {
  335. if (entry->vertex_shader == vertex && entry->fragment_shader == fragment) {
  336. break;
  337. }
  338. entry = entry->next;
  339. }
  340. if (entry) {
  341. if (data->program_cache.head != entry) {
  342. if (entry->next) {
  343. entry->next->prev = entry->prev;
  344. }
  345. if (entry->prev) {
  346. entry->prev->next = entry->next;
  347. }
  348. entry->prev = NULL;
  349. entry->next = data->program_cache.head;
  350. data->program_cache.head->prev = entry;
  351. data->program_cache.head = entry;
  352. }
  353. return entry;
  354. }
  355. /* Create a program cache entry */
  356. entry = (GLES2_ProgramCacheEntry *)SDL_calloc(1, sizeof(GLES2_ProgramCacheEntry));
  357. if (!entry) {
  358. SDL_OutOfMemory();
  359. return NULL;
  360. }
  361. entry->vertex_shader = vertex;
  362. entry->fragment_shader = fragment;
  363. /* Create the program and link it */
  364. entry->id = data->glCreateProgram();
  365. data->glAttachShader(entry->id, vertex);
  366. data->glAttachShader(entry->id, fragment);
  367. data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_POSITION, "a_position");
  368. data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_COLOR, "a_color");
  369. data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
  370. data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_ANGLE, "a_angle");
  371. data->glBindAttribLocation(entry->id, GLES2_ATTRIBUTE_CENTER, "a_center");
  372. data->glLinkProgram(entry->id);
  373. data->glGetProgramiv(entry->id, GL_LINK_STATUS, &linkSuccessful);
  374. if (!linkSuccessful) {
  375. data->glDeleteProgram(entry->id);
  376. SDL_free(entry);
  377. SDL_SetError("Failed to link shader program");
  378. return NULL;
  379. }
  380. /* Predetermine locations of uniform variables */
  381. entry->uniform_locations[GLES2_UNIFORM_PROJECTION] =
  382. data->glGetUniformLocation(entry->id, "u_projection");
  383. entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V] =
  384. data->glGetUniformLocation(entry->id, "u_texture_v");
  385. entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U] =
  386. data->glGetUniformLocation(entry->id, "u_texture_u");
  387. entry->uniform_locations[GLES2_UNIFORM_TEXTURE] =
  388. data->glGetUniformLocation(entry->id, "u_texture");
  389. data->glUseProgram(entry->id);
  390. if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V] != -1) {
  391. data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE_V], 2); /* always texture unit 2. */
  392. }
  393. if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U] != -1) {
  394. data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE_U], 1); /* always texture unit 1. */
  395. }
  396. if (entry->uniform_locations[GLES2_UNIFORM_TEXTURE] != -1) {
  397. data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE], 0); /* always texture unit 0. */
  398. }
  399. if (entry->uniform_locations[GLES2_UNIFORM_PROJECTION] != -1) {
  400. data->glUniformMatrix4fv(entry->uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (GLfloat *)entry->projection);
  401. }
  402. /* Cache the linked program */
  403. if (data->program_cache.head) {
  404. entry->next = data->program_cache.head;
  405. data->program_cache.head->prev = entry;
  406. } else {
  407. data->program_cache.tail = entry;
  408. }
  409. data->program_cache.head = entry;
  410. ++data->program_cache.count;
  411. /* Evict the last entry from the cache if we exceed the limit */
  412. if (data->program_cache.count > GLES2_MAX_CACHED_PROGRAMS) {
  413. data->glDeleteProgram(data->program_cache.tail->id);
  414. data->program_cache.tail = data->program_cache.tail->prev;
  415. if (data->program_cache.tail != NULL) {
  416. SDL_free(data->program_cache.tail->next);
  417. data->program_cache.tail->next = NULL;
  418. }
  419. --data->program_cache.count;
  420. }
  421. return entry;
  422. }
  423. static GLuint
  424. GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type, GLenum shader_type)
  425. {
  426. GLuint id;
  427. GLint compileSuccessful = GL_FALSE;
  428. const Uint8 *shader_src = GLES2_GetShader(type);
  429. if (!shader_src) {
  430. SDL_SetError("No shader src");
  431. return 0;
  432. }
  433. /* Compile */
  434. id = data->glCreateShader(shader_type);
  435. data->glShaderSource(id, 1, (const char**)&shader_src, NULL);
  436. data->glCompileShader(id);
  437. data->glGetShaderiv(id, GL_COMPILE_STATUS, &compileSuccessful);
  438. if (!compileSuccessful) {
  439. SDL_bool isstack = SDL_FALSE;
  440. char *info = NULL;
  441. int length = 0;
  442. data->glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  443. if (length > 0) {
  444. info = SDL_small_alloc(char, length, &isstack);
  445. if (info) {
  446. data->glGetShaderInfoLog(id, length, &length, info);
  447. }
  448. }
  449. if (info) {
  450. SDL_SetError("Failed to load the shader: %s", info);
  451. SDL_small_free(info, isstack);
  452. } else {
  453. SDL_SetError("Failed to load the shader");
  454. }
  455. data->glDeleteShader(id);
  456. return 0;
  457. }
  458. /* Cache */
  459. data->shader_id_cache[(Uint32)type] = id;
  460. return id;
  461. }
  462. static int
  463. GLES2_SelectProgram(GLES2_RenderData *data, GLES2_ImageSource source, int w, int h)
  464. {
  465. GLuint vertex;
  466. GLuint fragment;
  467. GLES2_ShaderType vtype, ftype;
  468. GLES2_ProgramCacheEntry *program;
  469. /* Select an appropriate shader pair for the specified modes */
  470. vtype = GLES2_SHADER_VERTEX_DEFAULT;
  471. switch (source) {
  472. case GLES2_IMAGESOURCE_SOLID:
  473. ftype = GLES2_SHADER_FRAGMENT_SOLID;
  474. break;
  475. case GLES2_IMAGESOURCE_TEXTURE_ABGR:
  476. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR;
  477. break;
  478. case GLES2_IMAGESOURCE_TEXTURE_ARGB:
  479. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB;
  480. break;
  481. case GLES2_IMAGESOURCE_TEXTURE_RGB:
  482. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_RGB;
  483. break;
  484. case GLES2_IMAGESOURCE_TEXTURE_BGR:
  485. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_BGR;
  486. break;
  487. case GLES2_IMAGESOURCE_TEXTURE_YUV:
  488. switch (SDL_GetYUVConversionModeForResolution(w, h)) {
  489. case SDL_YUV_CONVERSION_JPEG:
  490. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_JPEG;
  491. break;
  492. case SDL_YUV_CONVERSION_BT601:
  493. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_BT601;
  494. break;
  495. case SDL_YUV_CONVERSION_BT709:
  496. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_YUV_BT709;
  497. break;
  498. default:
  499. SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
  500. goto fault;
  501. }
  502. break;
  503. case GLES2_IMAGESOURCE_TEXTURE_NV12:
  504. switch (SDL_GetYUVConversionModeForResolution(w, h)) {
  505. case SDL_YUV_CONVERSION_JPEG:
  506. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_JPEG;
  507. break;
  508. case SDL_YUV_CONVERSION_BT601:
  509. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_BT601;
  510. break;
  511. case SDL_YUV_CONVERSION_BT709:
  512. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV12_BT709;
  513. break;
  514. default:
  515. SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
  516. goto fault;
  517. }
  518. break;
  519. case GLES2_IMAGESOURCE_TEXTURE_NV21:
  520. switch (SDL_GetYUVConversionModeForResolution(w, h)) {
  521. case SDL_YUV_CONVERSION_JPEG:
  522. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_JPEG;
  523. break;
  524. case SDL_YUV_CONVERSION_BT601:
  525. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT601;
  526. break;
  527. case SDL_YUV_CONVERSION_BT709:
  528. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT709;
  529. break;
  530. default:
  531. SDL_SetError("Unsupported YUV conversion mode: %d\n", SDL_GetYUVConversionModeForResolution(w, h));
  532. goto fault;
  533. }
  534. break;
  535. case GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES:
  536. ftype = GLES2_SHADER_FRAGMENT_TEXTURE_EXTERNAL_OES;
  537. break;
  538. default:
  539. goto fault;
  540. }
  541. /* Load the requested shaders */
  542. vertex = data->shader_id_cache[(Uint32)vtype];
  543. if (!vertex) {
  544. vertex = GLES2_CacheShader(data, vtype, GL_VERTEX_SHADER);
  545. if (!vertex) {
  546. goto fault;
  547. }
  548. }
  549. fragment = data->shader_id_cache[(Uint32)ftype];
  550. if (!fragment) {
  551. fragment = GLES2_CacheShader(data, ftype, GL_FRAGMENT_SHADER);
  552. if (!fragment) {
  553. goto fault;
  554. }
  555. }
  556. /* Check if we need to change programs at all */
  557. if (data->drawstate.program &&
  558. data->drawstate.program->vertex_shader == vertex &&
  559. data->drawstate.program->fragment_shader == fragment) {
  560. return 0;
  561. }
  562. /* Generate a matching program */
  563. program = GLES2_CacheProgram(data, vertex, fragment);
  564. if (!program) {
  565. goto fault;
  566. }
  567. /* Select that program in OpenGL */
  568. data->glUseProgram(program->id);
  569. /* Set the current program */
  570. data->drawstate.program = program;
  571. /* Clean up and return */
  572. return 0;
  573. fault:
  574. data->drawstate.program = NULL;
  575. return -1;
  576. }
  577. static int
  578. GLES2_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
  579. {
  580. return 0; /* nothing to do in this backend. */
  581. }
  582. static int
  583. GLES2_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  584. {
  585. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  586. const float r = (colorswap ? cmd->data.draw.b : cmd->data.draw.r) * inv255f;
  587. const float g = cmd->data.draw.g * inv255f;
  588. const float b = (colorswap ? cmd->data.draw.r : cmd->data.draw.b) * inv255f;
  589. const float a = cmd->data.draw.a * inv255f;
  590. GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * (2 + 4) * sizeof (GLfloat), 0, &cmd->data.draw.first);
  591. int i;
  592. if (!verts) {
  593. return -1;
  594. }
  595. cmd->data.draw.count = count;
  596. for (i = 0; i < count; i++) {
  597. *(verts++) = 0.5f + points[i].x;
  598. *(verts++) = 0.5f + points[i].y;
  599. *(verts++) = r;
  600. *(verts++) = g;
  601. *(verts++) = b;
  602. *(verts++) = a;
  603. }
  604. return 0;
  605. }
  606. static int
  607. GLES2_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  608. {
  609. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  610. const float r = (colorswap ? cmd->data.draw.b : cmd->data.draw.r) * inv255f;
  611. const float g = cmd->data.draw.g * inv255f;
  612. const float b = (colorswap ? cmd->data.draw.r : cmd->data.draw.b) * inv255f;
  613. const float a = cmd->data.draw.a * inv255f;
  614. int i;
  615. const size_t vertlen = sizeof (GLfloat) * (2 + 4) * count;
  616. GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
  617. if (!verts) {
  618. return -1;
  619. }
  620. cmd->data.draw.count = count;
  621. /* Offset to hit the center of the pixel. */
  622. for (i = 0; i < count; i++) {
  623. *(verts++) = 0.5f + points[i].x;
  624. *(verts++) = 0.5f + points[i].y;
  625. *(verts++) = r;
  626. *(verts++) = g;
  627. *(verts++) = b;
  628. *(verts++) = a;
  629. }
  630. /* Make the last line segment one pixel longer, to satisfy the
  631. diamond-exit rule. */
  632. verts -= 2 + 4 + 2 + 4;
  633. {
  634. const GLfloat xstart = verts[0];
  635. const GLfloat ystart = verts[1];
  636. const GLfloat xend = verts[2 + 4];
  637. const GLfloat yend = verts[3 + 4];
  638. if (ystart == yend) { /* horizontal line */
  639. verts[(xend > xstart) ? 2 + 4: 0] += 1.0f;
  640. } else if (xstart == xend) { /* vertical line */
  641. verts[(yend > ystart) ? 3 + 4: 1] += 1.0f;
  642. } else { /* bump a pixel in the direction we are moving in. */
  643. const GLfloat deltax = xend - xstart;
  644. const GLfloat deltay = yend - ystart;
  645. const GLfloat angle = SDL_atan2f(deltay, deltax);
  646. verts[2 + 4] += SDL_cosf(angle);
  647. verts[3 + 4] += SDL_sinf(angle);
  648. }
  649. }
  650. return 0;
  651. }
  652. static int
  653. GLES2_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
  654. {
  655. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  656. const float r = (colorswap ? cmd->data.draw.b : cmd->data.draw.r) * inv255f;
  657. const float g = cmd->data.draw.g * inv255f;
  658. const float b = (colorswap ? cmd->data.draw.r : cmd->data.draw.b) * inv255f;
  659. const float a = cmd->data.draw.a * inv255f;
  660. GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 4 * (2 + 4) * sizeof (GLfloat), 0, &cmd->data.draw.first);
  661. int i;
  662. if (!verts) {
  663. return -1;
  664. }
  665. cmd->data.draw.count = count;
  666. for (i = 0; i < count; i++) {
  667. const SDL_FRect *rect = &rects[i];
  668. const GLfloat minx = rect->x;
  669. const GLfloat maxx = rect->x + rect->w;
  670. const GLfloat miny = rect->y;
  671. const GLfloat maxy = rect->y + rect->h;
  672. *(verts++) = minx;
  673. *(verts++) = miny;
  674. *(verts++) = r;
  675. *(verts++) = g;
  676. *(verts++) = b;
  677. *(verts++) = a;
  678. *(verts++) = maxx;
  679. *(verts++) = miny;
  680. *(verts++) = r;
  681. *(verts++) = g;
  682. *(verts++) = b;
  683. *(verts++) = a;
  684. *(verts++) = minx;
  685. *(verts++) = maxy;
  686. *(verts++) = r;
  687. *(verts++) = g;
  688. *(verts++) = b;
  689. *(verts++) = a;
  690. *(verts++) = maxx;
  691. *(verts++) = maxy;
  692. *(verts++) = r;
  693. *(verts++) = g;
  694. *(verts++) = b;
  695. *(verts++) = a;
  696. }
  697. return 0;
  698. }
  699. static int
  700. GLES2_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  701. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  702. {
  703. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  704. const float r = (colorswap ? cmd->data.draw.b : cmd->data.draw.r) * inv255f;
  705. const float g = cmd->data.draw.g * inv255f;
  706. const float b = (colorswap ? cmd->data.draw.r : cmd->data.draw.b) * inv255f;
  707. const float a = cmd->data.draw.a * inv255f;
  708. GLfloat minx, miny, maxx, maxy;
  709. GLfloat minu, maxu, minv, maxv;
  710. GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 4 * (2 + 4 + 2) * sizeof (GLfloat), 0, &cmd->data.draw.first);
  711. if (!verts) {
  712. return -1;
  713. }
  714. cmd->data.draw.count = 1;
  715. minx = dstrect->x;
  716. miny = dstrect->y;
  717. maxx = dstrect->x + dstrect->w;
  718. maxy = dstrect->y + dstrect->h;
  719. minu = (GLfloat) srcrect->x / texture->w;
  720. maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
  721. minv = (GLfloat) srcrect->y / texture->h;
  722. maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
  723. *(verts++) = minx;
  724. *(verts++) = miny;
  725. *(verts++) = r;
  726. *(verts++) = g;
  727. *(verts++) = b;
  728. *(verts++) = a;
  729. *(verts++) = minu;
  730. *(verts++) = minv;
  731. *(verts++) = maxx;
  732. *(verts++) = miny;
  733. *(verts++) = r;
  734. *(verts++) = g;
  735. *(verts++) = b;
  736. *(verts++) = a;
  737. *(verts++) = maxu;
  738. *(verts++) = minv;
  739. *(verts++) = minx;
  740. *(verts++) = maxy;
  741. *(verts++) = r;
  742. *(verts++) = g;
  743. *(verts++) = b;
  744. *(verts++) = a;
  745. *(verts++) = minu;
  746. *(verts++) = maxv;
  747. *(verts++) = maxx;
  748. *(verts++) = maxy;
  749. *(verts++) = r;
  750. *(verts++) = g;
  751. *(verts++) = b;
  752. *(verts++) = a;
  753. *(verts++) = maxu;
  754. *(verts++) = maxv;
  755. return 0;
  756. }
  757. static int
  758. GLES2_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  759. const SDL_Rect * srcquad, const SDL_FRect * dstrect,
  760. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  761. {
  762. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  763. const float r = (colorswap ? cmd->data.draw.b : cmd->data.draw.r) * inv255f;
  764. const float g = cmd->data.draw.g * inv255f;
  765. const float b = (colorswap ? cmd->data.draw.r : cmd->data.draw.b) * inv255f;
  766. const float a = cmd->data.draw.a * inv255f;
  767. /* render expects cos value - 1 (see GLES2_Vertex_Default) */
  768. const float radian_angle = (float)(M_PI * (360.0 - angle) / 180.0);
  769. const GLfloat s = (GLfloat) SDL_sin(radian_angle);
  770. const GLfloat c = (GLfloat) SDL_cos(radian_angle) - 1.0f;
  771. const GLfloat centerx = center->x + dstrect->x;
  772. const GLfloat centery = center->y + dstrect->y;
  773. GLfloat minx, miny, maxx, maxy;
  774. GLfloat minu, maxu, minv, maxv;
  775. GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 4 * (2 + 4 + 2 + 2 + 2) * sizeof (GLfloat), 0, &cmd->data.draw.first);
  776. if (!verts) {
  777. return -1;
  778. }
  779. if (flip & SDL_FLIP_HORIZONTAL) {
  780. minx = dstrect->x + dstrect->w;
  781. maxx = dstrect->x;
  782. } else {
  783. minx = dstrect->x;
  784. maxx = dstrect->x + dstrect->w;
  785. }
  786. if (flip & SDL_FLIP_VERTICAL) {
  787. miny = dstrect->y + dstrect->h;
  788. maxy = dstrect->y;
  789. } else {
  790. miny = dstrect->y;
  791. maxy = dstrect->y + dstrect->h;
  792. }
  793. minu = ((GLfloat) srcquad->x) / ((GLfloat) texture->w);
  794. maxu = ((GLfloat) (srcquad->x + srcquad->w)) / ((GLfloat) texture->w);
  795. minv = ((GLfloat) srcquad->y) / ((GLfloat) texture->h);
  796. maxv = ((GLfloat) (srcquad->y + srcquad->h)) / ((GLfloat) texture->h);
  797. cmd->data.draw.count = 1;
  798. *(verts++) = minx;
  799. *(verts++) = miny;
  800. *(verts++) = r;
  801. *(verts++) = g;
  802. *(verts++) = b;
  803. *(verts++) = a;
  804. *(verts++) = minu;
  805. *(verts++) = minv;
  806. *(verts++) = s;
  807. *(verts++) = c;
  808. *(verts++) = centerx;
  809. *(verts++) = centery;
  810. *(verts++) = maxx;
  811. *(verts++) = miny;
  812. *(verts++) = r;
  813. *(verts++) = g;
  814. *(verts++) = b;
  815. *(verts++) = a;
  816. *(verts++) = maxu;
  817. *(verts++) = minv;
  818. *(verts++) = s;
  819. *(verts++) = c;
  820. *(verts++) = centerx;
  821. *(verts++) = centery;
  822. *(verts++) = minx;
  823. *(verts++) = maxy;
  824. *(verts++) = r;
  825. *(verts++) = g;
  826. *(verts++) = b;
  827. *(verts++) = a;
  828. *(verts++) = minu;
  829. *(verts++) = maxv;
  830. *(verts++) = s;
  831. *(verts++) = c;
  832. *(verts++) = centerx;
  833. *(verts++) = centery;
  834. *(verts++) = maxx;
  835. *(verts++) = maxy;
  836. *(verts++) = r;
  837. *(verts++) = g;
  838. *(verts++) = b;
  839. *(verts++) = a;
  840. *(verts++) = maxu;
  841. *(verts++) = maxv;
  842. *(verts++) = s;
  843. *(verts++) = c;
  844. *(verts++) = centerx;
  845. *(verts++) = centery;
  846. return 0;
  847. }
  848. static int
  849. GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  850. SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y)
  851. {
  852. int i;
  853. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  854. int count = indices ? num_indices : num_vertices;
  855. GLfloat *verts;
  856. int sz = 2 + 4 + (texture ? 2 : 0);
  857. verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * sz * sizeof (GLfloat), 0, &cmd->data.draw.first);
  858. if (!verts) {
  859. return -1;
  860. }
  861. cmd->data.draw.count = count;
  862. for (i = 0; i < count; i++) {
  863. SDL_Vertex *v = &vertices[indices ? indices[i] : i];
  864. *(verts++) = v->position.x * scale_x;
  865. *(verts++) = v->position.y * scale_y;
  866. *(verts++) = (colorswap ? v->color.b : v->color.r) * inv255f;
  867. *(verts++) = v->color.g * inv255f;
  868. *(verts++) = (colorswap ? v->color.r : v->color.b) * inv255f;
  869. *(verts++) = v->color.a * inv255f;
  870. if (texture) {
  871. *(verts++) = v->tex_coord.x;
  872. *(verts++) = v->tex_coord.y;
  873. }
  874. }
  875. return 0;
  876. }
  877. static int
  878. SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_ImageSource imgsrc)
  879. {
  880. const SDL_bool was_copy_ex = data->drawstate.is_copy_ex;
  881. const SDL_bool is_copy_ex = (cmd->command == SDL_RENDERCMD_COPY_EX);
  882. SDL_Texture *texture = cmd->data.draw.texture;
  883. const SDL_BlendMode blend = cmd->data.draw.blend;
  884. GLES2_ProgramCacheEntry *program;
  885. int stride = sizeof (GLfloat) * (2 /* position */ + 4 /* color */);
  886. SDL_assert((texture != NULL) == (imgsrc != GLES2_IMAGESOURCE_SOLID));
  887. if (data->drawstate.viewport_dirty) {
  888. const SDL_Rect *viewport = &data->drawstate.viewport;
  889. data->glViewport(viewport->x,
  890. data->drawstate.target ? viewport->y : (data->drawstate.drawableh - viewport->y - viewport->h),
  891. viewport->w, viewport->h);
  892. if (viewport->w && viewport->h) {
  893. data->drawstate.projection[0][0] = 2.0f / viewport->w;
  894. data->drawstate.projection[1][1] = (data->drawstate.target ? 2.0f : -2.0f) / viewport->h;
  895. data->drawstate.projection[3][1] = data->drawstate.target ? -1.0f : 1.0f;
  896. }
  897. data->drawstate.viewport_dirty = SDL_FALSE;
  898. }
  899. if (data->drawstate.cliprect_enabled_dirty) {
  900. if (!data->drawstate.cliprect_enabled) {
  901. data->glDisable(GL_SCISSOR_TEST);
  902. } else {
  903. data->glEnable(GL_SCISSOR_TEST);
  904. }
  905. data->drawstate.cliprect_enabled_dirty = SDL_FALSE;
  906. }
  907. if (data->drawstate.cliprect_enabled && data->drawstate.cliprect_dirty) {
  908. const SDL_Rect *viewport = &data->drawstate.viewport;
  909. const SDL_Rect *rect = &data->drawstate.cliprect;
  910. data->glScissor(viewport->x + rect->x,
  911. data->drawstate.target ? viewport->y + rect->y : data->drawstate.drawableh - viewport->y - rect->y - rect->h,
  912. rect->w, rect->h);
  913. data->drawstate.cliprect_dirty = SDL_FALSE;
  914. }
  915. if (texture != data->drawstate.texture) {
  916. if ((texture != NULL) != data->drawstate.texturing) {
  917. if (texture == NULL) {
  918. data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
  919. data->drawstate.texturing = SDL_FALSE;
  920. } else {
  921. data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
  922. data->drawstate.texturing = SDL_TRUE;
  923. }
  924. }
  925. if (texture) {
  926. GLES2_TextureData *tdata = (GLES2_TextureData *) texture->driverdata;
  927. #if SDL_HAVE_YUV
  928. if (tdata->yuv) {
  929. data->glActiveTexture(GL_TEXTURE2);
  930. data->glBindTexture(tdata->texture_type, tdata->texture_v);
  931. data->glActiveTexture(GL_TEXTURE1);
  932. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  933. data->glActiveTexture(GL_TEXTURE0);
  934. } else if (tdata->nv12) {
  935. data->glActiveTexture(GL_TEXTURE1);
  936. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  937. data->glActiveTexture(GL_TEXTURE0);
  938. }
  939. #endif
  940. data->glBindTexture(tdata->texture_type, tdata->texture);
  941. }
  942. data->drawstate.texture = texture;
  943. }
  944. if (texture) {
  945. stride += sizeof (GLfloat) * 2; /* tex coord */
  946. }
  947. if (is_copy_ex) {
  948. stride += sizeof (GLfloat) * (2 /* angle sin/cos */ + 2 /* center */);
  949. }
  950. if (texture) {
  951. data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + (sizeof (GLfloat) * 8)));
  952. }
  953. if (GLES2_SelectProgram(data, imgsrc, texture ? texture->w : 0, texture ? texture->h : 0) < 0) {
  954. return -1;
  955. }
  956. program = data->drawstate.program;
  957. if (program->uniform_locations[GLES2_UNIFORM_PROJECTION] != -1) {
  958. if (SDL_memcmp(program->projection, data->drawstate.projection, sizeof (data->drawstate.projection)) != 0) {
  959. data->glUniformMatrix4fv(program->uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (GLfloat *)data->drawstate.projection);
  960. SDL_memcpy(program->projection, data->drawstate.projection, sizeof (data->drawstate.projection));
  961. }
  962. }
  963. if (blend != data->drawstate.blend) {
  964. if (blend == SDL_BLENDMODE_NONE) {
  965. data->glDisable(GL_BLEND);
  966. } else {
  967. data->glEnable(GL_BLEND);
  968. data->glBlendFuncSeparate(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)),
  969. GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)),
  970. GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)),
  971. GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
  972. data->glBlendEquationSeparate(GetBlendEquation(SDL_GetBlendModeColorOperation(blend)),
  973. GetBlendEquation(SDL_GetBlendModeAlphaOperation(blend)));
  974. }
  975. data->drawstate.blend = blend;
  976. }
  977. /* all drawing commands use this */
  978. data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) cmd->data.draw.first);
  979. data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) (cmd->data.draw.first + sizeof (GLfloat) * 2));
  980. if (is_copy_ex != was_copy_ex) {
  981. if (is_copy_ex) {
  982. data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_ANGLE);
  983. data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_CENTER);
  984. } else {
  985. data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_ANGLE);
  986. data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_CENTER);
  987. }
  988. data->drawstate.is_copy_ex = is_copy_ex;
  989. }
  990. if (is_copy_ex) {
  991. data->glVertexAttribPointer(GLES2_ATTRIBUTE_ANGLE, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) (cmd->data.draw.first + sizeof (GLfloat) * (2 + 4 + 2)));
  992. data->glVertexAttribPointer(GLES2_ATTRIBUTE_CENTER, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) (cmd->data.draw.first + sizeof (GLfloat) * (2 + 4 + 2 + 2)));
  993. }
  994. return 0;
  995. }
  996. static int
  997. SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd)
  998. {
  999. GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
  1000. GLES2_ImageSource sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
  1001. SDL_Texture *texture = cmd->data.draw.texture;
  1002. /* Pick an appropriate shader */
  1003. if (renderer->target) {
  1004. /* Check if we need to do color mapping between the source and render target textures */
  1005. if (renderer->target->format != texture->format) {
  1006. switch (texture->format) {
  1007. case SDL_PIXELFORMAT_ARGB8888:
  1008. switch (renderer->target->format) {
  1009. case SDL_PIXELFORMAT_ABGR8888:
  1010. case SDL_PIXELFORMAT_BGR888:
  1011. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1012. break;
  1013. case SDL_PIXELFORMAT_RGB888:
  1014. sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
  1015. break;
  1016. }
  1017. break;
  1018. case SDL_PIXELFORMAT_ABGR8888:
  1019. switch (renderer->target->format) {
  1020. case SDL_PIXELFORMAT_ARGB8888:
  1021. case SDL_PIXELFORMAT_RGB888:
  1022. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1023. break;
  1024. case SDL_PIXELFORMAT_BGR888:
  1025. sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
  1026. break;
  1027. }
  1028. break;
  1029. case SDL_PIXELFORMAT_RGB888:
  1030. switch (renderer->target->format) {
  1031. case SDL_PIXELFORMAT_ABGR8888:
  1032. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1033. break;
  1034. case SDL_PIXELFORMAT_ARGB8888:
  1035. sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
  1036. break;
  1037. case SDL_PIXELFORMAT_BGR888:
  1038. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1039. break;
  1040. }
  1041. break;
  1042. case SDL_PIXELFORMAT_BGR888:
  1043. switch (renderer->target->format) {
  1044. case SDL_PIXELFORMAT_ABGR8888:
  1045. sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
  1046. break;
  1047. case SDL_PIXELFORMAT_ARGB8888:
  1048. sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
  1049. break;
  1050. case SDL_PIXELFORMAT_RGB888:
  1051. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1052. break;
  1053. }
  1054. break;
  1055. case SDL_PIXELFORMAT_IYUV:
  1056. case SDL_PIXELFORMAT_YV12:
  1057. sourceType = GLES2_IMAGESOURCE_TEXTURE_YUV;
  1058. break;
  1059. case SDL_PIXELFORMAT_NV12:
  1060. sourceType = GLES2_IMAGESOURCE_TEXTURE_NV12;
  1061. break;
  1062. case SDL_PIXELFORMAT_NV21:
  1063. sourceType = GLES2_IMAGESOURCE_TEXTURE_NV21;
  1064. break;
  1065. case SDL_PIXELFORMAT_EXTERNAL_OES:
  1066. sourceType = GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES;
  1067. break;
  1068. default:
  1069. return SDL_SetError("Unsupported texture format");
  1070. }
  1071. } else {
  1072. sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR; /* Texture formats match, use the non color mapping shader (even if the formats are not ABGR) */
  1073. }
  1074. } else {
  1075. switch (texture->format) {
  1076. case SDL_PIXELFORMAT_ARGB8888:
  1077. sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
  1078. break;
  1079. case SDL_PIXELFORMAT_ABGR8888:
  1080. sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
  1081. break;
  1082. case SDL_PIXELFORMAT_RGB888:
  1083. sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
  1084. break;
  1085. case SDL_PIXELFORMAT_BGR888:
  1086. sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
  1087. break;
  1088. case SDL_PIXELFORMAT_IYUV:
  1089. case SDL_PIXELFORMAT_YV12:
  1090. sourceType = GLES2_IMAGESOURCE_TEXTURE_YUV;
  1091. break;
  1092. case SDL_PIXELFORMAT_NV12:
  1093. sourceType = GLES2_IMAGESOURCE_TEXTURE_NV12;
  1094. break;
  1095. case SDL_PIXELFORMAT_NV21:
  1096. sourceType = GLES2_IMAGESOURCE_TEXTURE_NV21;
  1097. break;
  1098. case SDL_PIXELFORMAT_EXTERNAL_OES:
  1099. sourceType = GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES;
  1100. break;
  1101. default:
  1102. return SDL_SetError("Unsupported texture format");
  1103. }
  1104. }
  1105. return SetDrawState(data, cmd, sourceType);
  1106. }
  1107. static int
  1108. GLES2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  1109. {
  1110. GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
  1111. const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
  1112. const int vboidx = data->current_vertex_buffer;
  1113. const GLuint vbo = data->vertex_buffers[vboidx];
  1114. size_t i;
  1115. if (GLES2_ActivateRenderer(renderer) < 0) {
  1116. return -1;
  1117. }
  1118. data->drawstate.target = renderer->target;
  1119. if (!data->drawstate.target) {
  1120. SDL_GL_GetDrawableSize(renderer->window, &data->drawstate.drawablew, &data->drawstate.drawableh);
  1121. }
  1122. /* upload the new VBO data for this set of commands. */
  1123. data->glBindBuffer(GL_ARRAY_BUFFER, vbo);
  1124. if (data->vertex_buffer_size[vboidx] < vertsize) {
  1125. data->glBufferData(GL_ARRAY_BUFFER, vertsize, vertices, GL_STREAM_DRAW);
  1126. data->vertex_buffer_size[vboidx] = vertsize;
  1127. } else {
  1128. data->glBufferSubData(GL_ARRAY_BUFFER, 0, vertsize, vertices);
  1129. }
  1130. /* cycle through a few VBOs so the GL has some time with the data before we replace it. */
  1131. data->current_vertex_buffer++;
  1132. if (data->current_vertex_buffer >= SDL_arraysize(data->vertex_buffers)) {
  1133. data->current_vertex_buffer = 0;
  1134. }
  1135. while (cmd) {
  1136. switch (cmd->command) {
  1137. case SDL_RENDERCMD_SETDRAWCOLOR: {
  1138. break;
  1139. }
  1140. case SDL_RENDERCMD_SETVIEWPORT: {
  1141. SDL_Rect *viewport = &data->drawstate.viewport;
  1142. if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect)) != 0) {
  1143. SDL_memcpy(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect));
  1144. data->drawstate.viewport_dirty = SDL_TRUE;
  1145. }
  1146. break;
  1147. }
  1148. case SDL_RENDERCMD_SETCLIPRECT: {
  1149. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  1150. if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
  1151. data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
  1152. data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
  1153. }
  1154. if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof (SDL_Rect)) != 0) {
  1155. SDL_memcpy(&data->drawstate.cliprect, rect, sizeof (SDL_Rect));
  1156. data->drawstate.cliprect_dirty = SDL_TRUE;
  1157. }
  1158. break;
  1159. }
  1160. case SDL_RENDERCMD_CLEAR: {
  1161. const Uint8 r = colorswap ? cmd->data.color.b : cmd->data.color.r;
  1162. const Uint8 g = cmd->data.color.g;
  1163. const Uint8 b = colorswap ? cmd->data.color.r : cmd->data.color.b;
  1164. const Uint8 a = cmd->data.color.a;
  1165. const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
  1166. if (color != data->drawstate.clear_color) {
  1167. const GLfloat fr = ((GLfloat) r) * inv255f;
  1168. const GLfloat fg = ((GLfloat) g) * inv255f;
  1169. const GLfloat fb = ((GLfloat) b) * inv255f;
  1170. const GLfloat fa = ((GLfloat) a) * inv255f;
  1171. data->glClearColor(fr, fg, fb, fa);
  1172. data->drawstate.clear_color = color;
  1173. }
  1174. if (data->drawstate.cliprect_enabled || data->drawstate.cliprect_enabled_dirty) {
  1175. data->glDisable(GL_SCISSOR_TEST);
  1176. data->drawstate.cliprect_enabled_dirty = data->drawstate.cliprect_enabled;
  1177. }
  1178. data->glClear(GL_COLOR_BUFFER_BIT);
  1179. break;
  1180. }
  1181. case SDL_RENDERCMD_DRAW_POINTS: {
  1182. if (SetDrawState(data, cmd, GLES2_IMAGESOURCE_SOLID) == 0) {
  1183. data->glDrawArrays(GL_POINTS, 0, (GLsizei) cmd->data.draw.count);
  1184. }
  1185. break;
  1186. }
  1187. case SDL_RENDERCMD_DRAW_LINES: {
  1188. const size_t count = cmd->data.draw.count;
  1189. SDL_assert(count >= 2);
  1190. if (SetDrawState(data, cmd, GLES2_IMAGESOURCE_SOLID) == 0) {
  1191. data->glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) count);
  1192. }
  1193. break;
  1194. }
  1195. case SDL_RENDERCMD_FILL_RECTS: {
  1196. const size_t count = cmd->data.draw.count;
  1197. size_t offset = 0;
  1198. if (SetDrawState(data, cmd, GLES2_IMAGESOURCE_SOLID) == 0) {
  1199. for (i = 0; i < count; ++i, offset += 4) {
  1200. data->glDrawArrays(GL_TRIANGLE_STRIP, (GLsizei) offset, 4);
  1201. }
  1202. }
  1203. break;
  1204. }
  1205. case SDL_RENDERCMD_COPY:
  1206. case SDL_RENDERCMD_COPY_EX: {
  1207. if (SetCopyState(renderer, cmd) == 0) {
  1208. data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1209. }
  1210. break;
  1211. }
  1212. case SDL_RENDERCMD_GEOMETRY: {
  1213. SDL_Texture *texture = cmd->data.draw.texture;
  1214. const size_t count = cmd->data.draw.count;
  1215. int ret;
  1216. if (texture) {
  1217. ret = SetCopyState(renderer, cmd);
  1218. } else {
  1219. ret = SetDrawState(data, cmd, GLES2_IMAGESOURCE_SOLID);
  1220. }
  1221. if (ret == 0) {
  1222. data->glDrawArrays(GL_TRIANGLES, 0, count);
  1223. }
  1224. break;
  1225. }
  1226. case SDL_RENDERCMD_NO_OP:
  1227. break;
  1228. }
  1229. cmd = cmd->next;
  1230. }
  1231. return GL_CheckError("", renderer);
  1232. }
  1233. static void
  1234. GLES2_DestroyRenderer(SDL_Renderer *renderer)
  1235. {
  1236. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1237. /* Deallocate everything */
  1238. if (data) {
  1239. GLES2_ActivateRenderer(renderer);
  1240. {
  1241. int i;
  1242. for (i = 0; i < GLES2_SHADER_COUNT; i++) {
  1243. GLuint id = data->shader_id_cache[i];
  1244. if (id) {
  1245. data->glDeleteShader(id);
  1246. }
  1247. }
  1248. }
  1249. {
  1250. GLES2_ProgramCacheEntry *entry;
  1251. GLES2_ProgramCacheEntry *next;
  1252. entry = data->program_cache.head;
  1253. while (entry) {
  1254. data->glDeleteProgram(entry->id);
  1255. next = entry->next;
  1256. SDL_free(entry);
  1257. entry = next;
  1258. }
  1259. }
  1260. if (data->context) {
  1261. while (data->framebuffers) {
  1262. GLES2_FBOList *nextnode = data->framebuffers->next;
  1263. data->glDeleteFramebuffers(1, &data->framebuffers->FBO);
  1264. GL_CheckError("", renderer);
  1265. SDL_free(data->framebuffers);
  1266. data->framebuffers = nextnode;
  1267. }
  1268. data->glDeleteBuffers(SDL_arraysize(data->vertex_buffers), data->vertex_buffers);
  1269. GL_CheckError("", renderer);
  1270. SDL_GL_DeleteContext(data->context);
  1271. }
  1272. SDL_free(data);
  1273. }
  1274. SDL_free(renderer);
  1275. }
  1276. static int
  1277. GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1278. {
  1279. GLES2_RenderData *renderdata = (GLES2_RenderData *)renderer->driverdata;
  1280. GLES2_TextureData *data;
  1281. GLenum format;
  1282. GLenum type;
  1283. GLenum scaleMode;
  1284. GLES2_ActivateRenderer(renderer);
  1285. renderdata->drawstate.texture = NULL; /* we trash this state. */
  1286. /* Determine the corresponding GLES texture format params */
  1287. switch (texture->format)
  1288. {
  1289. case SDL_PIXELFORMAT_ARGB8888:
  1290. case SDL_PIXELFORMAT_ABGR8888:
  1291. case SDL_PIXELFORMAT_RGB888:
  1292. case SDL_PIXELFORMAT_BGR888:
  1293. format = GL_RGBA;
  1294. type = GL_UNSIGNED_BYTE;
  1295. break;
  1296. case SDL_PIXELFORMAT_IYUV:
  1297. case SDL_PIXELFORMAT_YV12:
  1298. case SDL_PIXELFORMAT_NV12:
  1299. case SDL_PIXELFORMAT_NV21:
  1300. format = GL_LUMINANCE;
  1301. type = GL_UNSIGNED_BYTE;
  1302. break;
  1303. #ifdef GL_TEXTURE_EXTERNAL_OES
  1304. case SDL_PIXELFORMAT_EXTERNAL_OES:
  1305. format = GL_NONE;
  1306. type = GL_NONE;
  1307. break;
  1308. #endif
  1309. default:
  1310. return SDL_SetError("Texture format not supported");
  1311. }
  1312. if (texture->format == SDL_PIXELFORMAT_EXTERNAL_OES &&
  1313. texture->access != SDL_TEXTUREACCESS_STATIC) {
  1314. return SDL_SetError("Unsupported texture access for SDL_PIXELFORMAT_EXTERNAL_OES");
  1315. }
  1316. /* Allocate a texture struct */
  1317. data = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
  1318. if (!data) {
  1319. return SDL_OutOfMemory();
  1320. }
  1321. data->texture = 0;
  1322. #ifdef GL_TEXTURE_EXTERNAL_OES
  1323. data->texture_type = (texture->format == SDL_PIXELFORMAT_EXTERNAL_OES) ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
  1324. #else
  1325. data->texture_type = GL_TEXTURE_2D;
  1326. #endif
  1327. data->pixel_format = format;
  1328. data->pixel_type = type;
  1329. #if SDL_HAVE_YUV
  1330. data->yuv = ((texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12));
  1331. data->nv12 = ((texture->format == SDL_PIXELFORMAT_NV12) || (texture->format == SDL_PIXELFORMAT_NV21));
  1332. data->texture_u = 0;
  1333. data->texture_v = 0;
  1334. #endif
  1335. scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? GL_NEAREST : GL_LINEAR;
  1336. /* Allocate a blob for image renderdata */
  1337. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1338. size_t size;
  1339. data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
  1340. size = texture->h * data->pitch;
  1341. #if SDL_HAVE_YUV
  1342. if (data->yuv) {
  1343. /* Need to add size for the U and V planes */
  1344. size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
  1345. } else if (data->nv12) {
  1346. /* Need to add size for the U/V plane */
  1347. size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
  1348. }
  1349. #endif
  1350. data->pixel_data = SDL_calloc(1, size);
  1351. if (!data->pixel_data) {
  1352. SDL_free(data);
  1353. return SDL_OutOfMemory();
  1354. }
  1355. }
  1356. /* Allocate the texture */
  1357. GL_CheckError("", renderer);
  1358. #if SDL_HAVE_YUV
  1359. if (data->yuv) {
  1360. renderdata->glGenTextures(1, &data->texture_v);
  1361. if (GL_CheckError("glGenTexures()", renderer) < 0) {
  1362. return -1;
  1363. }
  1364. renderdata->glActiveTexture(GL_TEXTURE2);
  1365. renderdata->glBindTexture(data->texture_type, data->texture_v);
  1366. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
  1367. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
  1368. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1369. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1370. renderdata->glTexImage2D(data->texture_type, 0, format, (texture->w + 1) / 2, (texture->h + 1) / 2, 0, format, type, NULL);
  1371. renderdata->glGenTextures(1, &data->texture_u);
  1372. if (GL_CheckError("glGenTexures()", renderer) < 0) {
  1373. return -1;
  1374. }
  1375. renderdata->glActiveTexture(GL_TEXTURE1);
  1376. renderdata->glBindTexture(data->texture_type, data->texture_u);
  1377. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
  1378. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
  1379. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1380. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1381. renderdata->glTexImage2D(data->texture_type, 0, format, (texture->w + 1) / 2, (texture->h + 1) / 2, 0, format, type, NULL);
  1382. if (GL_CheckError("glTexImage2D()", renderer) < 0) {
  1383. return -1;
  1384. }
  1385. } else if (data->nv12) {
  1386. renderdata->glGenTextures(1, &data->texture_u);
  1387. if (GL_CheckError("glGenTexures()", renderer) < 0) {
  1388. return -1;
  1389. }
  1390. renderdata->glActiveTexture(GL_TEXTURE1);
  1391. renderdata->glBindTexture(data->texture_type, data->texture_u);
  1392. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
  1393. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
  1394. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1395. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1396. renderdata->glTexImage2D(data->texture_type, 0, GL_LUMINANCE_ALPHA, (texture->w + 1) / 2, (texture->h + 1) / 2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
  1397. if (GL_CheckError("glTexImage2D()", renderer) < 0) {
  1398. return -1;
  1399. }
  1400. }
  1401. #endif
  1402. renderdata->glGenTextures(1, &data->texture);
  1403. if (GL_CheckError("glGenTexures()", renderer) < 0) {
  1404. return -1;
  1405. }
  1406. texture->driverdata = data;
  1407. renderdata->glActiveTexture(GL_TEXTURE0);
  1408. renderdata->glBindTexture(data->texture_type, data->texture);
  1409. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
  1410. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
  1411. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1412. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1413. if (texture->format != SDL_PIXELFORMAT_EXTERNAL_OES) {
  1414. renderdata->glTexImage2D(data->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
  1415. if (GL_CheckError("glTexImage2D()", renderer) < 0) {
  1416. return -1;
  1417. }
  1418. }
  1419. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  1420. data->fbo = GLES2_GetFBO(renderer->driverdata, texture->w, texture->h);
  1421. } else {
  1422. data->fbo = NULL;
  1423. }
  1424. return GL_CheckError("", renderer);
  1425. }
  1426. static int
  1427. GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels, GLint pitch, GLint bpp)
  1428. {
  1429. Uint8 *blob = NULL;
  1430. Uint8 *src;
  1431. int src_pitch;
  1432. int y;
  1433. if ((width == 0) || (height == 0) || (bpp == 0)) {
  1434. return 0; /* nothing to do */
  1435. }
  1436. /* Reformat the texture data into a tightly packed array */
  1437. src_pitch = width * bpp;
  1438. src = (Uint8 *)pixels;
  1439. if (pitch != src_pitch) {
  1440. blob = (Uint8 *)SDL_malloc(src_pitch * height);
  1441. if (!blob) {
  1442. return SDL_OutOfMemory();
  1443. }
  1444. src = blob;
  1445. for (y = 0; y < height; ++y)
  1446. {
  1447. SDL_memcpy(src, pixels, src_pitch);
  1448. src += src_pitch;
  1449. pixels = (Uint8 *)pixels + pitch;
  1450. }
  1451. src = blob;
  1452. }
  1453. data->glTexSubImage2D(target, 0, xoffset, yoffset, width, height, format, type, src);
  1454. if (blob) {
  1455. SDL_free(blob);
  1456. }
  1457. return 0;
  1458. }
  1459. static int
  1460. GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
  1461. const void *pixels, int pitch)
  1462. {
  1463. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1464. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1465. GLES2_ActivateRenderer(renderer);
  1466. /* Bail out if we're supposed to update an empty rectangle */
  1467. if (rect->w <= 0 || rect->h <= 0) {
  1468. return 0;
  1469. }
  1470. data->drawstate.texture = NULL; /* we trash this state. */
  1471. /* Create a texture subimage with the supplied data */
  1472. data->glBindTexture(tdata->texture_type, tdata->texture);
  1473. GLES2_TexSubImage2D(data, tdata->texture_type,
  1474. rect->x,
  1475. rect->y,
  1476. rect->w,
  1477. rect->h,
  1478. tdata->pixel_format,
  1479. tdata->pixel_type,
  1480. pixels, pitch, SDL_BYTESPERPIXEL(texture->format));
  1481. #if SDL_HAVE_YUV
  1482. if (tdata->yuv) {
  1483. /* Skip to the correct offset into the next texture */
  1484. pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
  1485. if (texture->format == SDL_PIXELFORMAT_YV12) {
  1486. data->glBindTexture(tdata->texture_type, tdata->texture_v);
  1487. } else {
  1488. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  1489. }
  1490. GLES2_TexSubImage2D(data, tdata->texture_type,
  1491. rect->x / 2,
  1492. rect->y / 2,
  1493. (rect->w + 1) / 2,
  1494. (rect->h + 1) / 2,
  1495. tdata->pixel_format,
  1496. tdata->pixel_type,
  1497. pixels, (pitch + 1) / 2, 1);
  1498. /* Skip to the correct offset into the next texture */
  1499. pixels = (const void*)((const Uint8*)pixels + ((rect->h + 1) / 2) * ((pitch + 1)/2));
  1500. if (texture->format == SDL_PIXELFORMAT_YV12) {
  1501. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  1502. } else {
  1503. data->glBindTexture(tdata->texture_type, tdata->texture_v);
  1504. }
  1505. GLES2_TexSubImage2D(data, tdata->texture_type,
  1506. rect->x / 2,
  1507. rect->y / 2,
  1508. (rect->w + 1) / 2,
  1509. (rect->h + 1) / 2,
  1510. tdata->pixel_format,
  1511. tdata->pixel_type,
  1512. pixels, (pitch + 1) / 2, 1);
  1513. } else if (tdata->nv12) {
  1514. /* Skip to the correct offset into the next texture */
  1515. pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
  1516. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  1517. GLES2_TexSubImage2D(data, tdata->texture_type,
  1518. rect->x / 2,
  1519. rect->y / 2,
  1520. (rect->w + 1) / 2,
  1521. (rect->h + 1) / 2,
  1522. GL_LUMINANCE_ALPHA,
  1523. GL_UNSIGNED_BYTE,
  1524. pixels, 2 * ((pitch + 1) / 2), 2);
  1525. }
  1526. #endif
  1527. return GL_CheckError("glTexSubImage2D()", renderer);
  1528. }
  1529. #if SDL_HAVE_YUV
  1530. static int
  1531. GLES2_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  1532. const SDL_Rect * rect,
  1533. const Uint8 *Yplane, int Ypitch,
  1534. const Uint8 *Uplane, int Upitch,
  1535. const Uint8 *Vplane, int Vpitch)
  1536. {
  1537. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1538. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1539. GLES2_ActivateRenderer(renderer);
  1540. /* Bail out if we're supposed to update an empty rectangle */
  1541. if (rect->w <= 0 || rect->h <= 0) {
  1542. return 0;
  1543. }
  1544. data->drawstate.texture = NULL; /* we trash this state. */
  1545. data->glBindTexture(tdata->texture_type, tdata->texture_v);
  1546. GLES2_TexSubImage2D(data, tdata->texture_type,
  1547. rect->x / 2,
  1548. rect->y / 2,
  1549. (rect->w + 1) / 2,
  1550. (rect->h + 1) / 2,
  1551. tdata->pixel_format,
  1552. tdata->pixel_type,
  1553. Vplane, Vpitch, 1);
  1554. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  1555. GLES2_TexSubImage2D(data, tdata->texture_type,
  1556. rect->x / 2,
  1557. rect->y / 2,
  1558. (rect->w + 1) / 2,
  1559. (rect->h + 1) / 2,
  1560. tdata->pixel_format,
  1561. tdata->pixel_type,
  1562. Uplane, Upitch, 1);
  1563. data->glBindTexture(tdata->texture_type, tdata->texture);
  1564. GLES2_TexSubImage2D(data, tdata->texture_type,
  1565. rect->x,
  1566. rect->y,
  1567. rect->w,
  1568. rect->h,
  1569. tdata->pixel_format,
  1570. tdata->pixel_type,
  1571. Yplane, Ypitch, 1);
  1572. return GL_CheckError("glTexSubImage2D()", renderer);
  1573. }
  1574. static int
  1575. GLES2_UpdateTextureNV(SDL_Renderer * renderer, SDL_Texture * texture,
  1576. const SDL_Rect * rect,
  1577. const Uint8 *Yplane, int Ypitch,
  1578. const Uint8 *UVplane, int UVpitch)
  1579. {
  1580. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1581. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1582. GLES2_ActivateRenderer(renderer);
  1583. /* Bail out if we're supposed to update an empty rectangle */
  1584. if (rect->w <= 0 || rect->h <= 0) {
  1585. return 0;
  1586. }
  1587. data->drawstate.texture = NULL; /* we trash this state. */
  1588. data->glBindTexture(tdata->texture_type, tdata->texture_u);
  1589. GLES2_TexSubImage2D(data, tdata->texture_type,
  1590. rect->x / 2,
  1591. rect->y / 2,
  1592. (rect->w + 1) / 2,
  1593. (rect->h + 1) / 2,
  1594. GL_LUMINANCE_ALPHA,
  1595. GL_UNSIGNED_BYTE,
  1596. UVplane, UVpitch, 2);
  1597. data->glBindTexture(tdata->texture_type, tdata->texture);
  1598. GLES2_TexSubImage2D(data, tdata->texture_type,
  1599. rect->x,
  1600. rect->y,
  1601. rect->w,
  1602. rect->h,
  1603. tdata->pixel_format,
  1604. tdata->pixel_type,
  1605. Yplane, Ypitch, 1);
  1606. return GL_CheckError("glTexSubImage2D()", renderer);
  1607. }
  1608. #endif
  1609. static int
  1610. GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
  1611. void **pixels, int *pitch)
  1612. {
  1613. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1614. /* Retrieve the buffer/pitch for the specified region */
  1615. *pixels = (Uint8 *)tdata->pixel_data +
  1616. (tdata->pitch * rect->y) +
  1617. (rect->x * SDL_BYTESPERPIXEL(texture->format));
  1618. *pitch = tdata->pitch;
  1619. return 0;
  1620. }
  1621. static void
  1622. GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1623. {
  1624. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1625. SDL_Rect rect;
  1626. /* We do whole texture updates, at least for now */
  1627. rect.x = 0;
  1628. rect.y = 0;
  1629. rect.w = texture->w;
  1630. rect.h = texture->h;
  1631. GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
  1632. }
  1633. static void
  1634. GLES2_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
  1635. {
  1636. GLES2_RenderData *renderdata = (GLES2_RenderData *) renderer->driverdata;
  1637. GLES2_TextureData *data = (GLES2_TextureData *) texture->driverdata;
  1638. GLenum glScaleMode = (scaleMode == SDL_ScaleModeNearest) ? GL_NEAREST : GL_LINEAR;
  1639. #if SDL_HAVE_YUV
  1640. if (data->yuv) {
  1641. renderdata->glActiveTexture(GL_TEXTURE2);
  1642. renderdata->glBindTexture(data->texture_type, data->texture_v);
  1643. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, glScaleMode);
  1644. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, glScaleMode);
  1645. renderdata->glActiveTexture(GL_TEXTURE1);
  1646. renderdata->glBindTexture(data->texture_type, data->texture_u);
  1647. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, glScaleMode);
  1648. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, glScaleMode);
  1649. } else if (data->nv12) {
  1650. renderdata->glActiveTexture(GL_TEXTURE1);
  1651. renderdata->glBindTexture(data->texture_type, data->texture_u);
  1652. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, glScaleMode);
  1653. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, glScaleMode);
  1654. }
  1655. #endif
  1656. renderdata->glActiveTexture(GL_TEXTURE0);
  1657. renderdata->glBindTexture(data->texture_type, data->texture);
  1658. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, glScaleMode);
  1659. renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, glScaleMode);
  1660. }
  1661. static int
  1662. GLES2_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  1663. {
  1664. GLES2_RenderData *data = (GLES2_RenderData *) renderer->driverdata;
  1665. GLES2_TextureData *texturedata = NULL;
  1666. GLenum status;
  1667. data->drawstate.viewport_dirty = SDL_TRUE;
  1668. if (texture == NULL) {
  1669. data->glBindFramebuffer(GL_FRAMEBUFFER, data->window_framebuffer);
  1670. } else {
  1671. texturedata = (GLES2_TextureData *) texture->driverdata;
  1672. data->glBindFramebuffer(GL_FRAMEBUFFER, texturedata->fbo->FBO);
  1673. /* TODO: check if texture pixel format allows this operation */
  1674. data->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texturedata->texture_type, texturedata->texture, 0);
  1675. /* Check FBO status */
  1676. status = data->glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1677. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1678. return SDL_SetError("glFramebufferTexture2D() failed");
  1679. }
  1680. }
  1681. return 0;
  1682. }
  1683. static void
  1684. GLES2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1685. {
  1686. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1687. GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
  1688. GLES2_ActivateRenderer(renderer);
  1689. if (data->drawstate.texture == texture) {
  1690. data->drawstate.texture = NULL;
  1691. }
  1692. if (data->drawstate.target == texture) {
  1693. data->drawstate.target = NULL;
  1694. }
  1695. /* Destroy the texture */
  1696. if (tdata) {
  1697. data->glDeleteTextures(1, &tdata->texture);
  1698. #if SDL_HAVE_YUV
  1699. if (tdata->texture_v) {
  1700. data->glDeleteTextures(1, &tdata->texture_v);
  1701. }
  1702. if (tdata->texture_u) {
  1703. data->glDeleteTextures(1, &tdata->texture_u);
  1704. }
  1705. #endif
  1706. SDL_free(tdata->pixel_data);
  1707. SDL_free(tdata);
  1708. texture->driverdata = NULL;
  1709. }
  1710. }
  1711. static int
  1712. GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1713. Uint32 pixel_format, void * pixels, int pitch)
  1714. {
  1715. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1716. Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ABGR8888;
  1717. size_t buflen;
  1718. void *temp_pixels;
  1719. int temp_pitch;
  1720. Uint8 *src, *dst, *tmp;
  1721. int w, h, length, rows;
  1722. int status;
  1723. temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1724. buflen = rect->h * temp_pitch;
  1725. if (buflen == 0) {
  1726. return 0; /* nothing to do. */
  1727. }
  1728. temp_pixels = SDL_malloc(buflen);
  1729. if (!temp_pixels) {
  1730. return SDL_OutOfMemory();
  1731. }
  1732. SDL_GetRendererOutputSize(renderer, &w, &h);
  1733. data->glReadPixels(rect->x, renderer->target ? rect->y : (h-rect->y)-rect->h,
  1734. rect->w, rect->h, GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
  1735. if (GL_CheckError("glReadPixels()", renderer) < 0) {
  1736. return -1;
  1737. }
  1738. /* Flip the rows to be top-down if necessary */
  1739. if (!renderer->target) {
  1740. SDL_bool isstack;
  1741. length = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1742. src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
  1743. dst = (Uint8*)temp_pixels;
  1744. tmp = SDL_small_alloc(Uint8, length, &isstack);
  1745. rows = rect->h / 2;
  1746. while (rows--) {
  1747. SDL_memcpy(tmp, dst, length);
  1748. SDL_memcpy(dst, src, length);
  1749. SDL_memcpy(src, tmp, length);
  1750. dst += temp_pitch;
  1751. src -= temp_pitch;
  1752. }
  1753. SDL_small_free(tmp, isstack);
  1754. }
  1755. status = SDL_ConvertPixels(rect->w, rect->h,
  1756. temp_format, temp_pixels, temp_pitch,
  1757. pixel_format, pixels, pitch);
  1758. SDL_free(temp_pixels);
  1759. return status;
  1760. }
  1761. static void
  1762. GLES2_RenderPresent(SDL_Renderer *renderer)
  1763. {
  1764. /* Tell the video driver to swap buffers */
  1765. SDL_GL_SwapWindow(renderer->window);
  1766. }
  1767. /*************************************************************************************************
  1768. * Bind/unbinding of textures
  1769. *************************************************************************************************/
  1770. static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
  1771. static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
  1772. static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh)
  1773. {
  1774. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1775. GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
  1776. GLES2_ActivateRenderer(renderer);
  1777. data->glBindTexture(texturedata->texture_type, texturedata->texture);
  1778. data->drawstate.texture = texture;
  1779. if (texw) {
  1780. *texw = 1.0;
  1781. }
  1782. if (texh) {
  1783. *texh = 1.0;
  1784. }
  1785. return 0;
  1786. }
  1787. static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture)
  1788. {
  1789. GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
  1790. GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
  1791. GLES2_ActivateRenderer(renderer);
  1792. data->glBindTexture(texturedata->texture_type, 0);
  1793. data->drawstate.texture = NULL;
  1794. return 0;
  1795. }
  1796. /*************************************************************************************************
  1797. * Renderer instantiation *
  1798. *************************************************************************************************/
  1799. static SDL_Renderer *
  1800. GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
  1801. {
  1802. SDL_Renderer *renderer;
  1803. GLES2_RenderData *data;
  1804. Uint32 window_flags = 0; /* -Wconditional-uninitialized */
  1805. GLint window_framebuffer;
  1806. GLint value;
  1807. int profile_mask = 0, major = 0, minor = 0;
  1808. SDL_bool changed_window = SDL_FALSE;
  1809. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask) < 0) {
  1810. goto error;
  1811. }
  1812. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major) < 0) {
  1813. goto error;
  1814. }
  1815. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor) < 0) {
  1816. goto error;
  1817. }
  1818. window_flags = SDL_GetWindowFlags(window);
  1819. /* OpenGL ES 3.0 is a superset of OpenGL ES 2.0 */
  1820. if (!(window_flags & SDL_WINDOW_OPENGL) ||
  1821. profile_mask != SDL_GL_CONTEXT_PROFILE_ES || major < RENDERER_CONTEXT_MAJOR) {
  1822. changed_window = SDL_TRUE;
  1823. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  1824. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
  1825. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
  1826. if (SDL_RecreateWindow(window, (window_flags & ~(SDL_WINDOW_VULKAN | SDL_WINDOW_METAL)) | SDL_WINDOW_OPENGL) < 0) {
  1827. goto error;
  1828. }
  1829. }
  1830. /* Create the renderer struct */
  1831. renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
  1832. if (!renderer) {
  1833. SDL_OutOfMemory();
  1834. goto error;
  1835. }
  1836. data = (GLES2_RenderData *)SDL_calloc(1, sizeof(GLES2_RenderData));
  1837. if (!data) {
  1838. SDL_free(renderer);
  1839. SDL_OutOfMemory();
  1840. goto error;
  1841. }
  1842. renderer->info = GLES2_RenderDriver.info;
  1843. renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  1844. renderer->driverdata = data;
  1845. renderer->window = window;
  1846. /* Create an OpenGL ES 2.0 context */
  1847. data->context = SDL_GL_CreateContext(window);
  1848. if (!data->context) {
  1849. SDL_free(renderer);
  1850. SDL_free(data);
  1851. goto error;
  1852. }
  1853. if (SDL_GL_MakeCurrent(window, data->context) < 0) {
  1854. SDL_GL_DeleteContext(data->context);
  1855. SDL_free(renderer);
  1856. SDL_free(data);
  1857. goto error;
  1858. }
  1859. if (GLES2_LoadFunctions(data) < 0) {
  1860. SDL_GL_DeleteContext(data->context);
  1861. SDL_free(renderer);
  1862. SDL_free(data);
  1863. goto error;
  1864. }
  1865. #if __WINRT__
  1866. /* DLudwig, 2013-11-29: ANGLE for WinRT doesn't seem to work unless VSync
  1867. * is turned on. Not doing so will freeze the screen's contents to that
  1868. * of the first drawn frame.
  1869. */
  1870. flags |= SDL_RENDERER_PRESENTVSYNC;
  1871. #endif
  1872. if (flags & SDL_RENDERER_PRESENTVSYNC) {
  1873. SDL_GL_SetSwapInterval(1);
  1874. } else {
  1875. SDL_GL_SetSwapInterval(0);
  1876. }
  1877. if (SDL_GL_GetSwapInterval() > 0) {
  1878. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1879. }
  1880. /* Check for debug output support */
  1881. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
  1882. (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
  1883. data->debug_enabled = SDL_TRUE;
  1884. }
  1885. value = 0;
  1886. data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
  1887. renderer->info.max_texture_width = value;
  1888. value = 0;
  1889. data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
  1890. renderer->info.max_texture_height = value;
  1891. /* we keep a few of these and cycle through them, so data can live for a few frames. */
  1892. data->glGenBuffers(SDL_arraysize(data->vertex_buffers), data->vertex_buffers);
  1893. data->framebuffers = NULL;
  1894. data->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &window_framebuffer);
  1895. data->window_framebuffer = (GLuint)window_framebuffer;
  1896. /* Populate the function pointers for the module */
  1897. renderer->WindowEvent = GLES2_WindowEvent;
  1898. renderer->GetOutputSize = GLES2_GetOutputSize;
  1899. renderer->SupportsBlendMode = GLES2_SupportsBlendMode;
  1900. renderer->CreateTexture = GLES2_CreateTexture;
  1901. renderer->UpdateTexture = GLES2_UpdateTexture;
  1902. #if SDL_HAVE_YUV
  1903. renderer->UpdateTextureYUV = GLES2_UpdateTextureYUV;
  1904. renderer->UpdateTextureNV = GLES2_UpdateTextureNV;
  1905. #endif
  1906. renderer->LockTexture = GLES2_LockTexture;
  1907. renderer->UnlockTexture = GLES2_UnlockTexture;
  1908. renderer->SetTextureScaleMode = GLES2_SetTextureScaleMode;
  1909. renderer->SetRenderTarget = GLES2_SetRenderTarget;
  1910. renderer->QueueSetViewport = GLES2_QueueSetViewport;
  1911. renderer->QueueSetDrawColor = GLES2_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
  1912. renderer->QueueDrawPoints = GLES2_QueueDrawPoints;
  1913. renderer->QueueDrawLines = GLES2_QueueDrawLines;
  1914. renderer->QueueFillRects = GLES2_QueueFillRects;
  1915. renderer->QueueCopy = GLES2_QueueCopy;
  1916. renderer->QueueCopyEx = GLES2_QueueCopyEx;
  1917. renderer->QueueGeometry = GLES2_QueueGeometry;
  1918. renderer->RunCommandQueue = GLES2_RunCommandQueue;
  1919. renderer->RenderReadPixels = GLES2_RenderReadPixels;
  1920. renderer->RenderPresent = GLES2_RenderPresent;
  1921. renderer->DestroyTexture = GLES2_DestroyTexture;
  1922. renderer->DestroyRenderer = GLES2_DestroyRenderer;
  1923. renderer->GL_BindTexture = GLES2_BindTexture;
  1924. renderer->GL_UnbindTexture = GLES2_UnbindTexture;
  1925. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
  1926. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
  1927. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV12;
  1928. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV21;
  1929. #ifdef GL_TEXTURE_EXTERNAL_OES
  1930. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_EXTERNAL_OES;
  1931. #endif
  1932. /* Set up parameters for rendering */
  1933. data->glActiveTexture(GL_TEXTURE0);
  1934. data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1935. data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1936. data->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  1937. data->glEnableVertexAttribArray(GLES2_ATTRIBUTE_COLOR);
  1938. data->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
  1939. data->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  1940. data->drawstate.blend = SDL_BLENDMODE_INVALID;
  1941. data->drawstate.clear_color = 0xFFFFFFFF;
  1942. data->drawstate.projection[3][0] = -1.0f;
  1943. data->drawstate.projection[3][3] = 1.0f;
  1944. GL_CheckError("", renderer);
  1945. return renderer;
  1946. error:
  1947. if (changed_window) {
  1948. /* Uh oh, better try to put it back... */
  1949. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
  1950. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
  1951. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
  1952. SDL_RecreateWindow(window, window_flags);
  1953. }
  1954. return NULL;
  1955. }
  1956. SDL_RenderDriver GLES2_RenderDriver = {
  1957. GLES2_CreateRenderer,
  1958. {
  1959. "opengles2",
  1960. (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
  1961. 4,
  1962. {
  1963. SDL_PIXELFORMAT_ARGB8888,
  1964. SDL_PIXELFORMAT_ABGR8888,
  1965. SDL_PIXELFORMAT_RGB888,
  1966. SDL_PIXELFORMAT_BGR888
  1967. },
  1968. 0,
  1969. 0
  1970. }
  1971. };
  1972. #endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
  1973. /* vi: set ts=4 sw=4 expandtab: */