SDL_render_gl.c 50 KB

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