gl-cocoa.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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, 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(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 (!ogl_LoadFunctions())
  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. return wi;
  131. }
  132. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  133. {
  134. if(!wi)
  135. return;
  136. wi->view = nil;
  137. bfree(wi);
  138. }
  139. void gl_update(device_t device)
  140. {
  141. [device->plat->context update];
  142. }
  143. void device_entercontext(device_t device)
  144. {
  145. [device->plat->context makeCurrentContext];
  146. }
  147. void device_leavecontext(device_t device)
  148. {
  149. UNUSED_PARAMETER(device);
  150. [NSOpenGLContext clearCurrentContext];
  151. }
  152. void device_load_swapchain(device_t device, swapchain_t swap)
  153. {
  154. if(!swap)
  155. swap = &device->plat->swap;
  156. if(device->cur_swap == swap)
  157. return;
  158. device->cur_swap = swap;
  159. [device->plat->context setView:swap->wi->view];
  160. }
  161. void device_present(device_t device)
  162. {
  163. [device->plat->context flushBuffer];
  164. }
  165. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  166. uint32_t *height)
  167. {
  168. if(width) *width = swap->info.cx;
  169. if(height) *height = swap->info.cy;
  170. }
  171. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  172. {
  173. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  174. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  175. OSType pf = IOSurfaceGetPixelFormat(ref);
  176. if (pf != 'BGRA')
  177. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  178. pf >> 24, pf >> 16, pf >> 8, pf);
  179. const enum gs_color_format color_format = GS_BGRA;
  180. tex->base.device = device;
  181. tex->base.type = GS_TEXTURE_2D;
  182. tex->base.format = GS_BGRA;
  183. tex->base.levels = 1;
  184. tex->base.gl_format = convert_gs_format(color_format);
  185. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  186. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  187. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  188. tex->base.is_dynamic = false;
  189. tex->base.is_render_target = false;
  190. tex->base.gen_mipmaps = false;
  191. tex->width = IOSurfaceGetWidth(ref);
  192. tex->height = IOSurfaceGetHeight(ref);
  193. if (!gl_gen_textures(1, &tex->base.texture))
  194. goto fail;
  195. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  196. goto fail;
  197. CGLError err = CGLTexImageIOSurface2D(
  198. [[NSOpenGLContext currentContext] CGLContextObj],
  199. tex->base.gl_target,
  200. tex->base.gl_internal_format,
  201. tex->width, tex->height,
  202. tex->base.gl_format,
  203. tex->base.gl_type,
  204. ref, 0);
  205. if(err != kCGLNoError) {
  206. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  207. " (texture_create_from_iosurface)",
  208. err, CGLErrorString(err));
  209. gl_success("CGLTexImageIOSurface2D");
  210. goto fail;
  211. }
  212. if (!gl_tex_param_i(tex->base.gl_target,
  213. GL_TEXTURE_MAX_LEVEL, 0))
  214. goto fail;
  215. if (!gl_bind_texture(tex->base.gl_target, 0))
  216. goto fail;
  217. return (texture_t)tex;
  218. fail:
  219. texture_destroy((texture_t)tex);
  220. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  221. return NULL;
  222. }
  223. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  224. {
  225. if (!texture)
  226. return false;
  227. if (!iosurf)
  228. return false;
  229. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  230. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  231. OSType pf = IOSurfaceGetPixelFormat(ref);
  232. if (pf != 'BGRA')
  233. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  234. pf >> 24, pf >> 16, pf >> 8, pf);
  235. if (tex->width != IOSurfaceGetWidth(ref) ||
  236. tex->height != IOSurfaceGetHeight(ref))
  237. return false;
  238. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  239. return false;
  240. CGLError err = CGLTexImageIOSurface2D(
  241. [[NSOpenGLContext currentContext] CGLContextObj],
  242. tex->base.gl_target,
  243. tex->base.gl_internal_format,
  244. tex->width, tex->height,
  245. tex->base.gl_format,
  246. tex->base.gl_type,
  247. ref, 0);
  248. if(err != kCGLNoError) {
  249. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  250. " (texture_rebind_iosurface)",
  251. err, CGLErrorString(err));
  252. gl_success("CGLTexImageIOSurface2D");
  253. return false;
  254. }
  255. if (!gl_bind_texture(tex->base.gl_target, 0))
  256. return false;
  257. return true;
  258. }