gl-cocoa.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 = bmalloc(sizeof(struct gl_platform));
  87. memset(plat, 0, sizeof(struct gl_platform));
  88. if(!gl_init_default_swap(plat, device, info))
  89. goto fail;
  90. [plat->context makeCurrentContext];
  91. if (!ogl_LoadFunctions())
  92. goto fail;
  93. return plat;
  94. fail:
  95. blog(LOG_ERROR, "gl_platform_create failed");
  96. gl_platform_destroy(plat);
  97. return NULL;
  98. }
  99. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  100. {
  101. if(platform)
  102. return &platform->swap;
  103. return NULL;
  104. }
  105. void gl_platform_destroy(struct gl_platform *platform)
  106. {
  107. if(!platform)
  108. return;
  109. [platform->context release];
  110. platform->context = nil;
  111. gl_windowinfo_destroy(platform->swap.wi);
  112. bfree(platform);
  113. }
  114. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  115. {
  116. if(!info)
  117. return NULL;
  118. if(!info->window.view)
  119. return NULL;
  120. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  121. memset(wi, 0, sizeof(struct gl_windowinfo));
  122. wi->view = info->window.view;
  123. return wi;
  124. }
  125. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  126. {
  127. if(!wi)
  128. return;
  129. wi->view = nil;
  130. bfree(wi);
  131. }
  132. void gl_update(device_t device)
  133. {
  134. [device->plat->context update];
  135. }
  136. void device_entercontext(device_t device)
  137. {
  138. CGLLockContext([device->plat->context CGLContextObj]);
  139. [device->plat->context makeCurrentContext];
  140. }
  141. void device_leavecontext(device_t device)
  142. {
  143. [NSOpenGLContext clearCurrentContext];
  144. CGLUnlockContext([device->plat->context CGLContextObj]);
  145. }
  146. void device_load_swapchain(device_t device, swapchain_t swap)
  147. {
  148. if(!swap)
  149. swap = &device->plat->swap;
  150. if(device->cur_swap == swap)
  151. return;
  152. device->cur_swap = swap;
  153. }
  154. void device_present(device_t device)
  155. {
  156. [device->plat->context flushBuffer];
  157. }
  158. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  159. uint32_t *height)
  160. {
  161. if(width) *width = swap->info.cx;
  162. if(height) *height = swap->info.cy;
  163. }
  164. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  165. {
  166. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  167. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  168. memset(tex, 0, sizeof(struct gs_texture_2d));
  169. OSType pf = IOSurfaceGetPixelFormat(ref);
  170. if (pf != 'BGRA')
  171. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  172. pf >> 24, pf >> 16, pf >> 8, pf);
  173. const enum gs_color_format color_format = GS_BGRA;
  174. tex->base.device = device;
  175. tex->base.type = GS_TEXTURE_2D;
  176. tex->base.format = GS_BGRA;
  177. tex->base.levels = 1;
  178. tex->base.gl_format = convert_gs_format(color_format);
  179. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  180. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  181. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  182. tex->base.is_dynamic = false;
  183. tex->base.is_render_target = false;
  184. tex->base.gen_mipmaps = false;
  185. tex->width = IOSurfaceGetWidth(ref);
  186. tex->height = IOSurfaceGetHeight(ref);
  187. if (!gl_gen_textures(1, &tex->base.texture))
  188. goto fail;
  189. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  190. goto fail;
  191. CGLError err = CGLTexImageIOSurface2D(
  192. [[NSOpenGLContext currentContext] CGLContextObj],
  193. tex->base.gl_target,
  194. tex->base.gl_internal_format,
  195. tex->width, tex->height,
  196. tex->base.gl_format,
  197. tex->base.gl_type,
  198. ref, 0);
  199. if(err != kCGLNoError) {
  200. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  201. " (texture_create_from_iosurface)",
  202. err, CGLErrorString(err));
  203. gl_success("CGLTexImageIOSurface2D");
  204. goto fail;
  205. }
  206. if (!gl_tex_param_i(tex->base.gl_target,
  207. GL_TEXTURE_MAX_LEVEL, 0))
  208. goto fail;
  209. if (!gl_bind_texture(tex->base.gl_target, 0))
  210. goto fail;
  211. return (texture_t)tex;
  212. fail:
  213. texture_destroy((texture_t)tex);
  214. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  215. return NULL;
  216. }
  217. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  218. {
  219. if (!texture)
  220. return false;
  221. if (!iosurf)
  222. return false;
  223. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  224. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  225. OSType pf = IOSurfaceGetPixelFormat(ref);
  226. if (pf != 'BGRA')
  227. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  228. pf >> 24, pf >> 16, pf >> 8, pf);
  229. if (tex->width != IOSurfaceGetWidth(ref) ||
  230. tex->height != IOSurfaceGetHeight(ref))
  231. return false;
  232. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  233. return false;
  234. CGLError err = CGLTexImageIOSurface2D(
  235. [[NSOpenGLContext currentContext] CGLContextObj],
  236. tex->base.gl_target,
  237. tex->base.gl_internal_format,
  238. tex->width, tex->height,
  239. tex->base.gl_format,
  240. tex->base.gl_type,
  241. ref, 0);
  242. if(err != kCGLNoError) {
  243. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  244. " (texture_rebind_iosurface)",
  245. err, CGLErrorString(err));
  246. gl_success("CGLTexImageIOSurface2D");
  247. return false;
  248. }
  249. if (!gl_bind_texture(tex->base.gl_target, 0))
  250. return false;
  251. return true;
  252. }