gl-cocoa.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /******************************************************************************
  2. Copyright (C) 2013 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <GL/glew.h>
  15. #include "gl-subsystem.h"
  16. #include <OpenGL/OpenGL.h>
  17. #import <Cocoa/Cocoa.h>
  18. #import <AppKit/AppKit.h>
  19. //#include "util/darray.h"
  20. struct gl_windowinfo {
  21. NSView *view;
  22. };
  23. struct gl_platform {
  24. NSOpenGLContext *context;
  25. struct gs_swap_chain swap;
  26. };
  27. static NSOpenGLContext *gl_context_create(struct gs_init_data *info)
  28. {
  29. unsigned attrib_count = 0;
  30. #define ADD_ATTR(x) \
  31. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  32. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  33. NSOpenGLPixelFormatAttribute attributes[40];
  34. switch(info->num_backbuffers) {
  35. case 0:
  36. break;
  37. case 1:
  38. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  39. break;
  40. case 2:
  41. ADD_ATTR(NSOpenGLPFATripleBuffer);
  42. break;
  43. default:
  44. blog(LOG_ERROR, "Requested backbuffers (%d) not "
  45. "supported", info->num_backbuffers);
  46. }
  47. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  48. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  49. int color_bits = 0;//get_color_format_bits(info->format);
  50. if(color_bits == 0) color_bits = 24;
  51. else if(color_bits < 15) color_bits = 15;
  52. ADD_ATTR2(NSOpenGLPFAColorSize, color_bits);
  53. ADD_ATTR2(NSOpenGLPFAAlphaSize, 8);
  54. ADD_ATTR2(NSOpenGLPFADepthSize, 16);
  55. ADD_ATTR(0);
  56. #undef ADD_ATTR2
  57. #undef ADD_ATTR
  58. NSOpenGLPixelFormat *pf;
  59. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  60. if(!pf) {
  61. blog(LOG_ERROR, "Failed to create pixel format");
  62. return NULL;
  63. }
  64. NSOpenGLContext *context;
  65. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  66. [pf release];
  67. if(!context) {
  68. blog(LOG_ERROR, "Failed to create context");
  69. return NULL;
  70. }
  71. [context setView:info->window.view];
  72. return context;
  73. }
  74. static bool gl_init_extensions(device_t device)
  75. {
  76. glewExperimental=true;
  77. GLenum error = glewInit();
  78. if(error != GLEW_OK) {
  79. blog(LOG_ERROR, "glewInit failed, %u\n%s\n", error,
  80. glewGetErrorString(error));
  81. return false;
  82. }
  83. return true;
  84. }
  85. static bool gl_init_default_swap(struct gl_platform *plat, device_t dev,
  86. struct gs_init_data *info)
  87. {
  88. if(!(plat->context = gl_context_create(info)))
  89. return false;
  90. plat->swap.device = dev;
  91. plat->swap.info = *info;
  92. plat->swap.wi = gl_windowinfo_create(info);
  93. return plat->swap.wi != NULL;
  94. }
  95. struct gl_platform *gl_platform_create(device_t device,
  96. struct gs_init_data *info)
  97. {
  98. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  99. memset(plat, 0, sizeof(struct gl_platform));
  100. if(!gl_init_default_swap(plat, device, info))
  101. goto fail;
  102. [plat->context makeCurrentContext];
  103. if(!gl_init_extensions(device))
  104. goto fail;
  105. if (GLEW_ARB_seamless_cube_map) {
  106. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  107. gl_success("GL_TEXTURE_CUBE_MAP_SEAMLESS");
  108. }
  109. return plat;
  110. fail:
  111. blog(LOG_ERROR, "gl_platform_create failed");
  112. gl_platform_destroy(plat);
  113. return NULL;
  114. }
  115. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  116. {
  117. if(platform)
  118. return &platform->swap;
  119. return NULL;
  120. }
  121. void gl_platform_destroy(struct gl_platform *platform)
  122. {
  123. if(!platform)
  124. return;
  125. [platform->context release];
  126. platform->context = nil;
  127. gl_windowinfo_destroy(platform->swap.wi);
  128. bfree(platform);
  129. }
  130. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  131. {
  132. if(!info)
  133. return NULL;
  134. if(!info->window.view)
  135. return NULL;
  136. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  137. memset(wi, 0, sizeof(struct gl_windowinfo));
  138. wi->view = info->window.view;
  139. return wi;
  140. }
  141. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  142. {
  143. if(!wi)
  144. return;
  145. wi->view = nil;
  146. bfree(wi);
  147. }
  148. void gl_update(device_t device)
  149. {
  150. [device->plat->context update];
  151. }
  152. void device_entercontext(device_t device)
  153. {
  154. CGLLockContext([device->plat->context CGLContextObj]);
  155. [device->plat->context makeCurrentContext];
  156. }
  157. void device_leavecontext(device_t device)
  158. {
  159. [NSOpenGLContext clearCurrentContext];
  160. CGLUnlockContext([device->plat->context CGLContextObj]);
  161. }
  162. void device_load_swapchain(device_t device, swapchain_t swap)
  163. {
  164. if(!swap)
  165. swap = &device->plat->swap;
  166. if(device->cur_swap == swap)
  167. return;
  168. device->cur_swap = swap;
  169. }
  170. void device_present(device_t device)
  171. {
  172. [device->plat->context flushBuffer];
  173. }
  174. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  175. uint32_t *height)
  176. {
  177. if(width) *width = swap->info.cx;
  178. if(height) *height = swap->info.cy;
  179. }
  180. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  181. {
  182. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  183. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  184. memset(tex, 0, sizeof(struct gs_texture_2d));
  185. OSType pf = IOSurfaceGetPixelFormat(ref);
  186. if (pf != 'BGRA')
  187. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  188. pf >> 24, pf >> 16, pf >> 8, pf);
  189. const enum gs_color_format color_format = GS_BGRA;
  190. tex->base.device = device;
  191. tex->base.type = GS_TEXTURE_2D;
  192. tex->base.format = GS_BGRA;
  193. tex->base.levels = 1;
  194. tex->base.gl_format = convert_gs_format(color_format);
  195. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  196. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  197. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  198. tex->base.is_dynamic = false;
  199. tex->base.is_render_target = false;
  200. tex->base.gen_mipmaps = false;
  201. tex->width = IOSurfaceGetWidth(ref);
  202. tex->height = IOSurfaceGetHeight(ref);
  203. if (!gl_gen_textures(1, &tex->base.texture))
  204. goto fail;
  205. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  206. goto fail;
  207. CGLError err = CGLTexImageIOSurface2D(
  208. [[NSOpenGLContext currentContext] CGLContextObj],
  209. tex->base.gl_target,
  210. tex->base.gl_internal_format,
  211. tex->width, tex->height,
  212. tex->base.gl_format,
  213. tex->base.gl_type,
  214. ref, 0);
  215. if(err != kCGLNoError) {
  216. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  217. " (texture_create_from_iosurface)",
  218. err, CGLErrorString(err));
  219. gl_success("CGLTexImageIOSurface2D");
  220. goto fail;
  221. }
  222. if (!gl_tex_param_i(tex->base.gl_target,
  223. GL_TEXTURE_MAX_LEVEL, 0))
  224. goto fail;
  225. if (!gl_bind_texture(tex->base.gl_target, 0))
  226. goto fail;
  227. return (texture_t)tex;
  228. fail:
  229. texture_destroy((texture_t)tex);
  230. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  231. return NULL;
  232. }
  233. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  234. {
  235. if (!texture)
  236. return false;
  237. if (!iosurf)
  238. return false;
  239. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  240. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  241. OSType pf = IOSurfaceGetPixelFormat(ref);
  242. if (pf != 'BGRA')
  243. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  244. pf >> 24, pf >> 16, pf >> 8, pf);
  245. if (tex->width != IOSurfaceGetWidth(ref) ||
  246. tex->height != IOSurfaceGetHeight(ref))
  247. return false;
  248. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  249. return false;
  250. CGLError err = CGLTexImageIOSurface2D(
  251. [[NSOpenGLContext currentContext] CGLContextObj],
  252. tex->base.gl_target,
  253. tex->base.gl_internal_format,
  254. tex->width, tex->height,
  255. tex->base.gl_format,
  256. tex->base.gl_type,
  257. ref, 0);
  258. if(err != kCGLNoError) {
  259. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  260. " (texture_rebind_iosurface)",
  261. err, CGLErrorString(err));
  262. gl_success("CGLTexImageIOSurface2D");
  263. return false;
  264. }
  265. if (!gl_bind_texture(tex->base.gl_target, 0))
  266. return false;
  267. return true;
  268. }