SDL_render_gl.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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_config.h"
  19. #if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
  20. #include "SDL_hints.h"
  21. #include "SDL_log.h"
  22. #include "SDL_opengl.h"
  23. #include "../SDL_sysrender.h"
  24. #include "SDL_shaders_gl.h"
  25. #ifdef __MACOSX__
  26. #include <OpenGL/OpenGL.h>
  27. #endif
  28. /* OpenGL renderer implementation */
  29. /* Details on optimizing the texture path on Mac OS X:
  30. http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
  31. */
  32. /* Used to re-create the window with OpenGL capability */
  33. extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
  34. static const float inv255f = 1.0f / 255.0f;
  35. static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
  36. static void GL_WindowEvent(SDL_Renderer * renderer,
  37. const SDL_WindowEvent *event);
  38. static int GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
  39. static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  40. static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  41. const SDL_Rect * rect, const void *pixels,
  42. int pitch);
  43. static int GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  44. const SDL_Rect * rect,
  45. const Uint8 *Yplane, int Ypitch,
  46. const Uint8 *Uplane, int Upitch,
  47. const Uint8 *Vplane, int Vpitch);
  48. static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  49. const SDL_Rect * rect, void **pixels, int *pitch);
  50. static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  51. static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
  52. static int GL_UpdateViewport(SDL_Renderer * renderer);
  53. static int GL_UpdateClipRect(SDL_Renderer * renderer);
  54. static int GL_RenderClear(SDL_Renderer * renderer);
  55. static int GL_RenderDrawPoints(SDL_Renderer * renderer,
  56. const SDL_FPoint * points, int count);
  57. static int GL_RenderDrawLines(SDL_Renderer * renderer,
  58. const SDL_FPoint * points, int count);
  59. static int GL_RenderFillRects(SDL_Renderer * renderer,
  60. const SDL_FRect * rects, int count);
  61. static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  62. const SDL_Rect * srcrect, const SDL_FRect * dstrect);
  63. static int GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  64. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  65. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
  66. static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  67. Uint32 pixel_format, void * pixels, int pitch);
  68. static void GL_RenderPresent(SDL_Renderer * renderer);
  69. static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  70. static void GL_DestroyRenderer(SDL_Renderer * renderer);
  71. static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
  72. static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
  73. SDL_RenderDriver GL_RenderDriver = {
  74. GL_CreateRenderer,
  75. {
  76. "opengl",
  77. (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
  78. 1,
  79. {SDL_PIXELFORMAT_ARGB8888},
  80. 0,
  81. 0}
  82. };
  83. typedef struct GL_FBOList GL_FBOList;
  84. struct GL_FBOList
  85. {
  86. Uint32 w, h;
  87. GLuint FBO;
  88. GL_FBOList *next;
  89. };
  90. typedef struct
  91. {
  92. SDL_GLContext context;
  93. SDL_bool debug_enabled;
  94. SDL_bool GL_ARB_debug_output_supported;
  95. int errors;
  96. char **error_messages;
  97. GLDEBUGPROCARB next_error_callback;
  98. GLvoid *next_error_userparam;
  99. SDL_bool GL_ARB_texture_rectangle_supported;
  100. struct {
  101. GL_Shader shader;
  102. Uint32 color;
  103. int blendMode;
  104. } current;
  105. SDL_bool GL_EXT_framebuffer_object_supported;
  106. GL_FBOList *framebuffers;
  107. /* OpenGL functions */
  108. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  109. #include "SDL_glfuncs.h"
  110. #undef SDL_PROC
  111. /* Multitexture support */
  112. SDL_bool GL_ARB_multitexture_supported;
  113. PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
  114. GLint num_texture_units;
  115. PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
  116. PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
  117. PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
  118. PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
  119. PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
  120. /* Shader support */
  121. GL_ShaderContext *shaders;
  122. } GL_RenderData;
  123. typedef struct
  124. {
  125. GLuint texture;
  126. GLenum type;
  127. GLfloat texw;
  128. GLfloat texh;
  129. GLenum format;
  130. GLenum formattype;
  131. void *pixels;
  132. int pitch;
  133. SDL_Rect locked_rect;
  134. /* YV12 texture support */
  135. SDL_bool yuv;
  136. GLuint utexture;
  137. GLuint vtexture;
  138. GL_FBOList *fbo;
  139. } GL_TextureData;
  140. SDL_FORCE_INLINE const char*
  141. GL_TranslateError (GLenum error)
  142. {
  143. #define GL_ERROR_TRANSLATE(e) case e: return #e;
  144. switch (error) {
  145. GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
  146. GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
  147. GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
  148. GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
  149. GL_ERROR_TRANSLATE(GL_NO_ERROR)
  150. GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
  151. GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
  152. GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
  153. default:
  154. return "UNKNOWN";
  155. }
  156. #undef GL_ERROR_TRANSLATE
  157. }
  158. SDL_FORCE_INLINE void
  159. GL_ClearErrors(SDL_Renderer *renderer)
  160. {
  161. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  162. if (!data->debug_enabled)
  163. {
  164. return;
  165. }
  166. if (data->GL_ARB_debug_output_supported) {
  167. if (data->errors) {
  168. int i;
  169. for (i = 0; i < data->errors; ++i) {
  170. SDL_free(data->error_messages[i]);
  171. }
  172. SDL_free(data->error_messages);
  173. data->errors = 0;
  174. data->error_messages = NULL;
  175. }
  176. } else {
  177. while (data->glGetError() != GL_NO_ERROR) {
  178. continue;
  179. }
  180. }
  181. }
  182. SDL_FORCE_INLINE int
  183. GL_CheckAllErrors (const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
  184. {
  185. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  186. int ret = 0;
  187. if (!data->debug_enabled)
  188. {
  189. return 0;
  190. }
  191. if (data->GL_ARB_debug_output_supported) {
  192. if (data->errors) {
  193. int i;
  194. for (i = 0; i < data->errors; ++i) {
  195. SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
  196. ret = -1;
  197. }
  198. GL_ClearErrors(renderer);
  199. }
  200. } else {
  201. /* check gl errors (can return multiple errors) */
  202. for (;;) {
  203. GLenum error = data->glGetError();
  204. if (error != GL_NO_ERROR) {
  205. if (prefix == NULL || prefix[0] == '\0') {
  206. prefix = "generic";
  207. }
  208. SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
  209. ret = -1;
  210. } else {
  211. break;
  212. }
  213. }
  214. }
  215. return ret;
  216. }
  217. #if 0
  218. #define GL_CheckError(prefix, renderer)
  219. #elif defined(_MSC_VER)
  220. #define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __FUNCTION__)
  221. #else
  222. #define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  223. #endif
  224. static int
  225. GL_LoadFunctions(GL_RenderData * data)
  226. {
  227. #ifdef __SDL_NOGETPROCADDR__
  228. #define SDL_PROC(ret,func,params) data->func=func;
  229. #else
  230. #define SDL_PROC(ret,func,params) \
  231. do { \
  232. data->func = SDL_GL_GetProcAddress(#func); \
  233. if ( ! data->func ) { \
  234. return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
  235. } \
  236. } while ( 0 );
  237. #endif /* __SDL_NOGETPROCADDR__ */
  238. #include "SDL_glfuncs.h"
  239. #undef SDL_PROC
  240. return 0;
  241. }
  242. static SDL_GLContext SDL_CurrentContext = NULL;
  243. static int
  244. GL_ActivateRenderer(SDL_Renderer * renderer)
  245. {
  246. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  247. if (SDL_CurrentContext != data->context) {
  248. if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
  249. return -1;
  250. }
  251. SDL_CurrentContext = data->context;
  252. GL_UpdateViewport(renderer);
  253. }
  254. GL_ClearErrors(renderer);
  255. return 0;
  256. }
  257. /* This is called if we need to invalidate all of the SDL OpenGL state */
  258. static void
  259. GL_ResetState(SDL_Renderer *renderer)
  260. {
  261. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  262. if (SDL_CurrentContext == data->context) {
  263. GL_UpdateViewport(renderer);
  264. } else {
  265. GL_ActivateRenderer(renderer);
  266. }
  267. data->current.shader = SHADER_NONE;
  268. data->current.color = 0;
  269. data->current.blendMode = -1;
  270. data->glDisable(GL_DEPTH_TEST);
  271. data->glDisable(GL_CULL_FACE);
  272. /* This ended up causing video discrepancies between OpenGL and Direct3D */
  273. /* data->glEnable(GL_LINE_SMOOTH); */
  274. data->glMatrixMode(GL_MODELVIEW);
  275. data->glLoadIdentity();
  276. GL_CheckError("", renderer);
  277. }
  278. static void APIENTRY
  279. GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, void *userParam)
  280. {
  281. SDL_Renderer *renderer = (SDL_Renderer *) userParam;
  282. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  283. if (type == GL_DEBUG_TYPE_ERROR_ARB) {
  284. /* Record this error */
  285. ++data->errors;
  286. data->error_messages = SDL_realloc(data->error_messages, data->errors * sizeof(*data->error_messages));
  287. if (data->error_messages) {
  288. data->error_messages[data->errors-1] = SDL_strdup(message);
  289. }
  290. }
  291. /* If there's another error callback, pass it along, otherwise log it */
  292. if (data->next_error_callback) {
  293. data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
  294. } else {
  295. if (type == GL_DEBUG_TYPE_ERROR_ARB) {
  296. SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
  297. } else {
  298. SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
  299. }
  300. }
  301. }
  302. static GL_FBOList *
  303. GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
  304. {
  305. GL_FBOList *result = data->framebuffers;
  306. while (result && ((result->w != w) || (result->h != h))) {
  307. result = result->next;
  308. }
  309. if (!result) {
  310. result = SDL_malloc(sizeof(GL_FBOList));
  311. if (result) {
  312. result->w = w;
  313. result->h = h;
  314. data->glGenFramebuffersEXT(1, &result->FBO);
  315. result->next = data->framebuffers;
  316. data->framebuffers = result;
  317. }
  318. }
  319. return result;
  320. }
  321. SDL_Renderer *
  322. GL_CreateRenderer(SDL_Window * window, Uint32 flags)
  323. {
  324. SDL_Renderer *renderer;
  325. GL_RenderData *data;
  326. const char *hint;
  327. GLint value;
  328. Uint32 window_flags;
  329. window_flags = SDL_GetWindowFlags(window);
  330. if (!(window_flags & SDL_WINDOW_OPENGL)) {
  331. if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
  332. /* Uh oh, better try to put it back... */
  333. SDL_RecreateWindow(window, window_flags);
  334. return NULL;
  335. }
  336. }
  337. renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  338. if (!renderer) {
  339. SDL_OutOfMemory();
  340. return NULL;
  341. }
  342. data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
  343. if (!data) {
  344. GL_DestroyRenderer(renderer);
  345. SDL_OutOfMemory();
  346. return NULL;
  347. }
  348. renderer->WindowEvent = GL_WindowEvent;
  349. renderer->GetOutputSize = GL_GetOutputSize;
  350. renderer->CreateTexture = GL_CreateTexture;
  351. renderer->UpdateTexture = GL_UpdateTexture;
  352. renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
  353. renderer->LockTexture = GL_LockTexture;
  354. renderer->UnlockTexture = GL_UnlockTexture;
  355. renderer->SetRenderTarget = GL_SetRenderTarget;
  356. renderer->UpdateViewport = GL_UpdateViewport;
  357. renderer->UpdateClipRect = GL_UpdateClipRect;
  358. renderer->RenderClear = GL_RenderClear;
  359. renderer->RenderDrawPoints = GL_RenderDrawPoints;
  360. renderer->RenderDrawLines = GL_RenderDrawLines;
  361. renderer->RenderFillRects = GL_RenderFillRects;
  362. renderer->RenderCopy = GL_RenderCopy;
  363. renderer->RenderCopyEx = GL_RenderCopyEx;
  364. renderer->RenderReadPixels = GL_RenderReadPixels;
  365. renderer->RenderPresent = GL_RenderPresent;
  366. renderer->DestroyTexture = GL_DestroyTexture;
  367. renderer->DestroyRenderer = GL_DestroyRenderer;
  368. renderer->GL_BindTexture = GL_BindTexture;
  369. renderer->GL_UnbindTexture = GL_UnbindTexture;
  370. renderer->info = GL_RenderDriver.info;
  371. renderer->info.flags = SDL_RENDERER_ACCELERATED;
  372. renderer->driverdata = data;
  373. renderer->window = window;
  374. data->context = SDL_GL_CreateContext(window);
  375. if (!data->context) {
  376. GL_DestroyRenderer(renderer);
  377. return NULL;
  378. }
  379. if (SDL_GL_MakeCurrent(window, data->context) < 0) {
  380. GL_DestroyRenderer(renderer);
  381. return NULL;
  382. }
  383. if (GL_LoadFunctions(data) < 0) {
  384. GL_DestroyRenderer(renderer);
  385. return NULL;
  386. }
  387. #ifdef __MACOSX__
  388. /* Enable multi-threaded rendering */
  389. /* Disabled until Ryan finishes his VBO/PBO code...
  390. CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
  391. */
  392. #endif
  393. if (flags & SDL_RENDERER_PRESENTVSYNC) {
  394. SDL_GL_SetSwapInterval(1);
  395. } else {
  396. SDL_GL_SetSwapInterval(0);
  397. }
  398. if (SDL_GL_GetSwapInterval() > 0) {
  399. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  400. }
  401. /* Check for debug output support */
  402. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
  403. (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
  404. data->debug_enabled = SDL_TRUE;
  405. }
  406. if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
  407. PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
  408. data->GL_ARB_debug_output_supported = SDL_TRUE;
  409. data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)&data->next_error_callback);
  410. data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
  411. glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
  412. /* Make sure our callback is called when errors actually happen */
  413. data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  414. }
  415. if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
  416. || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
  417. data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
  418. data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
  419. renderer->info.max_texture_width = value;
  420. renderer->info.max_texture_height = value;
  421. } else {
  422. data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
  423. renderer->info.max_texture_width = value;
  424. renderer->info.max_texture_height = value;
  425. }
  426. /* Check for multitexture support */
  427. if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
  428. data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
  429. if (data->glActiveTextureARB) {
  430. data->GL_ARB_multitexture_supported = SDL_TRUE;
  431. data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
  432. }
  433. }
  434. /* Check for shader support */
  435. hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
  436. if (!hint || *hint != '0') {
  437. data->shaders = GL_CreateShaderContext();
  438. }
  439. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
  440. data->shaders ? "ENABLED" : "DISABLED");
  441. /* We support YV12 textures using 3 textures and a shader */
  442. if (data->shaders && data->num_texture_units >= 3) {
  443. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
  444. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
  445. }
  446. if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
  447. data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
  448. data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
  449. SDL_GL_GetProcAddress("glGenFramebuffersEXT");
  450. data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
  451. SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
  452. data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
  453. SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
  454. data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
  455. SDL_GL_GetProcAddress("glBindFramebufferEXT");
  456. data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
  457. SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
  458. renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
  459. }
  460. data->framebuffers = NULL;
  461. /* Set up parameters for rendering */
  462. GL_ResetState(renderer);
  463. return renderer;
  464. }
  465. static void
  466. GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  467. {
  468. if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
  469. event->event == SDL_WINDOWEVENT_SHOWN ||
  470. event->event == SDL_WINDOWEVENT_HIDDEN) {
  471. /* Rebind the context to the window area and update matrices */
  472. SDL_CurrentContext = NULL;
  473. }
  474. }
  475. static int
  476. GL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
  477. {
  478. SDL_GL_GetDrawableSize(renderer->window, w, h);
  479. return 0;
  480. }
  481. SDL_FORCE_INLINE int
  482. power_of_2(int input)
  483. {
  484. int value = 1;
  485. while (value < input) {
  486. value <<= 1;
  487. }
  488. return value;
  489. }
  490. SDL_FORCE_INLINE SDL_bool
  491. convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
  492. GLint* internalFormat, GLenum* format, GLenum* type)
  493. {
  494. switch (pixel_format) {
  495. case SDL_PIXELFORMAT_ARGB8888:
  496. *internalFormat = GL_RGBA8;
  497. *format = GL_BGRA;
  498. *type = GL_UNSIGNED_INT_8_8_8_8_REV;
  499. break;
  500. case SDL_PIXELFORMAT_YV12:
  501. case SDL_PIXELFORMAT_IYUV:
  502. *internalFormat = GL_LUMINANCE;
  503. *format = GL_LUMINANCE;
  504. *type = GL_UNSIGNED_BYTE;
  505. break;
  506. default:
  507. return SDL_FALSE;
  508. }
  509. return SDL_TRUE;
  510. }
  511. static GLenum
  512. GetScaleQuality(void)
  513. {
  514. const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
  515. if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
  516. return GL_NEAREST;
  517. } else {
  518. return GL_LINEAR;
  519. }
  520. }
  521. static int
  522. GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  523. {
  524. GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
  525. GL_TextureData *data;
  526. GLint internalFormat;
  527. GLenum format, type;
  528. int texture_w, texture_h;
  529. GLenum scaleMode;
  530. GL_ActivateRenderer(renderer);
  531. if (!convert_format(renderdata, texture->format, &internalFormat,
  532. &format, &type)) {
  533. return SDL_SetError("Texture format %s not supported by OpenGL",
  534. SDL_GetPixelFormatName(texture->format));
  535. }
  536. data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
  537. if (!data) {
  538. return SDL_OutOfMemory();
  539. }
  540. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  541. size_t size;
  542. data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
  543. size = texture->h * data->pitch;
  544. if (texture->format == SDL_PIXELFORMAT_YV12 ||
  545. texture->format == SDL_PIXELFORMAT_IYUV) {
  546. /* Need to add size for the U and V planes */
  547. size += (2 * (texture->h * data->pitch) / 4);
  548. }
  549. data->pixels = SDL_calloc(1, size);
  550. if (!data->pixels) {
  551. SDL_free(data);
  552. return SDL_OutOfMemory();
  553. }
  554. }
  555. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  556. data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
  557. } else {
  558. data->fbo = NULL;
  559. }
  560. GL_CheckError("", renderer);
  561. renderdata->glGenTextures(1, &data->texture);
  562. if (GL_CheckError("glGenTexures()", renderer) < 0) {
  563. SDL_free(data);
  564. return -1;
  565. }
  566. texture->driverdata = data;
  567. if ((renderdata->GL_ARB_texture_rectangle_supported)
  568. /* && texture->access != SDL_TEXTUREACCESS_TARGET */){
  569. data->type = GL_TEXTURE_RECTANGLE_ARB;
  570. texture_w = texture->w;
  571. texture_h = texture->h;
  572. data->texw = (GLfloat) texture_w;
  573. data->texh = (GLfloat) texture_h;
  574. } else {
  575. data->type = GL_TEXTURE_2D;
  576. texture_w = power_of_2(texture->w);
  577. texture_h = power_of_2(texture->h);
  578. data->texw = (GLfloat) (texture->w) / texture_w;
  579. data->texh = (GLfloat) texture->h / texture_h;
  580. }
  581. data->format = format;
  582. data->formattype = type;
  583. scaleMode = GetScaleQuality();
  584. renderdata->glEnable(data->type);
  585. renderdata->glBindTexture(data->type, data->texture);
  586. renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
  587. renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
  588. /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
  589. and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
  590. */
  591. if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
  592. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
  593. GL_CLAMP_TO_EDGE);
  594. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
  595. GL_CLAMP_TO_EDGE);
  596. }
  597. #ifdef __MACOSX__
  598. #ifndef GL_TEXTURE_STORAGE_HINT_APPLE
  599. #define GL_TEXTURE_STORAGE_HINT_APPLE 0x85BC
  600. #endif
  601. #ifndef STORAGE_CACHED_APPLE
  602. #define STORAGE_CACHED_APPLE 0x85BE
  603. #endif
  604. #ifndef STORAGE_SHARED_APPLE
  605. #define STORAGE_SHARED_APPLE 0x85BF
  606. #endif
  607. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  608. renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
  609. GL_STORAGE_SHARED_APPLE);
  610. } else {
  611. renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
  612. GL_STORAGE_CACHED_APPLE);
  613. }
  614. if (texture->access == SDL_TEXTUREACCESS_STREAMING
  615. && texture->format == SDL_PIXELFORMAT_ARGB8888
  616. && (texture->w % 8) == 0) {
  617. renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
  618. renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  619. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
  620. (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
  621. renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
  622. texture_h, 0, format, type, data->pixels);
  623. renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
  624. }
  625. else
  626. #endif
  627. {
  628. renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
  629. texture_h, 0, format, type, NULL);
  630. }
  631. renderdata->glDisable(data->type);
  632. if (GL_CheckError("glTexImage2D()", renderer) < 0) {
  633. return -1;
  634. }
  635. if (texture->format == SDL_PIXELFORMAT_YV12 ||
  636. texture->format == SDL_PIXELFORMAT_IYUV) {
  637. data->yuv = SDL_TRUE;
  638. renderdata->glGenTextures(1, &data->utexture);
  639. renderdata->glGenTextures(1, &data->vtexture);
  640. renderdata->glEnable(data->type);
  641. renderdata->glBindTexture(data->type, data->utexture);
  642. renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
  643. scaleMode);
  644. renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
  645. scaleMode);
  646. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
  647. GL_CLAMP_TO_EDGE);
  648. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
  649. GL_CLAMP_TO_EDGE);
  650. renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
  651. texture_h/2, 0, format, type, NULL);
  652. renderdata->glBindTexture(data->type, data->vtexture);
  653. renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
  654. scaleMode);
  655. renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
  656. scaleMode);
  657. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
  658. GL_CLAMP_TO_EDGE);
  659. renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
  660. GL_CLAMP_TO_EDGE);
  661. renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
  662. texture_h/2, 0, format, type, NULL);
  663. renderdata->glDisable(data->type);
  664. }
  665. return GL_CheckError("", renderer);
  666. }
  667. static int
  668. GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  669. const SDL_Rect * rect, const void *pixels, int pitch)
  670. {
  671. GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
  672. GL_TextureData *data = (GL_TextureData *) texture->driverdata;
  673. GL_ActivateRenderer(renderer);
  674. renderdata->glEnable(data->type);
  675. renderdata->glBindTexture(data->type, data->texture);
  676. renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  677. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
  678. (pitch / SDL_BYTESPERPIXEL(texture->format)));
  679. renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
  680. rect->h, data->format, data->formattype,
  681. pixels);
  682. if (data->yuv) {
  683. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
  684. /* Skip to the correct offset into the next texture */
  685. pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
  686. if (texture->format == SDL_PIXELFORMAT_YV12) {
  687. renderdata->glBindTexture(data->type, data->vtexture);
  688. } else {
  689. renderdata->glBindTexture(data->type, data->utexture);
  690. }
  691. renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
  692. rect->w/2, rect->h/2,
  693. data->format, data->formattype, pixels);
  694. /* Skip to the correct offset into the next texture */
  695. pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
  696. if (texture->format == SDL_PIXELFORMAT_YV12) {
  697. renderdata->glBindTexture(data->type, data->utexture);
  698. } else {
  699. renderdata->glBindTexture(data->type, data->vtexture);
  700. }
  701. renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
  702. rect->w/2, rect->h/2,
  703. data->format, data->formattype, pixels);
  704. }
  705. renderdata->glDisable(data->type);
  706. return GL_CheckError("glTexSubImage2D()", renderer);
  707. }
  708. static int
  709. GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  710. const SDL_Rect * rect,
  711. const Uint8 *Yplane, int Ypitch,
  712. const Uint8 *Uplane, int Upitch,
  713. const Uint8 *Vplane, int Vpitch)
  714. {
  715. GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
  716. GL_TextureData *data = (GL_TextureData *) texture->driverdata;
  717. GL_ActivateRenderer(renderer);
  718. renderdata->glEnable(data->type);
  719. renderdata->glBindTexture(data->type, data->texture);
  720. renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  721. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
  722. renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
  723. rect->h, data->format, data->formattype,
  724. Yplane);
  725. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
  726. renderdata->glBindTexture(data->type, data->utexture);
  727. renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
  728. rect->w/2, rect->h/2,
  729. data->format, data->formattype, Uplane);
  730. renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
  731. renderdata->glBindTexture(data->type, data->vtexture);
  732. renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
  733. rect->w/2, rect->h/2,
  734. data->format, data->formattype, Vplane);
  735. renderdata->glDisable(data->type);
  736. return GL_CheckError("glTexSubImage2D()", renderer);
  737. }
  738. static int
  739. GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  740. const SDL_Rect * rect, void **pixels, int *pitch)
  741. {
  742. GL_TextureData *data = (GL_TextureData *) texture->driverdata;
  743. data->locked_rect = *rect;
  744. *pixels =
  745. (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
  746. rect->x * SDL_BYTESPERPIXEL(texture->format));
  747. *pitch = data->pitch;
  748. return 0;
  749. }
  750. static void
  751. GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  752. {
  753. GL_TextureData *data = (GL_TextureData *) texture->driverdata;
  754. const SDL_Rect *rect;
  755. void *pixels;
  756. rect = &data->locked_rect;
  757. pixels =
  758. (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
  759. rect->x * SDL_BYTESPERPIXEL(texture->format));
  760. GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
  761. }
  762. static int
  763. GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  764. {
  765. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  766. GL_TextureData *texturedata;
  767. GLenum status;
  768. GL_ActivateRenderer(renderer);
  769. if (texture == NULL) {
  770. data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  771. return 0;
  772. }
  773. texturedata = (GL_TextureData *) texture->driverdata;
  774. data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
  775. /* TODO: check if texture pixel format allows this operation */
  776. data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
  777. /* Check FBO status */
  778. status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  779. if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  780. return SDL_SetError("glFramebufferTexture2DEXT() failed");
  781. }
  782. return 0;
  783. }
  784. static int
  785. GL_UpdateViewport(SDL_Renderer * renderer)
  786. {
  787. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  788. if (SDL_CurrentContext != data->context) {
  789. /* We'll update the viewport after we rebind the context */
  790. return 0;
  791. }
  792. data->glViewport(renderer->viewport.x, renderer->viewport.y,
  793. renderer->viewport.w, renderer->viewport.h);
  794. data->glMatrixMode(GL_PROJECTION);
  795. data->glLoadIdentity();
  796. if (renderer->viewport.w && renderer->viewport.h) {
  797. if (renderer->target) {
  798. data->glOrtho((GLdouble) 0,
  799. (GLdouble) renderer->viewport.w,
  800. (GLdouble) 0,
  801. (GLdouble) renderer->viewport.h,
  802. 0.0, 1.0);
  803. } else {
  804. data->glOrtho((GLdouble) 0,
  805. (GLdouble) renderer->viewport.w,
  806. (GLdouble) renderer->viewport.h,
  807. (GLdouble) 0,
  808. 0.0, 1.0);
  809. }
  810. }
  811. return GL_CheckError("", renderer);
  812. }
  813. static int
  814. GL_UpdateClipRect(SDL_Renderer * renderer)
  815. {
  816. const SDL_Rect *rect = &renderer->clip_rect;
  817. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  818. if (!SDL_RectEmpty(rect)) {
  819. data->glEnable(GL_SCISSOR_TEST);
  820. data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
  821. } else {
  822. data->glDisable(GL_SCISSOR_TEST);
  823. }
  824. return 0;
  825. }
  826. static void
  827. GL_SetShader(GL_RenderData * data, GL_Shader shader)
  828. {
  829. if (data->shaders && shader != data->current.shader) {
  830. GL_SelectShader(data->shaders, shader);
  831. data->current.shader = shader;
  832. }
  833. }
  834. static void
  835. GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  836. {
  837. Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
  838. if (color != data->current.color) {
  839. data->glColor4f((GLfloat) r * inv255f,
  840. (GLfloat) g * inv255f,
  841. (GLfloat) b * inv255f,
  842. (GLfloat) a * inv255f);
  843. data->current.color = color;
  844. }
  845. }
  846. static void
  847. GL_SetBlendMode(GL_RenderData * data, int blendMode)
  848. {
  849. if (blendMode != data->current.blendMode) {
  850. switch (blendMode) {
  851. case SDL_BLENDMODE_NONE:
  852. data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  853. data->glDisable(GL_BLEND);
  854. break;
  855. case SDL_BLENDMODE_BLEND:
  856. data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  857. data->glEnable(GL_BLEND);
  858. data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  859. break;
  860. case SDL_BLENDMODE_ADD:
  861. data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  862. data->glEnable(GL_BLEND);
  863. data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  864. break;
  865. case SDL_BLENDMODE_MOD:
  866. data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  867. data->glEnable(GL_BLEND);
  868. data->glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE);
  869. break;
  870. }
  871. data->current.blendMode = blendMode;
  872. }
  873. }
  874. static void
  875. GL_SetDrawingState(SDL_Renderer * renderer)
  876. {
  877. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  878. GL_ActivateRenderer(renderer);
  879. GL_SetColor(data, renderer->r,
  880. renderer->g,
  881. renderer->b,
  882. renderer->a);
  883. GL_SetBlendMode(data, renderer->blendMode);
  884. GL_SetShader(data, SHADER_SOLID);
  885. }
  886. static int
  887. GL_RenderClear(SDL_Renderer * renderer)
  888. {
  889. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  890. GL_ActivateRenderer(renderer);
  891. data->glClearColor((GLfloat) renderer->r * inv255f,
  892. (GLfloat) renderer->g * inv255f,
  893. (GLfloat) renderer->b * inv255f,
  894. (GLfloat) renderer->a * inv255f);
  895. data->glClear(GL_COLOR_BUFFER_BIT);
  896. return 0;
  897. }
  898. static int
  899. GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
  900. int count)
  901. {
  902. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  903. int i;
  904. GL_SetDrawingState(renderer);
  905. data->glBegin(GL_POINTS);
  906. for (i = 0; i < count; ++i) {
  907. data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
  908. }
  909. data->glEnd();
  910. return 0;
  911. }
  912. static int
  913. GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
  914. int count)
  915. {
  916. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  917. int i;
  918. GL_SetDrawingState(renderer);
  919. if (count > 2 &&
  920. points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
  921. data->glBegin(GL_LINE_LOOP);
  922. /* GL_LINE_LOOP takes care of the final segment */
  923. --count;
  924. for (i = 0; i < count; ++i) {
  925. data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
  926. }
  927. data->glEnd();
  928. } else {
  929. #if defined(__APPLE__) || defined(__WIN32__)
  930. #else
  931. int x1, y1, x2, y2;
  932. #endif
  933. data->glBegin(GL_LINE_STRIP);
  934. for (i = 0; i < count; ++i) {
  935. data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
  936. }
  937. data->glEnd();
  938. /* The line is half open, so we need one more point to complete it.
  939. * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
  940. * If we have to, we can use vertical line and horizontal line textures
  941. * for vertical and horizontal lines, and then create custom textures
  942. * for diagonal lines and software render those. It's terrible, but at
  943. * least it would be pixel perfect.
  944. */
  945. data->glBegin(GL_POINTS);
  946. #if defined(__APPLE__) || defined(__WIN32__)
  947. /* Mac OS X and Windows seem to always leave the second point open */
  948. data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
  949. #else
  950. /* Linux seems to leave the right-most or bottom-most point open */
  951. x1 = points[0].x;
  952. y1 = points[0].y;
  953. x2 = points[count-1].x;
  954. y2 = points[count-1].y;
  955. if (x1 > x2) {
  956. data->glVertex2f(0.5f + x1, 0.5f + y1);
  957. } else if (x2 > x1) {
  958. data->glVertex2f(0.5f + x2, 0.5f + y2);
  959. } else if (y1 > y2) {
  960. data->glVertex2f(0.5f + x1, 0.5f + y1);
  961. } else if (y2 > y1) {
  962. data->glVertex2f(0.5f + x2, 0.5f + y2);
  963. }
  964. #endif
  965. data->glEnd();
  966. }
  967. return GL_CheckError("", renderer);
  968. }
  969. static int
  970. GL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
  971. {
  972. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  973. int i;
  974. GL_SetDrawingState(renderer);
  975. for (i = 0; i < count; ++i) {
  976. const SDL_FRect *rect = &rects[i];
  977. data->glRectf(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
  978. }
  979. return GL_CheckError("", renderer);
  980. }
  981. static int
  982. GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  983. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  984. {
  985. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  986. GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
  987. GLfloat minx, miny, maxx, maxy;
  988. GLfloat minu, maxu, minv, maxv;
  989. GL_ActivateRenderer(renderer);
  990. data->glEnable(texturedata->type);
  991. if (texturedata->yuv) {
  992. data->glActiveTextureARB(GL_TEXTURE2_ARB);
  993. data->glBindTexture(texturedata->type, texturedata->vtexture);
  994. data->glActiveTextureARB(GL_TEXTURE1_ARB);
  995. data->glBindTexture(texturedata->type, texturedata->utexture);
  996. data->glActiveTextureARB(GL_TEXTURE0_ARB);
  997. }
  998. data->glBindTexture(texturedata->type, texturedata->texture);
  999. if (texture->modMode) {
  1000. GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
  1001. } else {
  1002. GL_SetColor(data, 255, 255, 255, 255);
  1003. }
  1004. GL_SetBlendMode(data, texture->blendMode);
  1005. if (texturedata->yuv) {
  1006. GL_SetShader(data, SHADER_YV12);
  1007. } else {
  1008. GL_SetShader(data, SHADER_RGB);
  1009. }
  1010. minx = dstrect->x;
  1011. miny = dstrect->y;
  1012. maxx = dstrect->x + dstrect->w;
  1013. maxy = dstrect->y + dstrect->h;
  1014. minu = (GLfloat) srcrect->x / texture->w;
  1015. minu *= texturedata->texw;
  1016. maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
  1017. maxu *= texturedata->texw;
  1018. minv = (GLfloat) srcrect->y / texture->h;
  1019. minv *= texturedata->texh;
  1020. maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
  1021. maxv *= texturedata->texh;
  1022. data->glBegin(GL_TRIANGLE_STRIP);
  1023. data->glTexCoord2f(minu, minv);
  1024. data->glVertex2f(minx, miny);
  1025. data->glTexCoord2f(maxu, minv);
  1026. data->glVertex2f(maxx, miny);
  1027. data->glTexCoord2f(minu, maxv);
  1028. data->glVertex2f(minx, maxy);
  1029. data->glTexCoord2f(maxu, maxv);
  1030. data->glVertex2f(maxx, maxy);
  1031. data->glEnd();
  1032. data->glDisable(texturedata->type);
  1033. return GL_CheckError("", renderer);
  1034. }
  1035. static int
  1036. GL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  1037. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  1038. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  1039. {
  1040. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  1041. GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
  1042. GLfloat minx, miny, maxx, maxy;
  1043. GLfloat centerx, centery;
  1044. GLfloat minu, maxu, minv, maxv;
  1045. GL_ActivateRenderer(renderer);
  1046. data->glEnable(texturedata->type);
  1047. if (texturedata->yuv) {
  1048. data->glActiveTextureARB(GL_TEXTURE2_ARB);
  1049. data->glBindTexture(texturedata->type, texturedata->vtexture);
  1050. data->glActiveTextureARB(GL_TEXTURE1_ARB);
  1051. data->glBindTexture(texturedata->type, texturedata->utexture);
  1052. data->glActiveTextureARB(GL_TEXTURE0_ARB);
  1053. }
  1054. data->glBindTexture(texturedata->type, texturedata->texture);
  1055. if (texture->modMode) {
  1056. GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
  1057. } else {
  1058. GL_SetColor(data, 255, 255, 255, 255);
  1059. }
  1060. GL_SetBlendMode(data, texture->blendMode);
  1061. if (texturedata->yuv) {
  1062. GL_SetShader(data, SHADER_YV12);
  1063. } else {
  1064. GL_SetShader(data, SHADER_RGB);
  1065. }
  1066. centerx = center->x;
  1067. centery = center->y;
  1068. if (flip & SDL_FLIP_HORIZONTAL) {
  1069. minx = dstrect->w - centerx;
  1070. maxx = -centerx;
  1071. }
  1072. else {
  1073. minx = -centerx;
  1074. maxx = dstrect->w - centerx;
  1075. }
  1076. if (flip & SDL_FLIP_VERTICAL) {
  1077. miny = dstrect->h - centery;
  1078. maxy = -centery;
  1079. }
  1080. else {
  1081. miny = -centery;
  1082. maxy = dstrect->h - centery;
  1083. }
  1084. minu = (GLfloat) srcrect->x / texture->w;
  1085. minu *= texturedata->texw;
  1086. maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
  1087. maxu *= texturedata->texw;
  1088. minv = (GLfloat) srcrect->y / texture->h;
  1089. minv *= texturedata->texh;
  1090. maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
  1091. maxv *= texturedata->texh;
  1092. /* Translate to flip, rotate, translate to position */
  1093. data->glPushMatrix();
  1094. data->glTranslatef((GLfloat)dstrect->x + centerx, (GLfloat)dstrect->y + centery, (GLfloat)0.0);
  1095. data->glRotated(angle, (GLdouble)0.0, (GLdouble)0.0, (GLdouble)1.0);
  1096. data->glBegin(GL_TRIANGLE_STRIP);
  1097. data->glTexCoord2f(minu, minv);
  1098. data->glVertex2f(minx, miny);
  1099. data->glTexCoord2f(maxu, minv);
  1100. data->glVertex2f(maxx, miny);
  1101. data->glTexCoord2f(minu, maxv);
  1102. data->glVertex2f(minx, maxy);
  1103. data->glTexCoord2f(maxu, maxv);
  1104. data->glVertex2f(maxx, maxy);
  1105. data->glEnd();
  1106. data->glPopMatrix();
  1107. data->glDisable(texturedata->type);
  1108. return GL_CheckError("", renderer);
  1109. }
  1110. static int
  1111. GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1112. Uint32 pixel_format, void * pixels, int pitch)
  1113. {
  1114. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  1115. Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
  1116. void *temp_pixels;
  1117. int temp_pitch;
  1118. GLint internalFormat;
  1119. GLenum format, type;
  1120. Uint8 *src, *dst, *tmp;
  1121. int w, h, length, rows;
  1122. int status;
  1123. GL_ActivateRenderer(renderer);
  1124. temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1125. temp_pixels = SDL_malloc(rect->h * temp_pitch);
  1126. if (!temp_pixels) {
  1127. return SDL_OutOfMemory();
  1128. }
  1129. convert_format(data, temp_format, &internalFormat, &format, &type);
  1130. SDL_GetRendererOutputSize(renderer, &w, &h);
  1131. data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1132. data->glPixelStorei(GL_PACK_ROW_LENGTH,
  1133. (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
  1134. data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
  1135. format, type, temp_pixels);
  1136. if (GL_CheckError("glReadPixels()", renderer) < 0) {
  1137. return -1;
  1138. }
  1139. /* Flip the rows to be top-down */
  1140. length = rect->w * SDL_BYTESPERPIXEL(temp_format);
  1141. src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
  1142. dst = (Uint8*)temp_pixels;
  1143. tmp = SDL_stack_alloc(Uint8, length);
  1144. rows = rect->h / 2;
  1145. while (rows--) {
  1146. SDL_memcpy(tmp, dst, length);
  1147. SDL_memcpy(dst, src, length);
  1148. SDL_memcpy(src, tmp, length);
  1149. dst += temp_pitch;
  1150. src -= temp_pitch;
  1151. }
  1152. SDL_stack_free(tmp);
  1153. status = SDL_ConvertPixels(rect->w, rect->h,
  1154. temp_format, temp_pixels, temp_pitch,
  1155. pixel_format, pixels, pitch);
  1156. SDL_free(temp_pixels);
  1157. return status;
  1158. }
  1159. static void
  1160. GL_RenderPresent(SDL_Renderer * renderer)
  1161. {
  1162. GL_ActivateRenderer(renderer);
  1163. SDL_GL_SwapWindow(renderer->window);
  1164. }
  1165. static void
  1166. GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1167. {
  1168. GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
  1169. GL_TextureData *data = (GL_TextureData *) texture->driverdata;
  1170. GL_ActivateRenderer(renderer);
  1171. if (!data) {
  1172. return;
  1173. }
  1174. if (data->texture) {
  1175. renderdata->glDeleteTextures(1, &data->texture);
  1176. }
  1177. if (data->yuv) {
  1178. renderdata->glDeleteTextures(1, &data->utexture);
  1179. renderdata->glDeleteTextures(1, &data->vtexture);
  1180. }
  1181. SDL_free(data->pixels);
  1182. SDL_free(data);
  1183. texture->driverdata = NULL;
  1184. }
  1185. static void
  1186. GL_DestroyRenderer(SDL_Renderer * renderer)
  1187. {
  1188. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  1189. if (data) {
  1190. GL_ClearErrors(renderer);
  1191. if (data->GL_ARB_debug_output_supported) {
  1192. PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC) SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
  1193. /* Uh oh, we don't have a safe way of removing ourselves from the callback chain, if it changed after we set our callback. */
  1194. /* For now, just always replace the callback with the original one */
  1195. glDebugMessageCallbackARBFunc(data->next_error_callback, data->next_error_userparam);
  1196. }
  1197. if (data->shaders) {
  1198. GL_DestroyShaderContext(data->shaders);
  1199. }
  1200. if (data->context) {
  1201. while (data->framebuffers) {
  1202. GL_FBOList *nextnode = data->framebuffers->next;
  1203. /* delete the framebuffer object */
  1204. data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
  1205. GL_CheckError("", renderer);
  1206. SDL_free(data->framebuffers);
  1207. data->framebuffers = nextnode;
  1208. }
  1209. SDL_GL_DeleteContext(data->context);
  1210. }
  1211. SDL_free(data);
  1212. }
  1213. SDL_free(renderer);
  1214. }
  1215. static int
  1216. GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh)
  1217. {
  1218. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  1219. GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
  1220. GL_ActivateRenderer(renderer);
  1221. data->glEnable(texturedata->type);
  1222. if (texturedata->yuv) {
  1223. data->glActiveTextureARB(GL_TEXTURE2_ARB);
  1224. data->glBindTexture(texturedata->type, texturedata->vtexture);
  1225. data->glActiveTextureARB(GL_TEXTURE1_ARB);
  1226. data->glBindTexture(texturedata->type, texturedata->utexture);
  1227. data->glActiveTextureARB(GL_TEXTURE0_ARB);
  1228. }
  1229. data->glBindTexture(texturedata->type, texturedata->texture);
  1230. if(texw) *texw = (float)texturedata->texw;
  1231. if(texh) *texh = (float)texturedata->texh;
  1232. return 0;
  1233. }
  1234. static int
  1235. GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture)
  1236. {
  1237. GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
  1238. GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
  1239. GL_ActivateRenderer(renderer);
  1240. if (texturedata->yuv) {
  1241. data->glActiveTextureARB(GL_TEXTURE2_ARB);
  1242. data->glDisable(texturedata->type);
  1243. data->glActiveTextureARB(GL_TEXTURE1_ARB);
  1244. data->glDisable(texturedata->type);
  1245. data->glActiveTextureARB(GL_TEXTURE0_ARB);
  1246. }
  1247. data->glDisable(texturedata->type);
  1248. return 0;
  1249. }
  1250. #endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
  1251. /* vi: set ts=4 sw=4 expandtab: */