gl-cocoa.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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-subsystem.h"
  15. #include <OpenGL/OpenGL.h>
  16. #import <Cocoa/Cocoa.h>
  17. #import <AppKit/AppKit.h>
  18. //#include "util/darray.h"
  19. struct gl_windowinfo {
  20. NSView *view;
  21. };
  22. struct gl_platform {
  23. NSOpenGLContext *context;
  24. struct gs_swap_chain swap;
  25. };
  26. static NSOpenGLContext *gl_context_create(struct gs_init_data *info)
  27. {
  28. unsigned attrib_count = 0;
  29. #define ADD_ATTR(x) \
  30. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  31. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  32. NSOpenGLPixelFormatAttribute attributes[40];
  33. switch(info->num_backbuffers) {
  34. case 0:
  35. break;
  36. case 1:
  37. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  38. break;
  39. case 2:
  40. ADD_ATTR(NSOpenGLPFATripleBuffer);
  41. break;
  42. default:
  43. blog(LOG_ERROR, "Requested backbuffers (%d) not "
  44. "supported", info->num_backbuffers);
  45. }
  46. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  47. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  48. int color_bits = 0;//get_color_format_bits(info->format);
  49. if(color_bits == 0) color_bits = 24;
  50. else if(color_bits < 15) color_bits = 15;
  51. ADD_ATTR2(NSOpenGLPFAColorSize, color_bits);
  52. ADD_ATTR2(NSOpenGLPFAAlphaSize, 8);
  53. ADD_ATTR2(NSOpenGLPFADepthSize, 16);
  54. ADD_ATTR(0);
  55. #undef ADD_ATTR2
  56. #undef ADD_ATTR
  57. NSOpenGLPixelFormat *pf;
  58. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  59. if(!pf) {
  60. blog(LOG_ERROR, "Failed to create pixel format");
  61. return NULL;
  62. }
  63. NSOpenGLContext *context;
  64. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  65. [pf release];
  66. if(!context) {
  67. blog(LOG_ERROR, "Failed to create context");
  68. return NULL;
  69. }
  70. [context setView:info->window.view];
  71. return context;
  72. }
  73. static bool gl_init_default_swap(struct gl_platform *plat, gs_device_t *dev,
  74. struct gs_init_data *info)
  75. {
  76. if(!(plat->context = gl_context_create(info)))
  77. return false;
  78. plat->swap.device = dev;
  79. plat->swap.info = *info;
  80. plat->swap.wi = gl_windowinfo_create(info);
  81. return plat->swap.wi != NULL;
  82. }
  83. struct gl_platform *gl_platform_create(gs_device_t *device,
  84. struct gs_init_data *info)
  85. {
  86. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  87. if(!gl_init_default_swap(plat, device, info))
  88. goto fail;
  89. [plat->context makeCurrentContext];
  90. if (!gladLoadGL())
  91. goto fail;
  92. return plat;
  93. fail:
  94. blog(LOG_ERROR, "gl_platform_create failed");
  95. gl_platform_destroy(plat);
  96. return NULL;
  97. }
  98. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  99. {
  100. if(platform)
  101. return &platform->swap;
  102. return NULL;
  103. }
  104. void gl_platform_destroy(struct gl_platform *platform)
  105. {
  106. if(!platform)
  107. return;
  108. [platform->context release];
  109. platform->context = nil;
  110. gl_windowinfo_destroy(platform->swap.wi);
  111. bfree(platform);
  112. }
  113. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  114. {
  115. UNUSED_PARAMETER(swap);
  116. return true;
  117. }
  118. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  119. {
  120. UNUSED_PARAMETER(swap);
  121. }
  122. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  123. {
  124. if(!info)
  125. return NULL;
  126. if(!info->window.view)
  127. return NULL;
  128. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  129. wi->view = info->window.view;
  130. [info->window.view setWantsBestResolutionOpenGLSurface:YES];
  131. return wi;
  132. }
  133. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  134. {
  135. if(!wi)
  136. return;
  137. wi->view = nil;
  138. bfree(wi);
  139. }
  140. void gl_update(gs_device_t *device)
  141. {
  142. [device->plat->context update];
  143. }
  144. void device_enter_context(gs_device_t *device)
  145. {
  146. [device->plat->context makeCurrentContext];
  147. }
  148. void device_leave_context(gs_device_t *device)
  149. {
  150. UNUSED_PARAMETER(device);
  151. [NSOpenGLContext clearCurrentContext];
  152. }
  153. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  154. {
  155. if(!swap)
  156. swap = &device->plat->swap;
  157. if(device->cur_swap == swap)
  158. return;
  159. device->cur_swap = swap;
  160. [device->plat->context setView:swap->wi->view];
  161. }
  162. void device_present(gs_device_t *device)
  163. {
  164. [device->plat->context flushBuffer];
  165. }
  166. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  167. uint32_t *height)
  168. {
  169. if(width) *width = swap->info.cx;
  170. if(height) *height = swap->info.cy;
  171. }
  172. gs_texture_t *device_texture_create_from_iosurface(gs_device_t *device,
  173. void *iosurf)
  174. {
  175. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  176. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  177. OSType pf = IOSurfaceGetPixelFormat(ref);
  178. if (pf != 'BGRA')
  179. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  180. pf >> 24, pf >> 16, pf >> 8, pf);
  181. const enum gs_color_format color_format = GS_BGRA;
  182. tex->base.device = device;
  183. tex->base.type = GS_TEXTURE_2D;
  184. tex->base.format = GS_BGRA;
  185. tex->base.levels = 1;
  186. tex->base.gl_format = convert_gs_format(color_format);
  187. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  188. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  189. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  190. tex->base.is_dynamic = false;
  191. tex->base.is_render_target = false;
  192. tex->base.gen_mipmaps = false;
  193. tex->width = IOSurfaceGetWidth(ref);
  194. tex->height = IOSurfaceGetHeight(ref);
  195. if (!gl_gen_textures(1, &tex->base.texture))
  196. goto fail;
  197. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  198. goto fail;
  199. CGLError err = CGLTexImageIOSurface2D(
  200. [[NSOpenGLContext currentContext] CGLContextObj],
  201. tex->base.gl_target,
  202. tex->base.gl_internal_format,
  203. tex->width, tex->height,
  204. tex->base.gl_format,
  205. tex->base.gl_type,
  206. ref, 0);
  207. if(err != kCGLNoError) {
  208. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  209. " (device_texture_create_from_iosurface)",
  210. err, CGLErrorString(err));
  211. gl_success("CGLTexImageIOSurface2D");
  212. goto fail;
  213. }
  214. if (!gl_tex_param_i(tex->base.gl_target,
  215. GL_TEXTURE_MAX_LEVEL, 0))
  216. goto fail;
  217. if (!gl_bind_texture(tex->base.gl_target, 0))
  218. goto fail;
  219. return (gs_texture_t*)tex;
  220. fail:
  221. gs_texture_destroy((gs_texture_t*)tex);
  222. blog(LOG_ERROR, "device_texture_create_from_iosurface (GL) failed");
  223. return NULL;
  224. }
  225. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  226. {
  227. if (!texture)
  228. return false;
  229. if (!iosurf)
  230. return false;
  231. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  232. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  233. OSType pf = IOSurfaceGetPixelFormat(ref);
  234. if (pf != 'BGRA')
  235. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  236. pf >> 24, pf >> 16, pf >> 8, pf);
  237. if (tex->width != IOSurfaceGetWidth(ref) ||
  238. tex->height != IOSurfaceGetHeight(ref))
  239. return false;
  240. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  241. return false;
  242. CGLError err = CGLTexImageIOSurface2D(
  243. [[NSOpenGLContext currentContext] CGLContextObj],
  244. tex->base.gl_target,
  245. tex->base.gl_internal_format,
  246. tex->width, tex->height,
  247. tex->base.gl_format,
  248. tex->base.gl_type,
  249. ref, 0);
  250. if(err != kCGLNoError) {
  251. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  252. " (gs_texture_rebind_iosurface)",
  253. err, CGLErrorString(err));
  254. gl_success("CGLTexImageIOSurface2D");
  255. return false;
  256. }
  257. if (!gl_bind_texture(tex->base.gl_target, 0))
  258. return false;
  259. return true;
  260. }